Family planning(计划生育)

Family planning(计划生育)

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.
译文:据我们所知,这个世界上有这么多人,特别是在中国。但很多像LJ这样的人总是坚持让更多的人有更多的权力。他经常说他会尽可能多地烧掉婴儿。不幸的是,小胡总统已经找到了LJ的极端思想,所以他必须制定一项控制人口不断增长的政策。根据男性多于女性,有些父母富裕,受过良好教育的事实,小胡总统做了计划生育政策:
根据每个家长的条件建立一个M号,这意味着父母最多可以生M个孩子。但是一旦生了男孩,他们就不能再生下其他的孩子了。如果有人打破政策,将首次处罚10000RMB,例如,如果LJ最多只允许生3个孩子,但他的第一个孩子是男孩,但他继续生下另外3个孩子,所以他将被处以70000RMB(10000 + 20000 +) 40000)总计。

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.
译文:输入的第一行包含一个整数T(1 <= T <= 100),表示测试用例的数量。在每种情况下,首先输入两个整数M(0 <= M <= 30)和N(0 <= N <= 30),N代表一对夫妇生的婴儿数,然后在下面的行中是N个二进制数,0代表女孩,1代表男孩。

Output

Foreach test case you should output the total money a couple have to pay for their babies.
译文:在Foreach测试案例中,您应该输出一对夫妇必须为其婴儿支付的总金额。

Sample Input

2
2 5
0 0 1 1 1

2 2
0 0

Sample Output

70000 RMB
0 RMB

C++编写:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

int kid[30];
int main()
{
    int T,M,N;
    cin>>T;
    while(T--)
    {
        cin>>M>>N;
        for(int i=0;i<N;i++)
            cin>>kid[i];						
        int pos=-2,money=1,sum=0;    //此处必须指明sum=0,pos一定不能为自然数
        for(int j=0;j<M;j++)
        {
            if(kid[j]==1)
            {
                pos = j;
                break;
            }
        }	
        if(pos != -2)
        {
            for(int i=pos+1;i<N;i++)
            {
                sum +=money;
                money *= 2;
            }
        }		
        else
        {
            for(int i=M;i<N;i++)
            {
                sum += money;
                money *= 2;
            }
        }
        if(sum != 0)
            cout<<sum<<"0000 RMB"<<endl;
        else
            cout<< "0 RMB" <<endl;
    }
    return 0;
}

另解:

#include<iostream>
using namespace std;
int main()
{
    int T,M,N;
    int kid[30];
    cin>>T;
    while(T--)
    {
        cin>>M>>N;
        int a = 0,sum = 0;
        for(int i=0;i<N;i++)
            cin>>kid[i];						
        int money = 1;				
        for(int i=0;i<N;i++)
        {						
            if(a)                 
            {				
                sum += money;				
                money *= 2;			
            }						
            if(kid[i] && i < M)				
                a = 1;							
            if(!a && i >= M)
            {				
                sum += money;				
                money *= 2;			
            }		
        }
        if(sum)
        cout<<sum<<"0000 RMB"<<endl;
        else
        cout<< "0 RMB" <<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值