煤气灶(2019牛客寒假算法基础集训营 Day6-B)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011815404/article/details/88382278

【题目描述】

小j开始打工,准备赚钱买煤气灶。
第一天,小j的工资为n元,之后每天他的工资都比前一天多d元。
已知煤气灶需要m元,求小j最少工作几天才能买到煤气灶。

【输入描述】

四个整数 n,m,d,x
分别表示小j第一天的工资,煤气灶的价格,工资每天的增长量,答案不超过x

【输出描述】

一个数表示答案

【样例】

示例1

输入
10 100 20 100
输出
4
说明
10+30+50+70>=100

思路:简单模拟

【源代码】

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define N 2000001
#define LL long long
using namespace std;
int main(){
    LL n,m,d,x;
    scanf("%lld%lld%lld%lld",&n,&m,&d,&x);

    LL before=n;
    LL today=0;
    LL sum=n;
    LL day=1;
    while(true){
        if(sum>=m)
            break;

        day++;
        today=before+d;
        before=today;
        sum+=today;
    }
    printf("%lld\n",day);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/u011815404/article/details/88382278