大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全家桶1年46,售后保障稳定
Candy Sharing Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5890 Accepted Submission(s): 3580
Write a program which determines the number of times the teacher blows the whistle and the final number of pieces of candy for each student from the amount of candy each child starts with.
6 36 2 2 2 2 2 11 22 20 18 16 14 12 10 8 6 4 2 4 2 4 6 8 0
15 14 17 22 4 8HintThe game ends in a finite number of steps because: 1. The maximum candy count can never increase. 2. The minimum candy count can never decrease. 3. No one with more than the minimum amount will ever decrease to the minimum. 4. If the maximum and minimum candy count are not the same, at least one student with the minimum amount must have their count increase.比较坑的地方是一个人必须先给下一个才能拿上一个人的,之前一直理解错误代码:比比较#include<algorithm> #include<iostream> #include<cstring> #include<cmath> using namespace std; int cake[10010]; int n; int pan() { int t = cake[0]; for(int i = 1;i < n;i++) { if(t != cake[i]) { return 1; } } return 0; } int main() { while(cin >> n&&n) { for(int i = 0;i < n;i++) cin >> cake[i]; int ans = 0; while(pan()) { ans++; int tem = cake[n-1]/2; for(int i = n-1;i > 0;i--) { cake[i] = cake[i]/2+cake[i-1]/2; if(cake[i] % 2) cake[i]++; } cake[0] = cake[0]/2 + tem; if(cake[0] % 2) cake[0]++; } cout << ans << " " << cake[0] << endl; } return 0;}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/234351.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...