USACO-Section2.2 Runaround Numbers

xiaoxiao2021-02-28  102

2017-9-1

题目描述

给定一个数M,找到并输出刚好比M大的下个循环数

解答

直接按照题目所给步骤求解即可 tips: 数不重复,不存在零 最后回到零的位置且必须走n次走过所有n位数字

代码

/* ID: 18795871 PROG: runround LANG: C++ */ #include<iostream> #include<fstream> #include<cstring> using namespace std; const int N = 20; ifstream fin("runround.in"); ofstream fout("runround.out"); int x[N+1]; bool d[N+1],f[N+1]; int l; bool cal(long n){ //判断是否存在零并把数拆开来 int i=0,j; int y[N+1]; while (n){ y[i]=n%10; if (d[y[i]]) return false; d[y[i]]=true; n/=10; if (y[i]==0) return false; i++; } l=i; for (j=0;j<l;j++) x[j]=y[l-j-1]; return true; } bool fun(){ for (int i=0;i<l;i++){ if (!f[i]) return false; } return true; } bool res(long n){ int i,j,k=0; for (i=0;i<l;i++){ f[k]=true; int p=x[k]; for (j=0;j<p;j++){ k=(k+1)%l; } } if (k==0&&fun()) return true; return false; } int main() { long i,n; fin>>n; for (i=n+1;;i++){ memset(x,0,sizeof(x)); memset(d,false,sizeof(d)); memset(f,false,sizeof(f)); if (!cal(i)) continue; if (res(i)) break; } fout<<i<<endl; return 0; }
转载请注明原文地址: https://www.6miu.com/read-85778.html

最新回复(0)