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 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

Calgary Collegiate Programming Contest 2008

【分析】

【思路一】

题目已经暗示计算器显示会的数字出现循环,所以不妨一个一个的模拟,每次判断新得到的数字是否以前出现过。如何判断呢?一种方法

就是把所有计算出的数字放在数组中,然后一个一个的比较。这种方法每次判断需要花费很长时间,相当慢,能否开一个数组visited,直接读取visited[k]

来判断整数k是否出现过,k(0 <= k < 10^9)的范围很大,需要开辟的空间很大,严重浪费资源。在这种情况下我们可以利用STL的集合set。

【思路二】

这个方法还是不够快的,第二种方法是用神奇的Floyd判圈算法。

假设两个小孩在一个可以无限向前跑的跑道上赛跑,同时出发,但其中一个小孩的速度是另一个小孩速度的两倍。如果跑道是直的,跑得快的小孩永远在前面,另一个小孩

永远都追不上。但如果跑道有环,则跑的快的小孩将”追上“跑的慢的小孩。

【代码】

/*********************************
*   日期:2014-5-2
*   作者:SJF0115
*   题号: 11549 - Calculator Conundrum
*   地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=show_problem&problem=2544
*   来源:UVA
*   结果:Accepted
*   总结:
**********************************/
#include <iostream>
#include <set>
#include <stdio.h>
using namespace std;

int bits[10];
//截取k平方的高n位
int HighNBits(int n,int k){
    if(!k){
        return 0;
    }
    long long s = (long long)k * k;
    int index = 0;
    //分离s的各位
    while(s > 0){
        bits[index++] = s % 10;
        s /= 10;
    }
    //不够n位
    if(n > index){
        n = index;
    }
    //高n位
    int result = 0;
    for(int i = 0;i < n;i++){
        result = result * 10 + bits[--index];
    }
    return result;
}

int main(){
    int T,i,j,n,k;
    scanf("%d",&T);
    //T组测试数据
    while(T--){
        scanf("%d %d",&n,&k);
        set<int> visited;
        int max = 0;
        //判断新得到的数值以前是否出现过
        while(visited.count(k) == 0){
            visited.insert(k);
            //更新最大值
            if(max < k){
                max = k;
            }
            k = HighNBits(n,k);
        }
        printf("%d\n",max);
    }
    return 0;
}

【代码2】

/*********************************
*   日期:2014-5-2
*   作者:SJF0115
*   题号: 11549 - Calculator Conundrum
*   地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=show_problem&problem=2544
*   来源:UVA
*   结果:Accepted
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
using namespace std;

int bits[10];
//截取k平方的高n位
int HighNBits(int n,int k){
    if(!k){
        return 0;
    }
    long long s = (long long)k * k;
    int index = 0;
    //分离s的各位
    while(s > 0){
        bits[index++] = s % 10;
        s /= 10;
    }
    //不够n位
    if(n > index){
        n = index;
    }
    //高n位
    int result = 0;
    for(int i = 0;i < n;i++){
        result = result * 10 + bits[--index];
    }
    return result;
}

int main(){
    int T,i,j,n,k;
    scanf("%d",&T);
    //T组测试数据
    while(T--){
        scanf("%d %d",&n,&k);
        int max = k;
        int k1 = k,k2 = k;
        do{
            //小孩1
            k1 = HighNBits(n,k1);
            //小孩2 第一步
            k2 = HighNBits(n,k2);
            if(k2 > max){
                max = k2;
            }
            //小孩2 第二步
            k2 = HighNBits(n,k2);
            if(k2 > max){
                max = k2;
            }
        }while(k1 != k2);//快的小孩追上慢的小孩停止
        printf("%d\n",max);
    }
    return 0;
}

时间: 2024-08-01 19:52:58

UVA之11549 - Calculator Conundrum的相关文章

uva 11549 - Calculator Conundrum

点击打开链接uva 11549 思路:模拟 分析: 1 题目要求找到最大的n位数,那么我们通过不断的模拟,求出最大的ans 2 这里有个问题就是我们怎么知道什么时候结束呢?我们知道如果当前数已经有出现了那么说明刚好一个循环,这里利用map映射 代码: #include<map> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using names

uva 11549 - Calculator Conundrum 模拟

      Floyd判圈算法,好厉害的样子       用sscanf要2s,速度太低,不过比较好写,要注意是字符数组要开19以上  /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib

uva11549Calculator Conundrum

题意:有一个老式计算器,只能保存最多n位数,如果结果超出n位,则保留前n位.现在输入一个n和一个k,k表示一个数字,然后不停的求k的平方并令k=k*k,发现会出现循环的结果,求所有结果种最大的一个. 分析:暴力模拟可以过的,但有更好的算法.暴力:用哈希.set都可以.高效算法:Floyd判圈算法,假设两个小孩在有环形的跑道上跑,一个速度为v,另一个速度为2*v,出发点相同,那么总会有相遇的时候,相遇的时候就是跑完一圈了,那么最大值一定跑过了~ 代码: View Code 1 #include <

算法题:uva 11549

题目链接 直接模拟计算过程. 可以看出计算器显示出来的数是循环的,关键在于模拟的过 程中,怎样判断是否循环了. 可以采用STL中的map或set,不过效率较低.hash的话耗很大的空 间. 从大白上可以知道还有一种叫做"Floyd判圈法"的东西.就是假设有两个小孩子在一个圆 圈跑道上赛跑,同时出发,但其中一个小孩的速度是另一个的两倍,所以跑得快的小孩将"追上"跑得 慢的小孩(已经超过n圈了).当超过的那一刻,可以肯定的是,此时快的小孩已经至少跑了1圈以上了 ,也就是

UVa 10494 If We Were a Child Again:高精度

10494 - If We Were a Child Again Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=1435 "Oooooooooooooooh! If I could do the easy mathematics like my s

UVa 706 / POJ 1102 LCD Display (模拟)

706 - LCD Display Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=647 http://poj.org/problem?id=1102 A friend of you has just bought a new computer. Until

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c