uva 11549 - Calculator Conundrum

点击打开链接uva 11549

思路:模拟
分析:
1 题目要求找到最大的n位数,那么我们通过不断的模拟,求出最大的ans
2 这里有个问题就是我们怎么知道什么时候结束呢?我们知道如果当前数已经有出现了那么说明刚好一个循环,这里利用map映射

代码:

#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int n , k;
map<long long , int>mp;

int solve(){
    long long num = k;
    int ans = k;
    mp.clear();
    mp[num] = 1;
    while(true){
        num = num*num;
        long long tmp = num;
        int digit = 0;
        while(tmp){
            digit++;
            tmp /= 10;
        }
        long long pow = 1;
        for(int i = 1 ; i <= digit-n ; i++)
            pow *= 10;
        num /= pow;
        ans = max((long long)ans , num);
        if(mp[num])
           return ans;
        mp[num] = 1;
    }
}

int main(){
    int Case;
    scanf("%d" , &Case);
    while(Case--){
        scanf("%d%d" , &n , &k);
        printf("%d\n" , solve());
    }
    return 0;
}

法二:利用floyd判圈法

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long int64;
int n , k;

int64 next(int64 num){
    int64 tmp = num*num;
    int digit = 0;
    while(tmp){
        digit++;
        tmp /= 10;
    }
    int64 pow = 1;
    for(int i = 1 ; i <= digit-n ; i++)
        pow *= 10;
    return (num*num)/pow;
}

int solve(){
    int ans = k;
    int64  num1 = k , num2 = k;
    while(true){
        num1 = next(num1);
        ans = max((int64)ans , num1);
        num2 = next(num2);
        ans = max((int64)ans , num2);
        num2 = next(num2);
        ans = max((int64)ans , num2);
        if(num1 == num2)
           return ans;
    }
}

int main(){
    int Case;
    scanf("%d" , &Case);
    while(Case--){
        scanf("%d%d" , &n , &k);
        printf("%d\n" , solve());
    }
    return 0;
}
时间: 2024-09-27 02:05:05

uva 11549 - Calculator Conundrum的相关文章

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

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

算法题:uva 11549

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

uva11549Calculator Conundrum

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

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