CodeFoeces-697A

题目

原题链接:A. Pineapple Incident

题意

给出t和s,按照t,t+s,t+s+1,t+2s,t+2s+1.....的规律能否得到x。用while居然超时了。第二种方法是参考其他作者的。

代码

模拟
#include
using namespace std;
int main() {
    int s,t,x;
    scanf("%d%d%d",&t,&s,&x);
    int flag=0,tmp=1,ans=s-1;
    if(t==x){
        printf("YES\n");
    }else{
        for(int i=1;i<1000000000;i++){
            if(t+i*s==x || t+i*s+1==x){
                printf("YES\n");
                break;
            }else if(t+i*s>x){
                printf("NO\n");
                break;
            }
        }
    }
    return 0;
}
规律
#include
using namespace std;
int main() {
    int s,t,x;
    scanf("%d%d%d",&t,&s,&x);
    if(x==t || (x>t && (t-x%s==0)) || (x>t+1 && (x-t-1%s==0))) printf("YES\n");
    else printf("NO\n");
    return 0;
}

你可能感兴趣的:(CodeFoeces-697A)