您的位置:首页 > 其它

USACO Section 2.2: Runaround Numbers

2013-07-07 06:10 399 查看
简单题

/*
ID: yingzho1
LANG: C++
TASK: runround
*/
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <cstring>

using namespace std;

ifstream fin("runround.in");
ofstream fout("runround.out");

int N;

int weishu(int n) {
int ret = 0;
while (n) {
n /= 10;
ret++;
}
return ret;
}

bool isRunAround(int n) {
int acount = weishu(n);
vector<bool> visit(acount);
string s;
while (n) {
if (n % 10 == 0) return false;
s += '0' + n % 10;
n /= 10;
}
reverse(s.begin(), s.end());
int cur = 0;
int len = s.size();
for (int i = 0; i < acount; i++) {
cur = (cur + int(s[cur] - '0')) % len;
if (visit[cur]) return false;
visit[cur] = true;
}
return true;
}

bool isUnique(int n) {
set<int> S;
while (n) {
if (S.count(n % 10)) return false;
S.insert(n % 10);
n /= 10;
}
return true;
}

int main()
{
fin >> N;
int M = N+1;
while (1) {
if (isUnique(M) && isRunAround(M)) break;
M++;
}
fout << M << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: