您的位置:首页 > 编程语言 > Go语言

hdu1313 Round and Round We Go (大数乘法)

2016-02-21 21:37 393 查看
Problem Description

A cyclic number is an integer n digits in length which, when multiplied by any integer from 1 to n, yields a ~{!0~}cycle~{!1~} of the digits of the original number. That is, if you consider the number after the last digit to ~{!0~}wrap around~{!1~} 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

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, ~{!0~}01~{!1~} is a two-digit number, distinct from ~{!0~}1~{!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

 
题意:给你一个字符串,让你判断它是不是一个“循环串”,循环串的定义是这个字符串所对应的整数乘上1~n(它的长度)的任何一个数,所得的结果为这个字符串循环后所得的整数。
思路:因为长度最多只有60,所以直接模拟就行了,附上大数乘法模板。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define eps 1e-15
#define maxn 200
#define Len 3000//大数的长度
using namespace std;
int len;
char a[Len],b[Len],c[Len];
void Mul(char a[],char b[],char c[])//大数乘法
{
int i,j,alen,blen,clen;
for(i=0; i<Len;i++){
c[i]='0';
}
c[Len]='\0';
alen=strlen(a);
blen=strlen(b);
reverse(a,a+alen);
reverse(b,b+blen);
int sum=0;
for(i=0; i<alen; i++){
for(j=0; j<blen; j++){
sum+=c[i+j]-'0'+(a[i]-'0')*(b[j]-'0');
c[i+j]=(sum%10)+'0';
sum/=10;
}
while(sum){
c[i+j++]+=sum%10;
sum/=10;
}
}
clen=len;
c[clen]='\0';
reverse(c,c+clen);

}
char str[700],str1[700],str2[700];
struct node{
char s[70];
}d[70];
bool cmp(node a,node b){
return strcmp(a.s,b.s)<0;
}

int main()
{
int n,m,i,j,tot,alen,blen;
while(scanf("%s",str1)!=EOF)
{
len=strlen(str1);
for(i=0;i<len;i++){
str[i]=str1[i];
str[i+len]=str1[i];
}
str[2*len]='\0';
for(i=1;i<=len;i++){
tot=i-1;
for(j=0;j<len;j++){
d[i].s[j]=str[tot];tot++;
}
d[i].s[len]='\0';
}
sort(d+1,d+1+len,cmp);

int flag=1;
for(i=2;i<=len;i++){
alen=len;
for(j=0;j<len;j++){
a[j]=str[j];
}
a[alen]='\0';

int tt=i;
blen=0;
while(tt){
b[blen++]=tt%10+'0';
tt/=10;
}
b[blen]='\0';
reverse(b,b+blen);
Mul(a,b,c);
if(strcmp(c,d[i].s)!=0){
flag=0;
}
}
if(flag)printf("%s is cyclic\n",str1);
else printf("%s is not cyclic\n",str1);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: