`
SunnyYoona
  • 浏览: 361668 次
社区版块
存档分类
最新评论

UVA之11549 - Calculator Conundrum

 
阅读更多

【题目】

Problem C

CALCULATOR CONUNDRUM

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

She enters a numberkthen repeatedly squares it until the result overflows. When the result overflows, only thenmostsignificant 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:

“Givennandk, what is the largest number I can get by wasting time in this manner?”

Program Input

The first line of the input contains an integert(1 ≤t≤ 200), the number of test cases. Each test case contains two integersn(1 ≤n≤ 9) andk(0 ≤k< 10n) wherenis the number of digits this calculator can displaykis 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;
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics