标签:== scan stream 耗电 一个 charge har 有一个 beginning
大概题意如下:有两个玩具,分别拥有a1的电量和a2的电量;两个玩具每分钟耗电量为2单位,如果有一个或两个的电量下降到1以下就停止游戏;我们还有一个充电器,每分钟只能给一个玩具净充电1单位;问游戏时间长度。
解法很简单,取贪心策略:每次充电给电量较低的玩具充电。
题目做不出来往往是踩了这个坑:如果两个玩具开局电量都是1,那么游戏时间将是0。注意这句话:“ Hence, if at the beginning of minute some joystick is charged by 1 percent, it has to be connected to a charger, otherwise the game stops. ”(好吧我承认我当时在这个坑里爬不出来了)
下面附代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    int t=0;
    while(a>0&&b>0)
    {
        if(a==1&&b==1)
        {
            t=0;
            break;
        }
        t+=1;
        if(a<=b)
        {
            a+=1;
            b-=2;
        }
        else
        {
            a-=2;
            b+=1;
        }
    }
    printf("%d\n",t);
    return 0;
}    
标签:== scan stream 耗电 一个 charge har 有一个 beginning
原文地址:http://www.cnblogs.com/shao0099876/p/7296815.html