hdu2103

Family planning

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7311    Accepted Submission(s): 1897


Problem Description
As far as we known,there are so many people in this world,expecially in china.But many people like LJ always insist on that more people more power.And he often says he will burn as much babies as he could.Unfortunatly,the president XiaoHu already found LJ's extreme mind,so he have to publish a policy to control the population from keep on growing.According the fact that there are much more men than women,and some parents are rich and well educated,so the president XiaoHu made a family planning policy:
According to every parents conditions to establish a number M which means that parents can born M children at most.But once borned a boy them can't born other babies any more.If anyone break the policy will punished for 10000RMB for the first time ,and twice for the next time.For example,if LJ only allowed to born 3 babies at most,but his first baby is a boy ,but he keep on borning another 3 babies, so he will be punished for 70000RMB(10000+20000+40000) totaly.
 

Input
The first line of the input contains an integer T(1 <= T <= 100) which means the number of test cases.In every case first input two integers M(0<=M<=30) and N(0<=N<=30),N represent the number of babies a couple borned,then in the follow line are N binary numbers,0 represent girl,and 1 represent boy.
 

Output
Foreach test case you should output the total money a couple have to pay for their babies.
 

Sample Input
2 2 5 0 0 1 1 1 2 2 0 0
 

Sample Output
70000 RMB 0 RMB
 
這道題,我覺得是一道水題,不過,還是需要考慮一些東西的。當法定生子數與實際生子數的大小關係。同時還要考慮當法定生子數爲零的情況。還要考慮生第一個兒子是第幾個生的(假如有的話)。

思路:從題意可以知道,當生了兒子後,以後所生的孩子都是要罰款的。那我們不妨把第一個兒子的位置設爲罰款點。然後從罰款點一直罰到實際生子數。不過,當n爲0的時候,要獨立考慮。
這道題我WA了幾次,因爲我總把1看成女孩,還有就是一個小問題
錯誤代碼:
#include<stdio.h>
#include<>
int main()
{
    int t,n,m,i,x,p=1;
    scanf("%d",&t);
    while(t--)
    {
        int first=1,flag=0;
        scanf("%d%d",&m,&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&x);
            if(first&&x)//  0是女孩,1是男孩。。
            {
                first=0;
                flag=i;
            }
        }
        __int64 j=10000,sum=0;    // j和sum都要用__int64 否則WA
        if(flag && flag<=m)
        {
            for(i=flag+1;i<=n;i++)
            {
                sum+=j;
                j*=2;
            }
        }
        else
        {
            for(i=m+1;i<=n;i++)
            {
                sum+=j;
                j*=2;
            }
        }
        printf("%I64d RMB\n",sum);
    }
    return 0;
}

正確代碼:
#include<stdio.h>
#include<string.h>
int main()
{
    int t,n,m,i,x,p=1;
    __int64 j,sum;
    scanf("%d",&t);
    while(t--)
    {
        int first=1,flag=0;
        scanf("%d%d",&m,&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&x);
            if(first&&x)  //  0是女孩,1是男孩。。
            {
                first=0;
                flag=i;
            }
        }
        j=10000,sum=0;    // j和sum都要用__int64 否則WA
        if(flag && flag<=m)
        {
            for(i=flag+1;i<=n;i++)
            {
                sum+=j;
                j*=2;
            }
        }
        else
        {
            for(i=m+1;i<=n;i++)
            {
                sum+=j;
                j*=2;
            }
        }
        printf("%I64d RMB\n",sum);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章