UVA 11549 Calculator Conundrum (Floyd判圈算法)
题意:有个老式计算器,每次只能记住一个数字的前n位。现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少。例如,n=1,k=6,那么一次显示:6,3,9,1...
思路:这个题一定会出现循环,所以一个个模拟,遇到相同的就再之前所有数中找最大的输出即可。
怎么判断遇到相同的呢?如果装在数组里一一比较显然很慢,如果用v[k]判断k是否出现过,k范围太大空间不够用。
第一种方法是使用STL里的set来判重。
#include<cstdio>
#include<set>
using namespace std;
int T,n,k;
int next(int x)//寻找x的后继
{
if(!k) return 0;
int buf[20],i=0;
long long t=(long long)x*x;
while(t)
{
buf[i++]=t%10;
t/=10;
}
int ans=0;
for(int j=1;j<=min(n,i);++j) ans=ans*10+buf[i-j];
return ans;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
set<int> S;
while(!S.count(k))
{
S.insert(k);
k=next(k);
}
printf("%d\n",*(--S.end()));
}
return 0;
}
这个方法还是不够快的,第二种方法是用神奇的Floyd判圈算法。
如果两个小孩在直线跑道上跑同时出发,第二个小孩的速度是第一个小孩的两倍,那第二个小孩永远在前面。但如果在环形跑道上的话,第二个小孩将会“追上”第一个小孩。
#include<cstdio>
#include<set>
using namespace std;
int T,n,k;
int next(int x)
{
if(!k) return 0;
int buf[20],i=0;
long long t=(long long)x*x;
while(t)
{
buf[i++]=t%10;
t/=10;
}
int ans=0;
for(int j=1;j<=min(n,i);++j) ans=ans*10+buf[i-j];
return ans;
}
int main()
{
freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
int ans=max(k,next(k)),k1=k,k2=next(k);//k1是第一个小孩,k2是第二个小孩
while(k1!=k2)
{
k1=next(k1);
k2=next(k2); if(k2>ans) ans=k2; //小孩跑第一次
k2=next(k2); if(k2>ans) ans=k2; //小孩跑第二次
}
printf("%d\n",ans);
}
return 0;
}
UVA 11549 Calculator Conundrum (Floyd判圈算法)的更多相关文章
- Floyd判圈算法 UVA 11549 - Calculator Conundrum
题意:给定一个数k,每次计算k的平方,然后截取最高的n位,然后不断重复这两个步骤,问这样可以得到的最大的数是多少? Floyd判圈算法:这个算法用在循环问题中,例如这个题目中,在不断重复中,一定有一个 ...
- UVA 11549 CALCULATOR CONUNDRUM(Floyd判圈算法)
CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She was bore ...
- UVa 11549 计算器谜题(Floyd判圈算法)
https://vjudge.net/problem/UVA-11549 题意: 有一个老式计算器,只能显示n位数字,输入一个整数k,然后反复平方,如果溢出的话,计算器会显示结果的最高n位.如果一直这 ...
- SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...
- leetcode202(Floyd判圈算法(龟兔赛跑算法))
Write an algorithm to determine if a number is "happy". 写出一个算法确定一个数是不是快乐数. A happy number ...
- Floyd判圈算法
Floyd判圈算法 leetcode 上 编号为202 的happy number 问题,有点意思.happy number 的定义为: A happy number is a number defi ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Floyd 判圈算法
Floyd 判圈算法 摘自维基百科, LeetCode 上 141题 Linked List Cycle 用到这个, 觉得很有意思. 记录一下. 链接: https://zh.wikipedia.or ...
- Floyd判圈算法 Floyd Cycle Detection Algorithm
2018-01-13 20:55:56 Floyd判圈算法(Floyd Cycle Detection Algorithm),又称龟兔赛跑算法(Tortoise and Hare Algorithm) ...
随机推荐
- php的页面缓存练习
<?php /* * 自定义页面缓存类 */ namespace page_cache; class Page { public $CacheRoot = "pageCache/&qu ...
- 软件测试学习日志————round 1 some questions of two small programs
Below are four faulty programs. Each includes a test case that results in failure. Answer the follow ...
- IP校验和
#include <stdio.h> #include <unistd.h> #include <linux/if_ether.h> #include <li ...
- JS获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...
- 让vs2010的html编辑器验证html5语法
或者在Tools -> option -> Text Editor -> Html -> Validation
- vim note
2016-1-22 vim plugin collections: (参考 https://www.youtube.com/watch?v=0QFR-_wUoA0) vim-pathogen 插件管 ...
- 转 批处理 %~dp0的意义
http://nealcai.iteye.com/blog/1685192 http://blog.csdn.net/caz28/article/details/7448677 http://stac ...
- RedHat Enterprise Linux 6.3 安装Oracle Database 11g
按照以下文章正确将oracle安装在linux上 http://yiyiboy2010.iteye.com/blog/1670795 http://mirrors.163.com/centos/6.5 ...
- Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)
Pat1043代码 题目描写叙述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the f ...
- 一步一步挖出Compute
前几天在做结账的时候,对数据表DataGridView控件的单列求和纠结了一番. 如今差点儿养成了习惯,对于一些东西疏于開始的思考,不会先想到百度,这里我是先想到了第一版的机房收费那块的 ...