Runaround numbers are integers with unique digits, none of which iszero (e.g., 81362) that also have an interesting property, exemplifiedby this demonstration:
Given a number M (that has anywhere from 1 through 9 digits), findand print the next runaround number higher than M, which will alwaysfit into an unsigned long integer for the given test data.
81361
82362
这题没什么特别的思路,就是从给出数字的下一个数字开始判断它是否是循环数,是的话直接输出就好了。
源代码:
/* ID: supersnow0622 PROG: runround LANG: C++ */ #include <iostream> #include <fstream> #include <string> #include<math.h> #include<memory.h> using namespace std; int used[10],exist[10]; bool allused() { for(int i=1;i<10;i++) if(exist[i]==1&&used[i]==0) return false; return true; } bool judge(unsigned int n) { memset(exist,0,sizeof(exist)); string str=""; int temp,sum=0; while(n!=0) { temp=n%10; if(temp==0||exist[temp]==1) return false; exist[temp]=1; sum+=temp; str+=temp+'0'; n/=10; } memset(used,0,sizeof(used)); int index=str.length()-1; used[str[index]-'0']=1; while(!allused()) { index-=str[index]-'0'; index=(index+str.length()*10)%str.length(); if(used[str[index]-'0']) return false; used[str[index]-'0']=1; if(str[index]==str[str.length()-1]) return false; } index-=str[index]-'0'; index=(index+str.length()*10)%str.length(); if(str[index]==str[str.length()-1]) return true; return false; } int main() { ofstream fout ("runround.out"); ifstream fin ("runround.in"); unsigned int M; fin>>M; for(unsigned int i=M+1;i<1<<24;i++) { if(judge(i)) { fout<<i; break; } } return 0; }