USACO Runaround Numbers

runaround numbers 肯定不包含0,数字不重复

两个技巧
把一个int 型整数按数字存入 char 数组, 然后将char 数组 改变为int 数组——sprintf函数

	sprintf(numstr, "%d", n);
	size = strlen(numstr);
	for(i = 0; i < size; i++) {
		numdig[i] = numstr[i] - 48;
	}

检验数字是否run around 时,注意每次检验开始的点index
	//whether Runaround
	int times = size;
	int index = 0;
	while(times) {
		index = (index + numdig[index]) % size;
		visited[index] = true;
		times--;
	}
	for(j = 0; j < size; j++) {
		if(!visited[j]) return false;
	}

我的代码
/*
ID: wangxin12
PROG: runround
LANG: C++
*/

#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>
#include <stack>

using namespace std;

int N;

bool isRunaround(int n);

int main() {
	
	//input
	ifstream fin("runround.in");
	fin>>N;
	fin.close();
	
	//
	int n = N + 1;
	while(!isRunaround(n)) {
		n++;
	}

	//bool n = isRunaround(147);

	//Output
	ofstream fout("runround.out");
	fout<<n<<endl;
	fout.close();

	return 0;
}

bool isRunaround(int n) {
	char numstr[10];
	int numdig[10];
	bool visited[10] = { false };
	int size, i, j;

	sprintf(numstr, "%d", n);
	size = strlen(numstr);
	for(i = 0; i < size; i++) {
		numdig[i] = numstr[i] - 48;
	}

	//有重复数字不算
	for(i = 0; i < size - 1; i++) {
		for(j = i + 1; j < size; j++) {
			if(numstr[i] == '0' || numstr[j] == '0' || numstr[i] == numstr[j])  return false;
		}
	}

	//whether Runaround
	int times = size;
	int index = 0;
	while(times) {
		index = (index + numdig[index]) % size;
		visited[index] = true;
		times--;
	}
	for(j = 0; j < size; j++) {
		if(!visited[j]) return false;
	}

	return true;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值