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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值