'commit'
This commit is contained in:
39
TangDou/XiTi/1267.cpp
Normal file
39
TangDou/XiTi/1267.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int a[105];
|
||||
|
||||
// 这个贪心算法太有意思了~
|
||||
int main() {
|
||||
//输入+输出重定向
|
||||
freopen("../1267.txt", "r", stdin);
|
||||
|
||||
int n, i, avg, count, sum = 0;
|
||||
count = 0;
|
||||
cin >> n;
|
||||
//读入数据,并计算出平均数
|
||||
for (i = 0; i < n; i++) {
|
||||
cin >> a[i];
|
||||
sum += a[i];
|
||||
}
|
||||
avg = sum / n;
|
||||
//从前面第一个开始,就计算出距离平均数都差多少
|
||||
for (i = 0; i < n; i++) {
|
||||
a[i] = a[i] - avg;
|
||||
}
|
||||
//遍历所有的堆
|
||||
for (i = 0; i < n - 1; i++) {
|
||||
if (a[i] != 0) //如果等于0的话就不用管了;
|
||||
{
|
||||
a[i + 1] = a[i] + a[i + 1]; //不管是正还是负,都向后面一个要
|
||||
a[i] = 0; //把自己标识为与平均值的差为0,就是达标了
|
||||
count++; //要了一次加1一次
|
||||
} else
|
||||
continue;
|
||||
}
|
||||
cout << count << endl;
|
||||
//关闭文件
|
||||
fclose(stdin);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user