A Game with Marbles
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
There are n bowls, numbered from 1 to n. Initially, bowl i contains m
i marbles. One game step consists of removing one marble from a bowl. When removing a marble from bowl i (i > 1), one marble is added to each of the first i-1 bowls; if a marble is removed from bowl 1, no new marble is added. The game is finished after each bowl is empty.
Your job is to determine how many game steps are needed to finish the game. You may assume that the supply of marbles is sufficient, and each bowl is large enough, so that each possible game step can be executed.
Your job is to determine how many game steps are needed to finish the game. You may assume that the supply of marbles is sufficient, and each bowl is large enough, so that each possible game step can be executed.
Input
The input contains several test cases. Each test case consists of one line containing one integer n (1 ≤ n ≤ 50), the number of bowls in the game. The following line contains n integers m
i (1 ≤ i ≤ n, 0 ≤ m
i ≤ 1000), where m
i gives the number of marbles in bowl i at the beginning of the game.
The last test case is followed by a line containing 0.
The last test case is followed by a line containing 0.
Output
For each test case, print one line with the number of game steps needed to finish the game. You may assume that this number fits into a signed 64-bit integer.
Sample Input
10 3 3 3 3 3 3 3 3 3 3 5 1 2 3 4 5 0
Sample Output
3069 129/*题目意思:从第n个碗取出一个球,则1——n-1个碗中都增加一个球,当n为1时不加,问经过多少次后变成空碗。先取n,再取n-1,n-2,······以此类推*/ #include<stdio.h> int main() { int n,i,j; __int64 sum,num[1005]; //一定要用64位整数,不然会Wrong Answer while(scanf("%d",&n)!=EOF) { if(n==0) break; sum=0; for(i=1;i<=n;i++) scanf("%I64d",&num[i]); for(j=n;j>0;j--) for(i=j-1;i>=1;i--) num[i]+=num[j]; //往前面的碗中加球 for(i=1;i<=n;i++) sum+=num[i]; //总求次数 printf("%I64d\n",sum); } return 0; }