原文请訪问我的博客:http://xiaoshig.sinaapp.com/?p=115

Alice got a hold of an old calculator that can display n digits. She was bored enough to come up with the following time waster.

She enters a number k then repeatedly squares it until the result overflows. When the result overflows, only the most significant digits are displayed on the screen and an error flag appears. Alice can clear the error and continue squaring the displayed number. She got bored by this soon enough, but wondered:

“Given n and k, what is the largest number I can get by wasting time in this manner?

Program Input

The first line of the input contains an integer (1 ≤ ≤ 200), the number of test cases. Each test case contains two integers (1 ≤ ≤ 9) and (0 ≤ < 10n) where n is the number of digits this calculator can display is the starting number.

Program Output

For each test case, print the maximum number that Alice can get by repeatedly squaring the starting number as described.

Sample Input & Output

INPUT

2
1 6
2 99

OUTPUT

9
99
#include<iostream>
#include<set>
#include<sstream>
using namespace std;
int next(int n,int k)
{stringstream ss;ss<<( long) k*k;string s=ss.str();if(s.length()>n)s=s.substr(0,n);int ans;stringstream ss2(s);ss2>>ans;return ans;
}
int main()
{int t;cin>>t;while(t--){int n,k;cin>>n>>k;set<int> s;int ans=k;while(!s.count(k)){s.insert(k);if(k>ans)ans=k;k=next(n,k);}cout<<ans<<endl;}
return 0;
}

转载于:https://www.cnblogs.com/zfyouxi/p/5229639.html

UVA 11549 Calculator Conundrum相关推荐

  1. UVa 11549 Calculator Conundrum

    大白书里面的题感觉就是没有什么固定的思路,只能认真理解学习汝佳大大的代码. 这里用的Floyd判圈法,就像插图里面的一样,两个小孩,一个快一个慢,如果实在一个环形跑道,那么快的那个最终一定会" ...

  2. UVA 11549 Calculator Conundrum

    很容看出来,一直平方又只取前面n位会构成循环,所以可以模拟,判重的话就用hash #include <iostream> #include <cstdio> #include ...

  3. UVa 11549 Calculator Conundrum (训练指南,Floyd 判圈算法)

    算法竞赛训练指南, 42 页 注意: 1. 用数组来记录一个数的各位的数字 2. 快慢指针来记录链表环的入点(Floyd 判圈算法) #include <cstdio> #include ...

  4. UVA 11549 Calculator Conundrum 题解

    题解: 题目暗示了计算机显示出的数将出现循环,最直接的方式是一个一个的模拟,并判断新得到的数字是否出现过.最简单的方式是通过数组记录已经出现过的数字,然而题目中 0<=k<=10^9 范围 ...

  5. UVa 11549 - Calculator Conundrum

    題目:計算k^2^m中,前n位構成的最大數字. 分析:數論,循環.找尋環節不斷的計算k^2^m和k^4^m相等時即為循環節. 說明:也可以使用map檢測循環╮(╯▽╰)╭. #include < ...

  6. UVA之11549 - Calculator Conundrum

    [题目] Problem C CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can displayndigits. S ...

  7. UVA 11549 calcular conundrum 计算机老谜题

    题目大意 用一个老式计算器,只显示n位数字,输入一个整数k后计算,计算器会反复平方,直至溢出,每次溢出只显示最高的n位,计算器会一直平方下去,直到出现重复的数字 分析 题目中已经暗示了计算器显示的数会 ...

  8. Calculator Conundrum UVA - 11549(floyd判圈)

    Calculator Conundrum UVA - 11549 题意: 给你一个n和k. 每次操作可以把k平方,之后取k*k的前n位 为 k. 思路: 首先可以想到,经过有限次操作后,会出现循环. ...

  9. UVA 11549 模拟 Floyed判圈法的应用 Calculator Conundrum

    此题很容易想到会出现环,那么就可以想到用map来判重,但是此题还有一种更加优越的所需空间复杂度为o(1)的算法 . #include<cstdio> #include<iostrea ...

最新文章

  1. python字典随机取数
  2. 题目1028:继续畅通工程
  3. 以列表形式输出_python格式化输出总结
  4. Repeater控件的分隔线
  5. 每日一题(27)—— define定义一个宏表明1年中有多少秒
  6. [洛谷P3391] 文艺平衡树 (Splay模板)
  7. 常见浏览器兼容性问题与解决方案(CSS)
  8. 最好看的css样式文字,二十款漂亮的CSS字体样式
  9. 短视频是如何赚钱的?切入短视频风口的3种玩法
  10. TabHost控件应用
  11. 【叨、校长】一个基于Extjs、Pushlet美轮美奂的Web聊天室
  12. 何为A站、B站、C站、D站、F站??
  13. P5208-[WC2019] I 君的商店【交互,二分】
  14. 推荐4个好用、接收邮件及时的临时邮箱
  15. FPGA实现UDP传输视频,提供2套verilog工程源码和接收显示上位机程序
  16. 作为开篇文章,我们聊聊“围城”
  17. 2023计算机毕业设计SSM最新选题之java健身俱乐部5jz0z
  18. 网站搜索引擎优化指南
  19. 数学建模:插值与拟合—插值问题的python求解
  20. BDD100K数据集简单解析以及格式转换成voc格式

热门文章

  1. Python实现八皇后问题
  2. [错误总结]升级spring-boot->2.6.2|hiberate->5.4.33.Final|spring cloud->2021.0.0 |spring admin->2.4.1
  3. plsql 查询存储过程死锁语句_ORACLE-Kill 杀死正在执行的Oracle存储过程和死锁语句...
  4. 编译原理 —— 1.2 编译系统的结构(终于弄懂语法和语义的区别了!)
  5. 逻辑回归、线性回归直观感受
  6. Harris角点检测原理详解(转载)
  7. 数据可视化系列(四):文字图例尽眉目
  8. 12563 - Jin Ge Jin Qu hao
  9. UML部署图和构件图
  10. Vmware 连接局域网通过桥接方式