杭电oj(Java版)—— 1034 Candy Sharing Game
来源:互联网 发布:淘宝一块钱包邮的东西 编辑:程序博客网 时间:2024/06/06 02:14
Candy Sharing Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6482 Accepted Submission(s): 3938
Problem Description
A number of students sit in a circle facing their teacher in the center. Each student initially has an even number of pieces of candy. When the teacher blows a whistle, each student simultaneously gives half of his or her candy to the neighbor on the right. Any student, who ends up with an odd number of pieces of candy, is given another piece by the teacher. The game ends when all students have the same number of pieces of candy.
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.
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.
Input
The input may describe more than one game. For each game, the input begins with the number N of students, followed by N (even) candy counts for the children counter-clockwise around the circle. The input ends with a student count of 0. Each input number is on a line by itself.
Output
For each game, output the number of rounds of the game followed by the amount of candy each child ends up with, both on one line.
Sample Input
6362222211222018161412108642424680
Sample Output
15 1417 224 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.
import java.util.Scanner;public class Main{public static void main(String[] args) {Scanner scanner = new Scanner(System.in);while (scanner.hasNextInt()) {int n = scanner.nextInt();if (n==0) {break;}int arr[] = new int[n];for (int i=0;i<n;i++) {arr[i] = scanner.nextInt();}f(arr);}}//分糖果private static void f(int[] arr) {int temp = 0;while (!same(arr)) {int n = arr[arr.length-1];for (int i=arr.length-1;i>0;i--) {arr[i] = arr[i]/2+arr[i-1]/2;}arr[0] = arr[0]/2+n/2;g(arr);temp++;}System.out.println((temp)+" "+(arr[0]));}//奇数加一private static void g(int[] arr) {for(int i=0;i<arr.length;i++) {if (arr[i]%2!=0) {arr[i]++;}}}//是否糖果数相同private static boolean same(int[] arr) {int temp = 1;for (int i=1;i<arr.length;i++) {if (arr[0]!=arr[i]) {temp = 0;break;}}if (temp==1) {return true;}return false;}}
0 0
- 杭电oj(Java版)—— 1034 Candy Sharing Game
- 【杭电oj】1034-Candy Sharing Game(水)
- 杭电 1034:Candy Sharing Game
- 杭电 HDU 1034 Candy Sharing Game
- 杭电-1034Candy Sharing Game(模拟)
- 杭电1034 Candy Sharing Game
- HDU杭电1034 Candy Sharing Game
- 杭电1034(模拟法) 之 Candy Sharing Game
- 杭电ACM OJ 1034 Candy Sharing Game 水水更健康(只是题目意思有点难理解)
- 1034:Candy Sharing Game
- 1034 Candy Sharing Game
- HDU1034 Candy Sharing Game(java)
- 杭电HOJ1034 Candy Sharing Game 的一种解法
- HDU 1034 Candy Sharing Game(水题)
- hdu 1034 Candy Sharing Game
- hdu 1034 Candy Sharing Game
- HDU 1034 Candy Sharing Game
- hdoj-1034-Candy Sharing Game
- 手机wap端开发微信绑定第三方登陆功能
- 【JSP页面】利用数据库中的表格中的数据实现验证用户名和密码
- lua ... (变长参数)
- Android
- 从MySQL 5.7 版本以后,不再含有 data 目录
- 杭电oj(Java版)—— 1034 Candy Sharing Game
- Hive语法简述(基础部分)
- oracleServiceXxx服务不见了
- GDOI2017 总结
- redis代码实战
- HTML给div设置百分比高度无效问题
- hdu4310
- 基数排序法
- zoj1076Gene Assembly