UVA - 11549 Calculator Conundrum

来源:互联网 发布:王者霸域 纯净数据库 编辑:程序博客网 时间:2024/04/28 13:42

题目大意:给出n和k,表示有一个n为的计算器,现在要计算k一直平方中的最大值(在计算器中出现的),超过n位取前n位。

解题思路:看队友题解上有一个判断说形成循环的条件a^2== b^4。然后模拟一下就可以了。

#include <iostream>#include <cmath>using namespace std;int next(int Max, int k) {long long temp = (long long)k * k;while (temp >= Max)temp /= 10;return temp;}int main() {int T;cin >> T;while (T--) {int n, A;cin >> n >> A;int ans = A, B = A;int Max = pow(10, n);do {A = next(Max, A);for (int i = 0; i < 2; i++) {B = next(Max, B);ans = max(ans, B);}} while (A != B);cout << ans << endl;}return 0;}


0 0
原创粉丝点击