【解题报告】poj1047_Round and Round We Go(循环数)
来源:互联网 发布:mac gcc 不安装xcode 编辑:程序博客网 时间:2024/05/19 12:41
Description
142857 *1 = 142857
142857 *2 = 285714
142857 *3 = 428571
142857 *4 = 571428
142857 *5 = 714285
142857 *6 = 857142
Input
Output
Sample Input
142857142856142858010588235294117647
Sample Output
142857 is cyclic142856 is not cyclic142858 is not cyclic01 is not cyclic0588235294117647 is cyclic
Source
思路:
证明:
1/11 = 0.09090909..
1/13 = 0.076923 076923 .....
规律为,当分母为素数时,所得结果都为循环小数。
以第一个式子 1/7 = 0.142857 142857 ..... 为例,因为后面小数的循环周期为6,所以不妨把它两边同时乘以 10^6 ,所得新式子为: 10^6/7 = 142857.142857 142857 ....
又因为 1/7 = 0.142857 142857 ..... 所以在 10^6/7 = 142857.142857 142857 .... 的基础上给分子减去1,就舍去了小数点后面的数值: 10^6/7 - 1/7 = 142857
化简得:(10^6-1)/7 = 142857 于是得到: 99999/7 = 142857 所以:142857 * 7 = 99999 成立。
因此可推出判断 n 是否有周期性的公式。
观察142857,我们用1至6的数来乘:
1´ 142857=142857
2´ 142857=285714
3´ 142857=428571
4´ 142857=571428
5´ 142857=714285
6´ 142857=857142
所得得结果是1、4、2、8、5、7六个数字依原来的次序循环排列只是开头的数字变动而已。这种数我们叫做循环数,其实这个数142857是由1/7所形成循环小数的循环节(1/7=0.142857142857142857…)。而所有循环数也都由某质数的倒数所形成之循环小数的循环节而得来的。下一个循环数是由质数17所形成的,1/17=0.0588235294117647…,而0588235294117647即为一循环数(2 0588235294117647=1176470588235294)。
会产生如此循环数的质数依序为7、17、19、23、29、47、59、61、97(<100)。
142857还有一个很有趣的性质。当142857乘以7时其乘积为一连串的9(142857´ 7=999999),而0588235294117647乘以17也是一连串的9。还有142857分成两半相加也是一连串的9(注:142+857=999),而0588235294117647分成两半相加: 05882352+ 94117647=99999999,这真是非常奇妙的巧合。
#include <cstdio>#include <cmath>#include <cstring>int main(){ //freopen("in.txt","r",stdin); char s[70]; while(~scanf("%s",s)){ int len=strlen(s); int num[70]; int i=0,flag=0,i_num=0,j; while(s[i]){ if(s[i]!='0') flag=1; if(flag) num[i_num++]=s[i]-'0'; i++; } for(i=0;i<(len/2);i++){//分成两半相加和为9为循环数 if(s[i]+s[i+len/2]-'0'*2!=9){ flag=0; break; } } if(!flag){ printf("%s is not cyclic\n",s); continue; } int res[70]={0},ii=0; for(i=i_num-1;i>=0;i--){ res[ii]=(res[ii]+num[i]*(len+1)); res[ii+1]+=res[ii]/10; if(res[ii]) res[ii]%=10; ii++; } while(res[ii]){ res[ii+1]+=res[ii]/10; res[ii]%=10; ii++; } if(ii==len){ flag=0; for(j=0;j<ii;j++) if(res[j]!=9){ flag=1; break; } if(flag) printf("%s is not cyclic\n",s); else printf("%s is cyclic\n",s); } else printf("%s is not cyclic\n",s); } return 0;}
- 【解题报告】poj1047_Round and Round We Go(循环数)
- POJ 1047 Round and Round We Go 循环数新解
- TOJ 1563 Round and Round We Go (弃9数)
- Round and Round We Go
- Round and Round we go
- Round and Round We Go
- OpenJudge百炼-2952-循环数 & poj-1047-Round and Round We Go-C语言-高精度计算
- ZOJ Problem Set - 1073 Round and Round We Go()
- (高精度) Round and Round We Go (1047)
- ZOJ Problem Set - 1073(Round and Round We Go)
- HDU 1313 Round and Round We Go (Java大数)
- 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
- <poj1047>Round and Round We Go
- java与json互相转换(解决日期问题)
- struts2+spring+hibernate整合(示例)
- 软件工程——面向过程的软件设计方法
- gcc and g++
- Android图片异步加载
- 【解题报告】poj1047_Round and Round We Go(循环数)
- Linux下安装Apache(Ubuntu)
- oracle错误代码
- Python模块概念和基本使用
- 设计模式学习笔记——命令模式
- Android之Service服务
- 具备互联网思维必读10本书
- LeetCode Add Binary
- 伟大的旅程已经起航