Water's coin
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 452 Accepted Submission(s) : 72
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Mr. Water find a interesting machine in street. There is a number n showed on the screen. When he put x coins in it, the machine will return y coins to him. And after trying several times , Mr. Water find the number of y is equal to the front n digits of x*x。Mr. Water want to know how much he can get at most, can you tell him?
Notes: Water is a doubi, every time he will put all his coins in machine.
Notes: Water is a doubi, every time he will put all his coins in machine.
Input
Input contains an integer T(1<=T<=200) in the first line, and then T lines follow. Each line consists of a pair of integers n(1<=n<=9)and x(0<=x<10^n), separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the max number of coins Mr. Water can get.
The first test
6->36
3->9
9->81
8->64
6->36
The first test
6->36
3->9
9->81
8->64
6->36
Sample Input
2 1 6 2 9
Sample Output
9 81
//题意:
给你一个n,表示数的前n位数,再给一个数x,表示初始的值,问一直将x平方,并每次取它的前n位数,问最大的那个数是什么?
//思路:
大白书上的一道题,貌似是Floyd判环问题(这个还不太懂),直接看代码吧,代码好理解
#include<stdio.h> #include<string.h> #include<algorithm> #define INF 0x3f3f3f3f #define N 10010 #define ll long long using namespace std; int a[110]; int next(int n,int k) { if(!k) return 0; ll k2=(ll)k*k; int l=0; while(k2>0) { a[l++]=k2%10; k2/=10; } if(n>l) n=l; int ans=0; for(int i=0;i<n;i++) ans=ans*10+a[--l]; return ans; } int main() { int t,n,i,j,k; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&k); int ans=k; int k1=k; int k2=k; while(1) { k1=next(n,k1); k2=next(n,k2); if(k2>ans) ans=k2; k2=next(n,k2); if(k2>ans) ans=k2; if(k1==k2)//追上以后停止 break; } printf("%d\n",ans); } return 0; }