首页 文章资讯内容详情

程序以C ++查找隐藏号码

2026-06-04 1 花语

在本教程中,我们将讨论查找隐藏号码的程序。

为此,我们将提供n个整数的数组。我们的任务是找到另一个整数,以使该整数与给定数组中所有元素的差总和为零。

示例

#include <iostream> using namespace std; int main() { //size of array int n = 3; int a[] = { 1, 2, 3 }; int i = 0; long sum = 0; for (i = 0; i < n; i++) { sum += a[i]; } long x = sum / n; if (x * n == sum) cout<<x<<endl; else cout<v("-1")<<endl; return 0; }

输出结果

2