简答题
偏难
#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 <cstdio>
#include <iostream>
using namespace std;
int a[105];
int main() {
int n, ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
bool f = false;
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (i != j && i != k && j != k && a[i] == a[j] + a[k]) {
f = true;
}
}
}
if (f == true)
ans++;
}
cout << ans;
return 0;
}</pre><pre class="brush:cpp;toolbar:false">// 参考答案2:
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
int N[100];
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> N[i];
}
int total = 0;
for (int c = 0; c < n; c++) {
bool done = false;
for (int a = 0; a < n && !done; a++) {
for (int b = a; b < n && !done; b++) {
if (a != b && a != c && b != c && N[c] == N[a] + N[b]) {
total++;
done = true;
}
}
}
}
cout << total << endl;
return 0;
}</pre>
这道题暂未补解析。