码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1313 高精度*单精度

时间:2014-08-15 01:24:36      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   java   os   io   strong   

Round and Round We Go

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 658    Accepted Submission(s): 365


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
 
 
 
 
题意:
给出一个数(长度<=60),然后这个数依次乘1--n(n为该数的长度),若每次乘后所得的数首尾相连构成的环和给的那个数构成的环相等,则该数是环。
 
 
思路:
高精度模拟一下就行了。
 
代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 char s[100];
 9 char str[200];
10 
11 int solve(){
12     int a[100];
13     char s1[100];
14     int i, j, n=strlen(s);
15     for(i=2;i<=n;i++){
16         memset(s1,\0,sizeof(s1));
17         memset(a,0,sizeof(a));
18         for(j=n-1;j>=0;j--) a[j]=s[n-j-1]-0;
19         for(j=0;j<n;j++) a[j]*=i;
20         for(i=0;i<=n+2;i++){
21             a[i+1]=a[i+1]+a[i]/10;
22             a[i]%=10;
23         }
24         int k=n+2;
25         while(!a[k]) k--;
26         for(j=n-1;j>=0;j--) s1[j]=a[n-j-1]+0;
27         if(!strstr(str,s1)) return 0;
28     }
29     return 1;
30 }
31 
32 main()
33 {
34     while(scanf("%s",s)!=EOF){
35         strcpy(str,s);
36         strcat(str,s);
37         int ans=solve();
38         if(ans) printf("%s is cyclic\n",s);
39         else printf("%s is not cyclic\n",s);
40     }
41 }

 

 
 
 
 

HDU 1313 高精度*单精度,布布扣,bubuko.com

HDU 1313 高精度*单精度

标签:des   style   blog   color   java   os   io   strong   

原文地址:http://www.cnblogs.com/qq1012662902/p/3913759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!