D - 4 CodeForces - 697A(模拟)

Ted has a pineapple. This pineapple is able to bark like a bulldog! At time t (in seconds) it barks for the first time. Then every s seconds after it, it barks twice with 1 second interval. Thus it barks at times t, t + s, t + s + 1, t + 2s, t + 2s + 1, etc.

Barney woke up in the morning and wants to eat the pineapple, but he can’t eat it when it’s barking. Barney plans to eat it at time x (in seconds), so he asked you to tell him if it’s gonna bark at that time.

Input
The first and only line of input contains three integers t, s and x (0 ≤ t, x ≤ 109, 2 ≤ s ≤ 109) — the time the pineapple barks for the first time, the pineapple barking interval, and the time Barney wants to eat the pineapple respectively.

Output
Print a single “YES” (without quotes) if the pineapple will bark at time x or a single “NO” (without quotes) otherwise in the only line of output.

Examples
Input
3 10 4
Output
NO
Input
3 10 3
Output
YES
Input
3 8 51
Output
YES
Input
3 8 52
Output
YES
Note
In the first and the second sample cases pineapple will bark at moments 3, 13, 14, …, so it won’t bark at the moment 4 and will bark at the moment 3.

In the third and fourth sample cases pineapple will bark at moments 3, 11, 12, 19, 20, 27, 28, 35, 36, 43, 44, 51, 52, 59, …, so it will bark at both moments 51 and 52.

题意:输入t,s,x。在t,t + s,t + s + 1,t + 2s,t + 2s + 1…的时间点pineapple会叫。如果x时间的时候会叫,输出YES,否则输出NO。
思路:模拟吧,一开始觉得把每个会叫的时间打表就可以了,但是数据是1e9,数组都开不下。那么可以直接开个for,模拟t的变化过程,如果变化的过程中t与x相等,则输出YES,否则为NO。

#include <bits/stdc++.h>//0404晚训C

using namespace std;
const int maxn = 1e9;
int main()
{
    int t,s,x;
    cin>>t>>s>>x;
    if(t == x)
    {
        printf("YES\n");
        return 0;
    }
    int tmp = t;
    for(int i = t;i <= maxn;i++)
    {
        if(tmp + s == x || tmp + s + 1 == x)
        {
            printf("YES\n");
            return 0;
        }
        else if(tmp > x)
        {
            printf("NO\n");
            return 0;
        }
        tmp = tmp + s;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/89042078