uva11549 Floyd判圈法

给定n和k,每次k平方后取前n位,求出现的最大值。

所谓Floyd判圈法,一个人跑一步一个人跑两步的神奇算法。

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

//Twenty
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int T,n,k,ans,k1,k2; 
typedef long long LL;
LL tep;
int nxt(int x){
    LL res=(LL)x;
    int k=0;
    res*=res; tep=res;
    while(tep) {tep/=10;k++;}
    if(k>n-1) {
    for(int i=1;i<=k-n;i++)
     res/=10;
    }
    return (int)res;
}
int main()
{
    scanf("%d",&T);
    while(T--){
    scanf("%d%d",&n,&k);
    tep=k; int kk=0;
    while(tep) {tep/=10;kk++;}
    if(kk>n-1) {
    for(int i=1;i<=kk-n;i++)
     k/=10;
    }
    k1=k; k2=nxt(k);
    ans=max(k1,k2);
    while(k1!=k2){
        k1=nxt(k1); ans=max(ans,k1);
        k2=nxt(k2); ans=max(ans,k2);
        k2=nxt(k2); ans=max(ans,k2);
    }
    printf("%d
",ans);
    }
    return 0;
}
Floyd判圈法
原文地址:https://www.cnblogs.com/Achenchen/p/7473534.html