首页 › 题库 › C/C++编程考级 › C/C++编程三级 › 简答题
简答题 偏难
#7331

和数(2023.3) 给定一个正整数序列,判断其中有多少个数,等于数列中其他两个数的和。 比如,对于数列1 2 3 4, 这个问题的答案就是2, 因为3 = 2…

和数(2023.3)

给定一个正整数序列,判断其中有多少个数,等于数列中其他两个数的和。 比如,对于数列1 2 3 4, 这个问题的答案就是2, 因为3 = 2 + 1, 4 = 1 + 3。

时间限制:10000

内存限制:65536

输入

共两行,第一行是数列中数的个数n ( 1 <= n <= 100),第二行是由n个不大于10000的正整数组成的数列,相邻两个整数之间用单个空格隔开。

输出

一个整数,即数列中等于其他两个数之和的数的个数。

样例输入

4
1 2 3 4

样例输出

2
未登录也能判分,登录后才会记录
答案与解析
正确答案:<pre class="brush:cpp;toolbar:false">// 参考答案1: #include &lt;cstdio&gt; #include &lt;iostream&gt; using namespace std; int a[105]; int main() {   int n, ans = 0;   cin &gt;&gt; n;   for (int i = 0; i &lt; n; i++) {     cin &gt;&gt; a[i];   }   for (int i = 0; i &lt; n; i++) {     bool f = false;     for (int j = 0; j &lt; n; j++) {       for (int k = 0; k &lt; n; k++) {         if (i != j &amp;&amp; i != k &amp;&amp; j != k &amp;&amp; a[i] == a[j] + a[k]) {           f = true;         }       }     }     if (f == true)       ans++;   }   cout &lt;&lt; ans;   return 0; }</pre><pre class="brush:cpp;toolbar:false">// 参考答案2: #include &lt;algorithm&gt; #include &lt;array&gt; #include &lt;cmath&gt; #include &lt;cstring&gt; #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt; #include &lt;vector&gt; using namespace std; int main() {   int N[100];   int n;   cin &gt;&gt; n;   for (int i = 0; i &lt; n; i++) {     cin &gt;&gt; N[i];   }   int total = 0;   for (int c = 0; c &lt; n; c++) {     bool done = false;     for (int a = 0; a &lt; n &amp;&amp; !done; a++) {       for (int b = a; b &lt; n &amp;&amp; !done; b++) {         if (a != b &amp;&amp; a != c &amp;&amp; b != c &amp;&amp; N[c] == N[a] + N[b]) {           total++;           done = true;         }       }     }   }   cout &lt;&lt; total &lt;&lt; endl;   return 0; }</pre>
这道题暂未补解析。