练习cfA. Sequence with Digits
题目如下
A. Sequence with Digits
time limit per test1 second
memory limit per test256 megabytes
Let's define the following recurrence:
𝑎𝑛+1=𝑎𝑛+𝑚𝑖𝑛𝐷𝑖𝑔𝑖𝑡(𝑎𝑛)⋅𝑚𝑎𝑥𝐷𝑖𝑔𝑖𝑡(𝑎𝑛).
Here 𝑚𝑖𝑛𝐷𝑖𝑔𝑖𝑡(𝑥) and 𝑚𝑎𝑥𝐷𝑖𝑔𝑖𝑡(𝑥) are the minimal and maximal digits in the decimal representation of 𝑥 without leading zeroes. For examples refer to notes.
Your task is calculate 𝑎𝐾 for given 𝑎1 and 𝐾.
Input
The first line contains one integer 𝑡 (1≤𝑡≤1000) — the number of independent test cases.
Each test case consists of a single line containing two integers 𝑎1 and 𝐾 (1≤𝑎1≤1018, 1≤𝐾≤1016) separated by a space.
Output
For each test case print one integer 𝑎𝐾 on a separate line.
题目大意
现有a1和k,根据𝑎𝑛+1=𝑎𝑛+𝑚𝑖𝑛𝐷𝑖𝑔𝑖𝑡(𝑎𝑛)⋅𝑚𝑎𝑥𝐷𝑖𝑔𝑖𝑡(𝑎𝑛)的操作,进行k次得出ak的大小。
𝑚𝑖𝑛𝐷𝑖𝑔𝑖𝑡(𝑎𝑛)代表十进制数an各位上最小一个数,𝑚𝑎𝑥𝐷𝑖𝑔𝑖𝑡(𝑎𝑛)则代表十进制数an各位上最大一个数。
题目分析
只要能够找出一个数各位上的最大和最小数即可;
点击查看代码
void min_max(long long x, int* min, int* max) {
*min = 9;
*max = 0;
while(x > 0){
int d = x % 10;
if(d < *min){
*min = d;
}
if(d > *max){
*max = d;
}
x /= 10;
}
}
点击查看代码
if(min == 0){
break;
}
完整代码
点击查看代码
#include <stdio.h>
void min_max(long long x, int* min, int* max){
*min = 9;
*max = 0;
while(x > 0){
int d = x % 10;
if(d < *min){
*min = d;
}
if(d > *max){
*max = d;
}
x /= 10;
}
}
int main(){
int t;
scanf("%d", &t);
while(t--){
long long a, k;
scanf("%lld%lld", &a, &k);
for(long long i = 1; i < k; ++i){
int min, max;
min_max(a, &min, &max);
if(min == 0){
break;
}
a += min * max;
}
printf("%lld\n", a);
}
return 0;
}

浙公网安备 33010602011771号