UVA11549 Calculator Conundrum

强大的STL啊,膜拜之!

题意:有一个老式计算器,只能显示n位数字。有一天,你无聊了,于是输入一个整数k,然后反复平方,知道溢出。每次溢出,计算器会显示出结果的最高n位和一个错误标记,然后清除错误标记,继续平方。问如果一直这么下去,能得到的最大数是多少?

View Code
#include <iostream>
#include <string>
#include <sstream>
#include <set>
using namespace std;
set<int> s;
int T;
int n,k;
int solve(int k){
    stringstream ss;
    ss<<(long long )k*k;
    string str=ss.str();
    if(str.length()>n)str=str.substr(0,n);

    stringstream ss2(str);
    int ans;
    ss2>>ans;
    return ans;
}
int main()
{

    cin>>T;
    while(T--){
        cin>>n>>k;
        s.clear();
        int ans=0;
        while(!s.count(k)){
            s.insert(k);
            ans=max(ans,k);
            k=solve(k);
        }
        cout<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/arbitrary/p/2817164.html