【原文】:
C - Even Digits Editorial
Time Limit: 2 sec / Memory Limit: 1024 MB
Score: 300300 points
Problem Statement
A non-negative integer n is called a good integer when it satisfies the following condition:
- All digits in the decimal notation of n are even numbers (00, 22, 44, 66, and 88).
For example, 00, 6868, and 20242024 are good integers.
You are given an integer N. Find the N-th smallest good integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the N-th smallest good integer.
Sample Input 1
8
Sample Output 1
24 The good integers in ascending order are 0,2,4,6,8,20,22,24,26,28,… The eighth smallest is 24, which should be printed.
【题解】
寻找规律发现是n这个数,先减一(因为第一个是0)十进制的数转换成五进制后,取余5*2;
【注意】
要注意n=1的特殊情况,n=1的时候输出的是0,不是1,并且这道题不用高精度的话,一定要开long long 。
Code
#include <bits/stdc++.h>//
using namespace std;
const int N = 1e6;
long long int n,b[N],s=0;//直接都开long long
int main(void)
{
scanf("%lld", &n);//开了long long 输入要%lld
if (n == 1)//排除“1”的情况
{
cout << 0;
return 0;
}
n -= 1;//先减去1,可以带入一个数字模拟一下
while (n > 0)
{
b[s++] = n % 5;//先得到答案的最后一位
n /= 5;
}
for (int i = s-1; i>=0; i--)//倒序输出,因为先计算的最后一位
{
printf("%d", 2 * b[i]);//乘上两倍
}
return 0;
}