NC22922. 循环数
描述
输入描述
仅仅一行,包括M
输出描述
仅仅一行,包括第一个比M大的循环数。
示例1
输入:
81361
输出:
81362
C 解法, 执行用时: 134ms, 内存消耗: 324K, 提交时间: 2022-09-23 10:34:01
#include<stdio.h> int a[10],len; int book[20]; void cunshu(int c) { int t=1,b; for(int i=len-1;i>=0;i--) { b=pow(10,i); a[t++]=c/b; c=c%b; } } int main() { int n,t,i,j,r,k,flag,flag1,flag2,t2,t3,r1; while(~scanf("%d",&n)) { for(i=n+1;;i++) { memset(book,0,sizeof(book)); flag=flag1=flag2=0; len=(int)log10(i)+1;//81361 cunshu(i); for(j=1;j<len;j++) { for(k=j+1;k<=len;k++) { if(a[j]==a[k]||a[j]==0||a[k]==0) { flag=1; break; } } if(flag==1) break; } if(flag==1) continue; else { t2=len; r=a[1]; t3=1; while(t2--) { r1=r; t3=(r+t3)%len; if(t3==0) t3=len; r=a[t3]; book[a[t3]]++; if((a[t3]==r1&&j!=len)||book[a[t3]]>1) { flag1=1; break; } } if(flag1==1) continue; else if(a[t3]==a[1]&&book[a[1]]<=1) { printf("%d\n",i); break; } } } } return 0; }
C++(g++ 7.5.0) 解法, 执行用时: 27ms, 内存消耗: 312K, 提交时间: 2022-09-25 12:45:00
#include<stdio.h> #include<string.h> #include<math.h> int main() { int m,n,i,j,k,s,t,x; int a[20],b[20],c[20]; int find; while(scanf("%d",&n)!=EOF) { for(x=n+1;;x++) { if(x==0) continue; find=0; for(t=1;t<=9;t++) { c[t]=b[t]=0; } m=x; s=k=(int)log10(m); while(m!=0) { a[k--]=m%10; // printf("%d*%d\n",k+1,a[k+1]); if(c[m%10]==1 || m%10==0) { find=1; break; } c[m%10]=1; m/=10; } if(find==1) continue; i=0;t=1; while(1) { i=(i+a[i])%(s+1); if(i==0 && t==s+1) { find=2; break; } else if(b[i]==1) { find=1; break; } else { b[i]=1; t++; } } if(find==1) continue; else if(find==2) { for(t=0;t<=s;t++) printf("%d",a[t]); printf(" \n"); break; } } } return 0; }
Pascal(fpc 3.0.2) 解法, 执行用时: 127ms, 内存消耗: 364K, 提交时间: 2019-08-25 14:20:37
var n:int64; function xunhuan:boolean; var i,j:longint; k,t,l,x:qword; w:array[0..99]of qword; s:string; begin k:=1; t:=0; str(n,s); l:=length(s); fillchar(w,sizeof(w),0); for i:=1 to l do begin val(s[i],x); if(w[x]>0)or(x=0)then exit(false); inc(w[x]); end; fillchar(w,sizeof(w),0); while t<l do begin val(s[k],x); if w[x]>0 then exit(false); inc(w[x]); inc(t); k:=(k+x) mod l; if k=0 then k:=l; end; if k<>1 then exit(false); exit(true); end; begin readln(n); while true do begin inc(n); if xunhuan then begin write(n); halt; end; end; end.
C++14(g++5.4) 解法, 执行用时: 11ms, 内存消耗: 508K, 提交时间: 2020-03-11 14:48:32
#include <bits/stdc++.h> using namespace std; int m; int ch[10]; bool vis[10]; bool check(int x) { memset(vis, 0, sizeof(vis)); int xlen = 0; for (; x; x /= 10) { if (x%10 == 0 || vis[x%10]) return false; ch[xlen++] = x%10; vis[x%10] = 1; } memset(vis, 0, sizeof(vis)); int st = 0, cnt = 0; for (; !vis[st]; st = (9*xlen+st-ch[st])%xlen) { ++cnt; vis[st] = 1; } return (cnt == xlen && st == 0); } int main() { cin >> m; for (int x = m+1; ; ++x) { if (check(x)) { cout << x << endl; break; } } return 0; }
Python3(3.5.2) 解法, 执行用时: 695ms, 内存消耗: 3432K, 提交时间: 2020-05-29 00:22:35
def nextNum(n): while True: n+=1 digits = set([x for x in str(n)]) if '0' in digits: continue if len(str(n))==len(digits): return n def isNum(n): digits = [int(x) for x in str(n)] N = len(str(n)) s = set() i, x = 0, digits[0] for m in range(N): i = (x % N + i) % N x = digits[i] if x in s: return False s.add(x) return True n = int(input()) while True: n = nextNum(n) if isNum(n): print(n) break
C++ 解法, 执行用时: 72ms, 内存消耗: 424K, 提交时间: 2022-01-28 09:36:36
#include<bits/stdc++.h> using namespace std; int n,i; int main() { cin>>n; for(i=n+1;;i++) { string c=to_string(i); int x=c[0]-'0',f[12]={0},j=0,l=0; for(;j<c.size();j++) { l=(l+x)%c.size(); f[c[l]-'0']++; x=c[l]-'0'; }for(j=0;j<c.size();j++) if(f[c[j]-'0']-1)goto w; cout<<i<<endl;return 0;w:; } }