USACO 2.2 Runaround Numbers

#include <stdio.h>
#define DEBUG 1
#define TESTCASES 9

unsigned long int num;
int visited[10];
int digitArray[20];
int size;

int main(){
#if DEBUG
	int testCase;
	for (testCase = 1; testCase <= TESTCASES; testCase++){
		char inputFileName[20] = "inputx.txt";
		inputFileName[5] = '1' +  (testCase - 1);
		freopen(inputFileName, "r", stdin);
		printf("\n#%d\n", testCase);
#endif

	scanf("%d", &num);

	while (1){
		//递增num然后判断是否符合要求,其实也可以先按要求生成num再判断
		num++;

		int digit;
		for (digit = 1; digit <= 9; digit++)
			visited[digit] = 0;

		int tempNum = num;
		int temp[20];
		size = 0;
		int isRunaround = 1;
		while (tempNum){
			digit = tempNum % 10;
			if (visited[digit] == 1 || digit == 0){
				isRunaround = 0;
				break;
			}
			visited[digit] = 1;
			temp[size++] = digit;
			tempNum /= 10;
		}

		if (isRunaround){
			int digitIndex;
			for (digitIndex = 0; digitIndex < size; digitIndex++)
				digitArray[digitIndex] = temp[size - 1 - digitIndex];

			digitIndex = 0;
			while (digitArray[digitIndex] != 0){
				digit = digitArray[digitIndex];
				digitArray[digitIndex] = 0;
				digitIndex = (digitIndex + digit) % size;	
			}

			if (digitIndex != 0)
				continue;
			
			for (digitIndex = 0; digitIndex < size; digitIndex++)
				if (digitArray[digitIndex] != 0){
					isRunaround = 0;
					break;
				}
					
			if (isRunaround){
				printf("%ld\n", num);
				break;	
			}
		}
	}

#if DEBUG
	}
#endif
	return 0;
}

你可能感兴趣的:(USACO,Numbers,2.1,Runaround)