Water's coin (高效)

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.

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

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值