您的位置:首页 > 大数据 > 人工智能

usaco training-Runaround Numbers

2011-06-02 17:56 232 查看
//usaco training-Runaround Numbers
/*
* 类型:枚举
* 思路:先判断是否满足不含数字0 和 数字无重复这一条件
* 后进行其他证明
* 将整数转化成数组的形式以方便进行数位操作
* 执行len(位数)次循环操作并作出相应的判断
*/
/*
Compiling...
Compile: OK
Executing...
Test 1: TEST OK [0.000 secs, 3028 KB]
Test 2: TEST OK [0.000 secs, 3028 KB]
Test 3: TEST OK [0.000 secs, 3028 KB]
Test 4: TEST OK [0.000 secs, 3028 KB]
Test 5: TEST OK [0.081 secs, 3028 KB]
Test 6: TEST OK [0.027 secs, 3028 KB]
Test 7: TEST OK [0.162 secs, 3028 KB]
All tests OK.
YOUR PROGRAM ('runround') WORKED FIRST TIME!  That's fantastic
-- and a rare thing.  Please accept these special automated
congratulations.
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <fstream>
using namespace std;

unsigned long long N;
int sign[10],res[50];

bool is_ligal(unsigned long long x);
bool is_runround(unsigned long long x);

int main(void)
{
unsigned long long i;
ifstream fin("runround.in");
ofstream fout("runround.out");
for(fin>>N,i=N+1; ; ++i){
if(is_ligal(i) && is_runround(i))
break;
}
fout<<i<<endl;
return 0;
}

bool is_ligal(unsigned long long x)
{
memset(sign,0,sizeof(sign));
while(x){
if(!(x%10) || sign[x%10])
return false;
x/=10;
}
return true;
}

bool is_runround(unsigned long long x)
{
int i,step,len=0,k=0;
while(x){
res[len++]=x%10;
x/=10;
}
reverse(res,res+len);
memset(sign,0,sizeof(sign));
sign[res[0]]=1,i=0;
while(k!=len){
step=res[i]%len;
i=i+step;
if(i>=len)
i=(i+len)%len;
if(!sign[res[i]])
sign[res[i]]=1;
else if((k==len-1 && i!=0) || k!=len-1)
return false;
k++;
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: