<poj1047>Round and Round We Go
来源:互联网 发布:什么叫linux版本 编辑:程序博客网 时间:2024/06/11 01:55
Round and Round We Go
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10410 Accepted: 4766
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
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
142857142856142858010588235294117647
Sample Output
142857 is cyclic142856 is not cyclic142858 is not cyclic01 is not cyclic0588235294117647 is cyclic
Source
Greater New York 2001
大数乘法部分不是难点,本题的难点是怎么判断进行大数乘法之后得到的新数从某个digit开始读就是原数,我参考一个网友提供的方法,假设存储原数的字符数组为ori_str,储存进行大数阶乘之后的数的字符数组是num_str,两个ori_str连接成一个新的字符串exp_str,若原数是cyclic的则num_str是exp_str的sub-string。
判断sub-string可以使用c语言string.h的strstr(http://www.cplusplus.com/reference/clibrary/cstring/strstr/),也可以使用STL的string::find(http://www.cplusplus.com/reference/string/string/find/).本文使用前者。
AC Code:
#include <stdio.h>#include <string.h>int main(){ int num[60],MulNum[60],i,j,k,len;//num是原数,MulNum是进行乘法之后的数 char ori_str[61],num_str[61],exp_str[121];//ori_str是原数,num_str是进行乘法之后的数,exp_str由 //两个ori_str连接而成 int temp,carry; int flag; while(scanf("%s",ori_str)!=EOF) { flag=1; strcpy(exp_str,ori_str); len=strlen(ori_str); for(i=0;i<len;i++) { exp_str[len+i]=ori_str[i]; num[i]=ori_str[i]-'0'; } exp_str[len+i]='\0'; for(i=2;i<=len;i++) { carry=0; for(j=len-1;j>-1;j--) { MulNum[j]=num[j]*i+carry; if(MulNum[j]>9) { temp=MulNum[j]; MulNum[j]%=10; carry=temp/10; } else carry=0; } //如果carry不是0,则上面算出来的数位数多于原数,原数不是cyclic if(carry) { flag=0; break; } //判断是否cyclic else { for(j=0;j<len;j++) { num_str[j]=MulNum[j]+'0'; } num_str[len]='\0'; if(strstr(exp_str,num_str)==NULL) { flag=0; break; } } }//for if(flag) printf("%s is cyclic\n",ori_str); else printf("%s is not cyclic\n",ori_str); }//while return 0;}
- <poj1047>Round and Round We Go
- poj1047 Round and Round We Go
- POJ1047 Round and Round We Go【高精度】
- 高精度乘以单精度 Round and Round We Go poj1047
- UVALive2287 POJ1047 HDU1313 ZOJ1073 Round and Round We Go【大数+数学计算】
- Round and Round We Go
- Round and Round we go
- Round and Round We Go
- zoj1073 Round and Round We Go
- pku1047 Round and Round We Go
- zoj 1073 Round and Round We Go
- Round and Round We Go POJ 1047
- poj 1047 Round and Round We Go
- poj 1047 Round and Round We Go
- ZOJ 1073 Round and Round We Go
- POJ 1047 Round and Round We Go
- ZOJ 1073 Round and Round We Go
- zoj 1073 Round and Round We Go
- Perl 前缀树实现
- 周报生活,让生命价值在你眼前裸奔!
- 知识管理
- poj2826 An Easy Problem?!
- linux之epoll3
- <poj1047>Round and Round We Go
- 左脑原创geoserver学习系列文章 (第二部分代码的结构)。
- 提问与回答的智慧
- linux之epoll4
- ubuntu下安装mysql
- 【Win32多线程】如何避免线程等待浪费CPU时间,降低对系统资源的冲击?
- hadooo 源代码分析
- 常用控件 04 复选框 Checkboxes
- c/c++在线编译器