USACO Section 2.2 Runaround Numbers

本文探讨了一个使用C++实现的独特算法,旨在识别并输出唯一数字序列中的数值,该序列由连续递增的整数组成,且每个数字在序列中仅出现一次。通过读取输入文件并验证条件,最终将符合条件的唯一序列输出至输出文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
ID: lucien23
PROG: runround
LANG: C++
*/

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int main()
{
	ifstream infile("runround.in");
	ofstream outfile("runround.out");
	if(!infile || !outfile)
	{
		cout << "file operation failure!" << endl;
		return -1;
	}

	unsigned long M;
	infile >> M;
	int numCnt[9];
	while (1) 
	{
		M++;

		char strNum[10];
		sprintf(strNum, "%ld", M);
		int len = strlen(strNum);
		//验证M中无0
		if (strchr(strNum, '0') != NULL)
		{
			continue;
		}
		//验证M中所有的数字都不一样
		int *flags = new int[len]();
		int i;
		for (i=0; i<9; i++)
		{
			numCnt[i] = 0;
		}
		for (i=0; i<len; i++)
		{
			numCnt[strNum[i]-'0'-1]++;
		}
		for (i=0; i<9; i++)
		{
			if (numCnt[i] >= 2)
				break;
		}
		if (i < 9)
		{
			continue;
		}
		//验证数字规律
		int pos = 0;
		i = 0;
		while (i++ < len)
		{
			flags[pos] = 1;
			int count = strNum[pos] - '0';
			pos = (count + pos) % len;
		}

		for (i=0; i<len; i++)
		{
			if (flags[i] == 0)
			{
				break;
			}
		}
		delete[] flags;
		//判断规律是否符合
		if (i == len && pos == 0)
		{
			outfile << M <<endl;
			break;
		}
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值