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.
Constraints
- N is an 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.
Sample Input 2
133
Sample Output 2
2024
Sample Input 3
31415926535
Sample Output 3
2006628868244228
解读题意可知:
当十进制表示的各位数中仅包含(0,2,4,6,8)的数成为良好整数。
给一个数字n,求第n个良好整数。
解法:
使用5进制即可。
代码展示:
#include<bits/stdc++.h>
using namespace std;
#define int long long//宏定义,题目数据过大防止背爆掉。经手搓代码最大不超过1e18可以用long long
signed main()
{
int n;
cin>>n;
n--;//先让n自减一
int ans=0;
for(int i=1;n!=0;i*=10)//i每次循环都乘10表示当前位数
{
int a=n%5;//定义一个整数a来判断当前位数应该为几
ans=ans+i*2*a;
n/=5;//每次循环让n整除5,也就是5进制。
}
cout<<ans;
return 0;
}
以上纯自我想法 如有错误可以告诉我。