UVA之11549 - Calculator Conundrum

22 篇文章 7 订阅
【题目】

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



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SmartSi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值