uva 11549 calculator conundrum (模拟)

https://vjudge.net/problem/UVA-11549

题意:输入k,反复平方,直到溢出,每次最多显示n个数(前n个),问最大可以显示出几。

wa是因为平方的时候没有转换成longlong型。

体验一把不用vis数组,用map来标记的感觉,还不错。

顺带看白书学了一把floyd判圈法。

#include <iostream>
#include<stdio.h>
#include<cstring>
#include<map>
using namespace std;

map<int,bool> vis;

int main()
{
    int cas,n,k;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d%d",&n,&k);
        int now=k,mark=k,maxn=1;
        for(int i=0;i<n;i++)
            maxn*=10;
        while(!vis.count(now)&&now)
        {
            vis[now]=1;
            long long temp=(long long)now*now;
            while(temp>=maxn)
                temp/=10;
            now=temp;
            mark=max(mark,now);
        }
        printf("%d\n",mark);
        vis.clear();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sadsummerholiday/article/details/82012587