标签:
方法1:STL超级暴力方法
方法2:小小优化版
方法3:Floyd判圈算法
#include <set>
#include <sstream>
#include <string>
#include <cstdio>
using namespace std;
typedef long long LL;
int t,n,k,ans;
int mnxt(){
stringstream ss;
ss<<(LL) k * k;
string str = ss.str();
if(str.length() > n) str = str.substr(0,n);
int num = 0;
stringstream ss2(str);
ss2>>num;
return num;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
set<int> s;
ans = k;
while(!s.count(k)){
s.insert(k);
if(k > ans) ans = k;
k = mnxt();
}
printf("%d\n",ans);
}
return 0;
}
#include <cstdio>
#include <set>
#include<algorithm>
using namespace std;
typedef long long LL;
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))
int t,n,k,ans,buf[20];
int mnxt(){
if(!k) return 0;
LL k2 = (LL)k*k;
int cnt = 0;
while (k2){
buf[cnt++] = k2 % 10;
k2 /= 10;
}
int tmpn = min(n,cnt);
FOR(i,0,tmpn){
k2 = k2 * 10 + buf[--cnt];
}
return k2;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
set<int> s;
ans = k;
while(!s.count(k)){
s.insert(k);
if(k > ans) ans = k;
k = mnxt();
}
printf("%d\n",ans);
}
return 0;
}
#include <cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))
int t,n,ans,buf[20],k1,k2;
int mnxt(int k){
if(!k) return 0;
LL k2 = (LL)k*k;
int cnt = 0;
while (k2){
buf[cnt++] = k2 % 10;
k2 /= 10;
}
int tmpn = min(n,cnt);
FOR(i,0,tmpn){
k2 = k2 * 10 + buf[--cnt];
}
return k2;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k1);
k2 = ans = k1;
do{
k1 = mnxt(k1);
k2 = mnxt(k2); if(k2 > ans) ans = k2;
k2 = mnxt(k2); if(k2 > ans) ans = k2;
}
while(k1 != k2);
printf("%d\n",ans);
}
return 0;
}
[2016-03-19][UVA][11549][Calculator Conundrum]
标签:
原文地址:http://www.cnblogs.com/qhy285571052/p/01b08802b544c5c4e3228e06ca8d5c5d.html