A. Sequence with Digits

题目:样例:

输入
8
1 4
487 1
487 2
487 3
487 4
487 5
487 6
487 7

输出
42
487
519
528
544
564
588
628

思路:

        暴力模拟题,看这数据范围,有些人可能会被唬住,以为是高精度或者容易超时,实际上,long long 型最多可以存储10^18次方,刚刚掐住这个数据范围点,所以我们直接用 long long 存储最后暴力模拟一遍即可,这里 ai 是 10^18次方,而我们需要取到当前位数最小的和最大的,我们可以将 ai 转换为字符串遍历一遍,就是 最多 循环 18 次,就可以取到相应的值,所以不用担心超时的。当我们取到 minx = 0 的时候,后面的 k 都是当前的 ai 了.

代码详解如下:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_map>
#define endl '\n'
#define int long long
#define YES puts("YES")
#define NO puts("NO")
#define umap unordered_map
#define All(x) (x).begin(),(x).end()
#pragma GCC optimize(3,"Ofast","inline")
#define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e6 + 10;

inline void solve()
{
	long long num,k;
	cin >> num >> k;
	
	// 根据题意,我们的操作是要 --k 的
	while(--k)
	{
		string tem = to_string(num);
		
		// 定义相应的边界值,取到该位数的最大最小值
		int minx = 11;
		int maxx = -1;
		
		// 开始遍历取值
		for(auto i : tem)
		{
			minx = min(minx,(long long)i - '0');
			maxx = max(maxx,(long long)i - '0');
		}
		
		// 如果出现了位数最小值是 0 ,说明后面的 k - i 个数都是当前 ai
		// 退出循环即可
		if(!minx) break;
		
		// 操作获取 ai
		num += (maxx * minx);
	}
	
	// 输出操作后,ak 的值
	cout << num << endl;
}
signed main()
{
//	freopen("a.txt", "r", stdin);
	___G;
	int _t = 1;
	cin >> _t;
	while (_t--)
	{
		solve();
	}

	return 0;
}

最后提交:

猜你喜欢

转载自blog.csdn.net/hacker_51/article/details/133442818
今日推荐