大数运算

  C++ 中一直错用cstring,今天终于出错了,改回string 才好了

Round and Round We Go

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10549 Accepted: 4830

 

Description

A cyclic number is an integer n digits in length which, when multiplied by any integer from 1 to n, yields a"cycle"of the digits of the original number. That is, if you consider the number after the last digit to "wrap around"back to the first digit, the sequence of digits in both numbers will be the same, though they may start at different positions.For example, the number 142857 is cyclic, as illustrated by the following 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 not numbers are cyclic. The input file is a list of integers from 2 to 60 digits in length. (Note that preceding zeros should not be removed, they are considered part of the number and count in determining n. Thus, "01"is a two-digit number, distinct from "1" which is a one-digit number.)

Output

For each input integer, write a line in the output indicating 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

代码如下
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;

struct Node
{
int a[80];
int digit;
}num1,num2;
int sign1[80];

bool compare(int k,int sum  )        //判断是否为一个颠倒顺序的数组
{
int i,j;
for(i=k,j=0;i<sum;i++,j++)
if(num1.a[i]!=num2.a[j])
return false;

for(i=0;i<k;i++,j++)
if(num1.a[i]!=num2.a[j])
return false;

return true;
}

int main()
{
int i,j,k;
int temp;
bool ans;
string str;
//while(scanf("%s",str)!=EOF)
while(cin>>str)                //改为cstring时编译出错
{
num1.digit=str.length();
for(i=0,j=num1.digit-1;i<num1.digit;i++,j--)
{
num1.a[i]=str[j]-'0';
}
for(i=2;i<=num1.digit;i++)
{
int carry=0;
ans=false;
memset(sign1,0,sizeof(sign1));
for(j=0;j<num1.digit;j++)
{
temp=num1.a[j]*i+carry;
carry=temp/10;
num2.a[j]=temp%10;
}
if(carry)
//{printf("%s is not cyclic\n",str);break;}
{cout<<str<<" is not cyclic"<<endl; break;}
else
{
for(k=0;k<num1.digit;k++)
if(num1.a[k]==num2.a[0]&&sign1[k]==0)
{
sign1[k]=1;
ans=compare(k,num1.digit);
if(ans)
break;    //防止数组里有相同的数而再判断为false
}
if(!ans)
//{printf("%s is not cyclic\n",str);break;;}
{cout<<str<<" is not cyclic"<<endl; break;}
}//end else

}//end for (31)
if(ans)
//printf("%s is cyclic\n",str);
cout<<str<<" is cyclic"<<endl;

}//end while
return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值