zoukankan      html  css  js  c++  java
  • 【USACO 2.2】Runaround Numbers

    找出第一个大于n的数满足:每一位上的数都不同,且没有0,第一位开始每次前进当前这位上的数那么多位,超过总位数就回到开头继续往前进,最后能不能每个位都到过一次且回到第一位,$n<10^9$。

    暴力,每次n++后模拟一边判断是否符合条件。

    /*
    TASK:runround
    LANG:C++
    */
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int n;
    int get(int now,int step,int len){
        return (now+step-1)%len+1;
    }
    bool ck(){
        char s[11];
    
        sprintf(s+1,"%d",n);
        int now=1;
        int len=strlen(s+1);
        bool v[11],used[11];
        memset(v,0,sizeof v);
        memset(used,0,sizeof used);
        used[0]=1;
        //printf("%d:",n);
        for(int i=0;i<len;i++){
    
            if(used[s[now]-'0']||v[now])
                return 0;
            used[s[now]-'0']=v[now]=1;
            //printf("%d %c
    ",now,s[now]);
            now=get(now,s[now]-'0',len);
        }
        return now==1;
    }
    int main(){
        freopen("runround.in","r",stdin);
        freopen("runround.out","w",stdout);
        scanf("%d",&n);
        n++;
        while(!ck())n++;
        printf("%d
    ",n);
        return 0;
    }

      

  • 相关阅读:
    CSP-S 2020 游记
    USACO Mowing the Lawn
    洛谷 P1725 琪露诺
    浅谈单调队列
    浅谈单调栈
    洛谷 P1440 求m区间内的最小值
    POJ 2823 Sliding Window
    洛谷 P1901 发射站
    POJ 2796 Feel Good
    POJ 2559 Largest Rectangle in a Histogram
  • 原文地址:https://www.cnblogs.com/flipped/p/5975590.html
Copyright © 2011-2022 走看看