poj-2183
来源:互联网 发布:电影制作软件下载 编辑:程序博客网 时间:2024/06/02 06:44
一道简单的模拟题,
给一个6位数,
1.取中间四个数,
2.平方这个四位数,
3.拼成6位数,(如果超过6位数,则去掉高位, 如果不够6位数, 前位补0)
重复1~3, 直到出现6位数与之前的重复。求出这个数, 循环的次数, 总共的步数。。
例子:
655554 5555 30858025 858025 858025 5802 33663204 663204 663204 6320 39942400 942400 942400 4240 17977600 977600 977600 7760 60217600 217600 <-+ 217600 1760 3097600 97600 | 97600 9760 95257600 257600 | 257600 5760 33177600 177600 | 177600 7760 60217600 217600 --+上面例子输出,
217600 4 9
保存出现的值,然后就循环吧,
#include <stdio.h>#include <string.h>#define SIXBIT 100000#define MAX 999999int findPos[MAX + 1];// 6位数int loopData(int data){// 取中间四位data = (data - (data / SIXBIT) * SIXBIT)/ 10;data = data * data;// 取6位。int val = 0;int i = 0;int index = 1;for(; i < 6; i ++){val = val + (data % 10 ) * index;data = data/ 10;index = index * 10;}return val;}int main(){int n;while(scanf("%d", &n) != EOF){memset(findPos, 0, sizeof(findPos));int times = 0;do {n = loopData(n);if(findPos[n] != 0){printf("%d %d %d\n", n, times - findPos[n] + 1, times + 1);break;}findPos[n] = ++times;} while (true);}}
0 0
- POJ 2183
- POJ 2183
- poj-2183
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 《30天》的汇编实现(二)
- android-- 一个FlowLayout带你学会自定义ViewGroup
- Valid Palindrome 合法的回文
- html中文乱码
- 头文件互相包含与error C2146: 语法错误 : 缺少“;”的问题
- poj-2183
- 第十四周上机实践项目3:数组类模板
- Ubuntu系统 + JDK1.8 + SVN (离线安装)
- leetcode题解日练--2016.6.17
- Cordova+ionic+angular开发app
- 如何编写驱动程序
- Java学习:static 静态变量
- 把字符串转换成整数
- leetcode 125 Valid Palindrome