E - Round and Round We Go

本文介绍了一种用于判断特定整数是否为循环数的算法,并提供了一个C++实现示例。循环数是指当该数乘以从1到其长度的任意整数时,所得结果为原数数字序列的一个循环排列。

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

Subject

A cyclic number is an integer n digits in length which,when multiplied by any integer from 1 to n, yields a"cycle"of thedigits of the original number. That is, if you consider the number after thelast digit to "wrap around"back to the first digit, the sequence ofdigits in both numbers will be the same, though they may start at differentpositions.For example, the number 142857 is cyclic, as illustrated by thefollowing table: 
142857 *1 = 142857 
142857 *2 = 285714 
142857 *3 = 428571 
142857 *4 = 571428 
142857 *5 = 714285 
142857 *6 = 857142 

------------------------------------------------------------------------------------------------------------------------------------


Input

Write a program which will determine whether or notnumbers are cyclic. The input file is a list of integers from 2 to 60 digits inlength. (Note that preceding zeros should not be removed, they are consideredpart of the number and count in determining n. Thus, "01"is atwo-digit number, distinct from "1" which is a one-digit number.)


------------------------------------------------------------------------------------------------------------------------------------

Output

For each input integer, write a line in the outputindicating whether or not it is cyclic.


------------------------------------------------------------------------------------------------------------------------------------

Sample Input

142857

142856

142858

01

0588235294117647


------------------------------------------------------------------------------------------------------------------------------------

Sample Output

142857 is cyclic

142856 is not cyclic

142858 is not cyclic

01 is not cyclic

0588235294117647 is cyclic

 

------------------------------------------------------------------------------------------------------------------------------------

                   注意一下,乘积的第一位数,是不是大于10,就可以了。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

int main()
{
   char a[100];
   int b[100];
   int num[100],num1[100];
   int i,j,len,k,r;
   while(gets(a)!=NULL)
   {
   	  memset(num1,0,sizeof(num1));
      len =strlen(a);
      for(int i=0;i<len;i++)
      {
      	  num1[a[i]-'0']++;
      }
	  for( i=0;i<len;i++)
	  {
	  	memset(num,0,sizeof(num));
	  	//赋值 
	  	for( k=0;k<len;k++)
	      b[k] = a[k]-'0';
	    //乘积 
	  	 for( j=0;j<len;j++)
	  	 	b[j] = b[j]*(i+1);
	  	 //改变 
	  	 for( j=len-1;j>0;j--)
	  	 {
	  	 	if(b[j]>=10){
	  	 		b[j-1]+=b[j]/10;
	  	 		b[j]=b[j]%10;
	  	 	}
	  	 }
	  	 //比较 
	  	 if(b[0]>=10){
	  	 	num[b[0]/10]++;
	  	 	num[b[0]%10]++;
	  	 	j=1;
	  	 }else{
	  	 	j=0;
	  	 }
	  	 for( ;j<len;j++)
	  	 {
	  	     num[b[j]]++;
	  	 }
	  	
	  	 for(j=0;j<10;j++)
	  	 {
	  	 	if(num1[j]!=num[j]){
			   	break;
	  	 	}
	  	 }
	  	 if(j!=10){
	  	   printf("%s is not cyclic\n",a);
	  	   break;
	      }
	  }
	  if(i>=len){
	  	printf("%s is cyclic\n",a);
	  }
   }   
   return 0;	
}
------------------------------------------------------------------------------------------------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值