uva11549 - Calculator Conundrum

题意:


一个数k,让它一直平方一直平方,它只能显示n位数,求这个n为数最大为多少。


思路:


我们可以得到,这个数将出现循环,所以可用set容器中的count函数检测某元素在此前是否出现过,一直获得下一个数,保留最大值即可。


代码:


#include <iostream>
#include <cstdio>
#include <cmath>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std;
int arr[100];
int Next(int n, int k) {
	if (!k)
		return 0;
	long long k2 = (long long)k*k;
	memset(arr, 0, sizeof(arr));
	int l = 0;
	while(k2 > 0) {
		arr[l++] = k2 % 10;
		k2 = k2 / 10;
	}
	if (l < n)
		n = l;
	int i = 0; k = 0;
	while (i<n){
		k = k * 10 + arr[--l];
		i++;
	}
//	printf("%d\n",k);
	return k;
}
int main(){
	int cas;
	scanf("%d", &cas);
	while (cas--) {
		int n, k;
		scanf("%d%d", &n, &k);
		set<int> st;
		int ans = k;
		while (!st.count(k))  {
			st.insert(k);
			if (ans < k)
				ans = k;
			k = Next(n, k);
		}
		printf("%d\n", ans);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值