UVa 11549 - Calculator Conundrum(计算循环节)

CALCULATOR CONUNDRUM

Alice got a hold of an old calculator that can display n digits. She was bored enough to come up with the following time waster.

She enters a number k then repeatedly squares it until the result overflows. When the result overflows, only then most significant digits are displayed on the screen and an error flag appears. Alice can clear the error and continue squaring the displayed number. She got bored by this soon enough, but wondered:

“Given n and k, what is the largest number I can get by wasting time in this manner?”

Program Input

The first line of the input contains an integert (1 ≤ t ≤ 200), the number of test cases. Each test case contains two integersn (1 ≤ n ≤ 9) and k (0 ≤ k < 10n) wheren is the number of digits this calculator can display k is the starting number.

Program Output

For each test case, print the maximum number that Alice can get by repeatedly squaring the starting number as described.

Sample Input & Output

INPUT

2
1 6
2 99
OUTPUT
9
99

Calgary Collegiate Programming Contest 2008

 

经验教训:思考问题的解决方法前,先要根据已知条件推断出问题的特点和性质。例如本题中,计算器能够显示的数字最多是n位,所能表示的数字的个数是有限的,那么按照对k求平方,然后取前n位数的方法,重复下去,总有一个时候,出现一个重复的数,这个数就是循环节。

//利用STL中的set来寻找循环节
#include <iostream>
#include <cstdio>
#include <set>
#include <cmath>
using namespace std;

//#define LOCAL

#ifdef LOCAL
#define TYPE __int64
#else
#define TYPE long long
#endif

int HighNDigits(int n, TYPE k)
{
	int len = floor(log10(k)) + 1;
	if (len > n)
	{
		len -= n;
		int m = 1;
		while (len-- != 0)
		{
			m *= 10;
		}
		return (int)(k / m);
	}
	return (int)k;
}

int main(void)
{
	int ncases;
	cin >> ncases;
	while (ncases-- != 0)
	{
		int n, k;
		cin >> n >> k;
		int max = k;
		set<int> s;
		while (s.find(k) == s.end())
		{
			s.insert(k);
			if (k > max)
			{
				max = k;
			}
			k = HighNDigits(n, (TYPE)k*k);
		}
		cout << max << endl;
	}
	return 0;
}


 

//Floyd判圈算法
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

//#define LOCAL

#ifdef LOCAL
#define TYPE __int64
#else
#define TYPE long long
#endif

int HighNDigits(int n, TYPE k)
{
	int len = floor(log10(k)) + 1;
	if (len > n)
	{
		len -= n;
		int m = 1;
		while (len-- != 0)
		{
			m *= 10;
		}
		return (int)(k / m);
	}
	return (int)k;
}

int main(void)
{
	int ncases;
	cin >> ncases;
	while (ncases-- != 0)
	{
		int n, k;
		cin >> n >> k;
		int k1 = k, k2 = k, max = k;
		//k1每次向前走一步,k2每次向前走两步,如果存在循环,
		//k2最终会与k1相遇,这时k2已经走过了所有的数字,求出max即可
		do
		{
			k1 = HighNDigits(n, (TYPE)k1*k1);
			k2 = HighNDigits(n, (TYPE)k2*k2);
			if (k2 > max) max = k2;
			k2 = HighNDigits(n, (TYPE)k2*k2);
			if (k2 > max) max = k2;
		} while (k2 != k1);
		cout << max << endl;
	}
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庞老板

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值