A. Sequence with Digits

本文介绍了解决一道编程题的方法,利用longlong类型处理大数范围,通过字符串操作找到位数最小值和最大值,进行有效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:样例:

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

最后提交:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值