[2016-03-19][UVA][11549][Calculator Conundrum]

  • 时间:2016-03-19 21:27:43 星期六

  • 题目编号:[2016-03-19][UVA][11549][Calculator Conundrum]

  • 题目大意:给定数k每次取前n位不断平方,求出现的最大值是多少

  • 方法:

    • 方法1:模拟一遍过程,直到出现循环
    • 方法2:Floyd判断算法,定义两个k,每次k1走一次,k2走两次,知道k1,k2相同

方法1:STL超级暴力方法

方法2:小小优化版

方法3:Floyd判圈算法

方法1:STL超级暴力方法

  1. #include <set>
  2. #include <sstream>
  3. #include <string>
  4. #include <cstdio>
  5. using namespace std;
  6. typedef long long LL;
  7. int t,n,k,ans;
  8. int mnxt(){
  9. stringstream ss;
  10. ss<<(LL) k * k;
  11. string str = ss.str();
  12. if(str.length() > n) str = str.substr(0,n);
  13. int num = 0;
  14. stringstream ss2(str);
  15. ss2>>num;
  16. return num;
  17. }
  18. int main(){
  19. scanf("%d",&t);
  20. while(t--){
  21. scanf("%d%d",&n,&k);
  22. set<int> s;
  23. ans = k;
  24. while(!s.count(k)){
  25. s.insert(k);
  26. if(k > ans) ans = k;
  27. k = mnxt();
  28. }
  29. printf("%d\n",ans);
  30. }
  31. return 0;
  32. }

方法2:小小优化版

  1. #include <cstdio>
  2. #include <set>
  3. #include<algorithm>
  4. using namespace std;
  5. typedef long long LL;
  6. #define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))
  7. int t,n,k,ans,buf[20];
  8. int mnxt(){
  9. if(!k) return 0;
  10. LL k2 = (LL)k*k;
  11. int cnt = 0;
  12. while (k2){
  13. buf[cnt++] = k2 % 10;
  14. k2 /= 10;
  15. }
  16. int tmpn = min(n,cnt);
  17. FOR(i,0,tmpn){
  18. k2 = k2 * 10 + buf[--cnt];
  19. }
  20. return k2;
  21. }
  22. int main(){
  23. scanf("%d",&t);
  24. while(t--){
  25. scanf("%d%d",&n,&k);
  26. set<int> s;
  27. ans = k;
  28. while(!s.count(k)){
  29. s.insert(k);
  30. if(k > ans) ans = k;
  31. k = mnxt();
  32. }
  33. printf("%d\n",ans);
  34. }
  35. return 0;
  36. }

方法3:Floyd判圈算法

  1. #include <cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. typedef long long LL;
  5. #define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))
  6. int t,n,ans,buf[20],k1,k2;
  7. int mnxt(int k){
  8. if(!k) return 0;
  9. LL k2 = (LL)k*k;
  10. int cnt = 0;
  11. while (k2){
  12. buf[cnt++] = k2 % 10;
  13. k2 /= 10;
  14. }
  15. int tmpn = min(n,cnt);
  16. FOR(i,0,tmpn){
  17. k2 = k2 * 10 + buf[--cnt];
  18. }
  19. return k2;
  20. }
  21. int main(){
  22. scanf("%d",&t);
  23. while(t--){
  24. scanf("%d%d",&n,&k1);
  25. k2 = ans = k1;
  26. do{
  27. k1 = mnxt(k1);
  28. k2 = mnxt(k2); if(k2 > ans) ans = k2;
  29. k2 = mnxt(k2); if(k2 > ans) ans = k2;
  30. }
  31. while(k1 != k2);
  32. printf("%d\n",ans);
  33. }
  34. return 0;
  35. }

来自为知笔记(Wiz)

时间: 2024-08-10 21:15:18

[2016-03-19][UVA][11549][Calculator Conundrum]的相关文章

[UVa 11549]Calculator Conundrum

题解 显然按题意模拟会出现环,因为可能出现的数字数有限的,所以不可能无限的衍生下去. 那么我们就可以按题意模拟,遍历整个过程,统计最大值即可. 判环的环我们想到$hash$,也可以用$STL$中的$set$,但是复杂度高... $Floyd$判圈.一步两步法,有环的话肯定会相遇,空间复杂度可以降到$O(1)$,时间也快不少. 1 //It is made by Awson on 2017.9.18 2 #include <map> 3 #include <set> 4 #inclu

UVA 11549 Calculator Conundrum Floyd判圈

题目链接:点击打开链接 题意: 输入n k,表示计算器能显示n位数字,初始有一个数字k 每次操作 k = k^2, 若超出n位则截取前n位. 求能获得的最大数字. 思路: 首先我们能判断这个操作一定存在循环. 那么如何终止循环,利用Floyd判圈法 让两个循环child1和child2刚开始都为k,然后child1每次变换一次,child2每次变换2次: 这样当child1再次等于child2时说明已经至少经过一个循环节了,因为child2已经从后面赶上child1了 import java.i

UVA之11549 - Calculator Conundrum

[题目] Problem C CALCULATOR CONUNDRUM 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

Murano Weekly Meeting 2016.07.19

Meeting time: 2016.July.19 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: 1.Backports Link:  https://etherpad.openstack.org/p/murano-stable-backports/ 2.Convergence so both of our CI servers are running heat with convergence n

2016.1.19 DEV Express控件GirdControl使用

DEV控件在工具箱中消失处理方法 开始-->程序-->Developer Express v2009 vol 3(依据版本不同)-->Components-->Tools-->ToolboxCreator   1.点击一行选择完整一行 Run Designer->View->OptionsBehavior->EditorShowMode 设置为:Click Run Designer->View->OptionsSelection.EnableAp

C - Calculator Conundrum

uva 11549 题意:你拥有一个老式计算机,它只能显示n为数字,有一天你输入数字k,接着一直平方下去,在这个过程中如果数字长度大于n,那么截取前n个数形成一个新的数k,再用这个新的数k一直平方下去,那么这个过程中能显示的最大数字是多少. 思路:在这个过程如果出现了以前出现过的数,那么从第一个开始到这个数就是一个循环节,后面的也与这个节一模一样,因为都是针对一个数字执行同一种操作. 为了时间复杂度上的考虑,可以使用set判重,耗时2.279s #include <iostream> #inc

分布式技术一周技术动态 2016.03.20

分布式系统实践 1. 基于Mesos和Docker的分布式计算平台 https://mp.weixin.qq.com/s?__biz=MzAxMDgzOTA2Mw==&mid=402769128&idx=1&sn=cea3ad1357bd9312acf1768c0a493bfd&scene=1&srcid=0318BTuxT0fsFYwPjpeyuDOa&key=710a5d99946419d90fbc1e7600cce055b6e997d6afafc74c

uva 11549

/**  * @brief uva 11549  * @file 11549.cpp  * @author mianma  * @created 2014/12/31 11:43  * @edited  2014/12/31 11:43  * @type   * @note floyd判圈算法  */ #include <fstream> #include <iostream> #include <cstring> #include <cmath> usin

翻译:Gregory Larsen,2016/02/19(第一版:2014年12月17日)高级T-SQL阶梯1级:使用CROSS JOIN介绍高级T-SQL

原文链接:http://www.sqlservercentral.com/articles/Stairway+Series/119933/ 原文作者:Gregory Larsen,2016/02/19(第一版:2014年12月17日) 系列 本文是"Stairway Series:Stairway to Advanced T-SQL"的一部分 这个阶梯将包含一系列文章,这些文章将在前面两个T-SQL阶梯,T-SQL DML和T-SQL超越基础知识的T-SQL基础上进行扩展. 这个楼梯应