首页 > ACM题库 > HDU-杭电 > hdu 2374 A Game with Marbles-模拟[解题报告]C++
2014
01-05

hdu 2374 A Game with Marbles-模拟[解题报告]C++

A Game with Marbles

问题描述 :

There are n bowls, numbered from 1 to n. Initially, bowl i contains mi 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.

输入:

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 mi (1 ≤ i ≤ n, 0 ≤ mi ≤ 1000), where mi 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 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 mi (1 ≤ i ≤ n, 0 ≤ mi ≤ 1000), where mi 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.

样例输入:

10
3 3 3 3 3 3 3 3 3 3
5
1 2 3 4 5
0

样例输出:

3069
129


题意:有n个碗,标号为1到n。每个碗内有一些小珠子。每次从一个碗里拿出一个珠子,此时要往所有编号小于它的碗内各放入一颗珠子。问要拿多少次才能把所有的珠子拿完。

mark:直接模拟。没注意I64d,1wa。

代码:

# include <stdio.h>


long long num[60] ;


int main ()
{
    int i, j, n ;
    long long rtn ;
    while (~scanf ("%d", &n),n)
    {
        rtn = 0 ;
        num[n] = 0 ;
        for(i = 0 ; i < n ; i++)
            scanf ("%I64d", &num[i]) ;
        for (i = n-1 ; i>= 0 ; i--)
        {
            rtn += num[i] ;
            for (j = i-1 ; j>= 0; j--)
                num[j] += num[i] ;
        }
        printf ("%I64d\n", rtn) ;
    }
    return 0 ;
}

 


  1. #include <cstdio>

    int main() {
    //answer must be odd
    int n, u, d;
    while(scanf("%d%d%d",&n,&u,&d)==3 && n>0) {
    if(n<=u) { puts("1"); continue; }
    n-=u; u-=d; n+=u-1; n/=u;
    n<<=1, ++n;
    printf("%dn",n);
    }
    return 0;
    }