题目在这里1052
还是模拟题,看懂题目就好。循环的终止条件,我设置的是最大值和最小值相等。如果你也是用这个来判断的话,需要注意,这里的最大值和最小值在每次外循环开始的时候要复位一次。如果不复位的话,计算的就是整体的最大值和最小值了。不懂的话,用vs的F10跟踪调试一下就知道了。
下面是我的代码,仅供参考
#include<iostream>
using namespace std;
int students[100000], stu[100000];
int n;
int play_games(int &ans)
{
int max = 0, min = -1, times = 0;
while (max != min)
{
times++;
for (int i = 0; i < n; i++)
{
stu[(i + 1) % n] = students[(i + 1) % n] / 2;
stu[(i + 1) % n] += students[i] / 2;
if (stu[(i + 1) % n] % 2 != 0)
stu[(i + 1) % n]++;
}
max = 0;
min = 1000000;
for (int i = 0; i < n; i++)
{
students[i] = stu[i];
if (stu[(i + 1) % n] > max) max = stu[(i + 1) % n];
if (stu[(i + 1) % n] < min) min = stu[(i + 1) % n];
}
}
ans = max;
return times;
}
int main()
{
int ans;
while (cin >> n && n != 0)
{
for (int i = 0; i < n; i++)
{
cin >> students[i];
}
cout << play_games(ans) << " ";
cout << ans << endl;
}
//system("pause");
return 0;
}