POJ - 2183 Bovine Math Geniuses
“模拟“题,运用哈希,不断地按照一定运算规律对一个结果进行计算,如果重复出现就停止并且输出该数。注意到仔细看题,这种题一定要细心!
POJ - 2183 Bovine Math Geniuses
Time Limit: 1000MS |
Memory Limit: 65536KB |
64bit IO Format: %I64d & %I64u |
Description
Farmer John loves to help the cows further their mathematical skills. He has promised them Hay-flavored ice cream if they can solve various mathematical problems.
He said to Bessie, "Choose a six digit integer, and tell me what it is. Then extract the middle four digits. Square them and discard digits at the top until you have another number six digits or shorter. Tell me the result."
Bessie, a mathematical genius in disguise, chose the six digit number 655554. "Moo: 6 5 5 5 5 4", she said. She then extracted the middle four digits: 5555 and squared them: 30858025. She kept only the bottom six digits: 858025. "Moo: 8 5 8 0 2 5", she replied to FJ.
FJ nodded wisely, acknowledging Bessie's prowess in arithmetic. "Now keep doing that until you encounter a number that repeats a number already seen," he requested.
Bessie decided she'd better create a table to keep everything straight:
Middle Middle Shrunk to
Num 4 digits square 6 or fewer
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 --+
Bessie showed her table to FJ who smiled and produced a big dish of delicious hay ice cream. "That's right, Bessie," he praised. "The chain repeats in a loop of four numbers, of which the first encountered was 217600. The loop was detected after nine iterations."
Help the other cows win ice cream treats. Given a six digit number, calculate the total number of iterations to detect a loop, the first looping number encountered, and also the length of the loop.
FJ wondered if Bessie knew all the tricks. He had made a table to help her, but she never asked:
Middle Middle Shrunk to
Num 4 digits square 6 or fewer
200023 0002 4 4
4 0 0 0
0 0 0 0 [a self-loop]
whose results would be: three iterations to detect a loop, looping on 0, and a length of loop equal to 1.
Remember: Your program can use no more than 16MB of memory.
Input
* Line 1: A single six digit integer that is the start of the sequence testing.
Output
* Line 1: Three space-separated integers: the first number of a loop, the length of the loop, and the minimum number of iterations to detect the loop.
Sample Input
655554
Sample Output
217600 4 9
Source
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h> int hash[];
char str[];
int main()
{
int n = , t = /*, len = 0*/, cnt = ;
while(~scanf("%d", &n)) {
memset(hash, , sizeof(hash));
t = n;
cnt = ;
while(true) {
if(hash[t]) {
printf("%d %d %d\n", t, cnt - hash[t] + , cnt);
break;
}
cnt++;
hash[t] = cnt;
t /= ;
t %= ;
t = t * t;
t %= ; }
}
return ;
}
POJ - 2183 Bovine Math Geniuses的更多相关文章
- POJ 3047 Bovine Birthday 日期定周求 泽勒公式
标题来源:POJ 3047 Bovine Birthday 意甲冠军:.. . 思考:式 适合于1582年(中国明朝万历十年)10月15日之后的情形 公式 w = y + y/4 + c/4 - 2* ...
- POJ 2389 Bull Math(水~Java -大数相乘)
题目链接:http://poj.org/problem?id=2389 题目大意: 大数相乘. 解题思路: java BigInteger类解决 o.0 AC Code: import java.ma ...
- poj 2389.Bull Math 解题报告
题目链接:http://poj.org/problem?id=2389 题目意思:就是大整数乘法. 题目中说每个整数不超过 40 位,是错的!!!要开大点,这里我开到100. 其实大整数乘法还是第一次 ...
- 数据结构——POJ 1686 Lazy Math Instructor 栈的应用
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...
- poj 1684 Lazy Math Instructor(字符串)
题目链接:http://poj.org/problem?id=1686 思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit. 代码如下: #includ ...
- POJ 1686 Lazy Math Instructor(栈)
原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...
- POJ 2183
模拟题 #include <iostream> #include <cstdio> #include <algorithm> using namespace std ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
随机推荐
- 关于Repeater嵌套绑定的问题
前台代码: <div id="firstpane" class="menu_list"> <asp:Repeat ...
- CSS3+HTML5实现块阴影与文字阴影
CSS 3 + HTML 5 是未来的 Web,它们都还没有正式到来,虽然不少浏览器已经开始对它们提供部分支持.本教程分5节介绍了 5 个 CSS3 技巧,可以帮你实现未来的 Web,不过,这些技术不 ...
- [LintCode] Happy Number 快乐数
Write an algorithm to determine if a number is happy. A happy number is a number defined by the foll ...
- mysql双主复制总结
双主复制: 1).在两台服务器上各自建立一个具有复制权限的用户: 2).修改配置文件: # 主服务器A上 [mysqld] server-id = 10 log-bin = mysql-bin rel ...
- spring security 判断用户是否登录 只要登录就可以访问资源
有些情况,只要用户登录就可以访问某些资源,而不需要具体要求用户拥有哪些权限,这时候可以使用IS_AUTHENTICATED_FULLY,配置如下所示: <http auto-config='tr ...
- javascript通过时区获取时间
/* 描述:时区的换算 参数:offset时区位置 使用:东八区calcTime(”+8"); */ function calcTime(offset) { // 创建一个本地日期 var ...
- html只允许输入的数据校验,只允许输入字母汉字数字等
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- javascript保留关键字
1.通用保留关键字 break delete function return typeof case do if switch var catch else in this void continue ...
- nyoj-71
描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客的总重量不能超过独木舟的最大承载量.我们要尽量减少这次活动中的花销,所以要找出可以安置所有旅 ...
- ready是先执行的,load后执行,DOM文档的加载步骤
在jq中在文档载入完毕后有这几种方式去执行指定函数: $(document).ready(function() { // ...代码... }); //document ready 简写 $(func ...