zcmu-1123: 松哥的困惑III

 

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 862  Solved: 143
[Submit][Status][Web Board]

Description

松哥大吃一顿后,他的体重随着时间的增长而不断增长,直到有一天他的体重达到了n吨,他意识到他不能再这样下去了,所以他居然决定减肥。他每天上午跑步能够减到a吨,但是晚上吃饭又增加了b吨。松哥想要直到第几天后他的体重第一次小于m吨,你能告诉他嘛?

Input

多组测试数据。

每组测试数据包含4个正整数n,m,a,b。

所有的整数大小均不大于10000。

Output

对于每组测试数据,输出一个整数代表松哥第几天后他的体重第一次小于m吨。

如果不可能输出”impossible”.

Sample Input

5 1 3 1

Sample Output

2


 

注意要用最低体重来判断

【通过代码】 参考了同学的代码

#include<stdio.h>
#include<math.h>
int main()
{
    int a,i,m,n,t,b,x;
    while(~scanf("%d%d%d%d",&n,&m,&a,&b))
    {
        x=0;
        while(n>=m)
        {
            x++;
            n-=a;
            if(n<m)
                break;
            if(b>=a)
            {
                x=-1;
                break;
            }
            n+=b;
        }
        if(x>=0)
            printf("%d\n",x);
        else
            printf("impossible\n");
    }
    return 0;
}

 【我的思路(WA)】

输入n,m,a,b,得到当前体重n,目标体重m,上午减重Δweight = n - a,下午增重 Δweight = b

注意:上午结束后体重达到最低,只要求这个数第一次小于m时之前的天数就行,因为当天还没过完,求出的是cnt - 1。

需要天数记为cnt;

有以下几种可能:

1.体重n <= 目标体重m

   不用减肥, cnt = 0;

2.体重n > 目标体重m

要减肥,

  2.1上午减重Δweigh <= 下午增重 Δweight

    减的还没增的多,不可能达到目标体重,输出”impossible”.

  2.2上午减重Δweigh > 下午增重 Δweight

    计算最低体重 < m时的天数,cnt - 1;

【代码(WA)】

找错! 挖个坑,日后来补


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <stack>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
    int n,m,a,b;
    while(scanf("%d",&n) != EOF)
    {
        int cnt = 0;
        scanf("%d%d%d",&m,&a,&b);
        if(n <= m)
            cnt = 0;
        else
        {
            if(n - a <= b)
                cnt = -1;
            else
            {
                int d1 = n - a;
                cnt = 0;
                while(n > m)
                {
                    cnt++;
                    int x = n - d1;//最低体重
                    if(x <= m)
                        break;
                    n = n - d1 + b;
                   
                }
                cnt--;
                if(cnt < 1)
                    cnt = 1;
            }
        }
        if(cnt >= 0)
            cout<<cnt<<endl;
        else
            cout<<"impossible"<<endl;
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/82708184