循环数的判断(60位以内)

循环数是一个整数,满足乘连续的若干个数后各位发生循环。最广为人知的循环数是142857.其循环如下:

142857 × 1 = 142857

142857 × 2 = 285714

142857 × 3 = 428571

142857 × 4 = 571428

142857 × 5 = 714285

142857 × 6 = 857142

因为时间原因代码没有优化之类的委屈。以后有时间我会把优化后的放上来的

#include<iostream>
#include<string>
using namespace std;
const int size = 60;

int main()
{
	
	string str;
	while (cin >> str)
	{
		int num[size] = { 0 }, num_temp[size * 2] = { 0 };
		int str_length = str.size();
		int locate = size - str_length;

		for (int f = 0; f < str_length; f++)
		{

			num[size - 1 - f] = str[str_length - 1 - f] - '0';
			num_temp[str_length - 1 - f] = str[str_length - 1 - f] - '0';
			num_temp[2 * str_length - 1 - f] = str[str_length - 1 - f] - '0';
		}
	
		int count = 1;
		for (int i = 2; i <= str_length; i++)
		{
			
			int lnum[size] = { 0 };
			for (int j = 0; j < str_length; j++)
			{
				lnum[size - 1 - j] += num[size - 1 - j] * i;
				if (lnum[size - 1 - j]>9)
				{
					lnum[size - 2 - j] += lnum[size - 1 - j] / 10;
					lnum[size - 1 - j] %= 10;
				}
			}    

			int k = 0,count_k=0;int count_l = 0;
			for (; k < str_length; k++)
			{
				if (lnum[locate] == num_temp[k])
				{
					int l = 0; 
					for (; l < str_length; l++)
					{
						if (lnum[locate + l] != num_temp[k + l])break;
					}
					if (l == str_length)
					{
						count_l++; break;
					}
				}
					
			}
			if (count_l == 1)
			{
				count++; continue;
			}
		}

		if (count == str_length)
		{
			cout << str << " is cyclic" << endl;
		}
		else
		{
			cout << str << " is not cyclic" << endl;
		}
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值