USACO 2.2 Runaround Numbers

题目:

Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration:

  • If you start at the left digit (8 in our number) and count that number of digits to the right (wrapping back to the first digit when no digits on the right are available), you'll end up at a new digit (a number which does not end up at a new digit is not a Runaround Number). Consider: 8 1 3 6 2 which cycles through eight digits: 1 3 6 2 8 1 3 6 so the next digit is 6.
  • Repeat this cycle (this time for the six counts designed by the `6') and you should end on a new digit: 2 8 1 3 6 2, namely 2.
  • Repeat again (two digits this time): 8 1
  • Continue again (one digit this time): 3
  • One more time: 6 2 8 and you have ended up back where you started, after touching each digit once. If you don't end up back where you started after touching each digit once, your number is not a Runaround number.

Given a number M (that has anywhere from 1 through 9 digits), find and print the next runaround number higher than M, which will always fit into an unsigned long integer for the given test data.

PROGRAM NAME: runround

INPUT FORMAT

A single line with a single integer, M

SAMPLE INPUT (file runround.in)

81361

OUTPUT FORMAT

A single line containing the next runaround number higher than the input value, M.

SAMPLE OUTPUT (file runround.out)

81362


分析: 水题。模拟。看懂题目是关键。注意一下数字不能有0,每一位数字都不同就ok了。

代码:

/*
ID: gjj50201
LANG: C++
TASK: runround
*/

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;

int vis[10], digits[10];
int M;

int convert(int k){
	int index = 0;
	int book[10];
        memset(book,0,sizeof(book));
	while(k>0){
		digits[index++] = k%10;
		if(k%10==0 || book[k%10] == 1)
			return -1;
		book[k%10] = 1;
		k = k/10;
	}
	return index-1;
}

int ok(int k){
	int len = convert(k);
	if(len<0)
		return 0;
	int pos = len;
	while(1){
		pos = (pos-digits[pos] ) % (len+1);
		if(pos<0)
			pos += len+1;

		if(vis[pos] == 1)
			break;
		vis[pos] = 1;
	}
	for(int i=0;i<=len;i++)
		if(!vis[i])
			return 0;
	return 1;
}

int main(){
    freopen("runround.in","r",stdin);
    freopen("runround.out","w",stdout);	
    cin>>M;
    for(int k = M+1;;k++){
    	memset(vis,0,sizeof(vis));
    	memset(digits,0,sizeof(digits));
    	if(ok(k)){
    		cout<<k<<endl;
    		break;    		
    	}

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值