目录
先看一下ABC 336 C - Even Digits的题目(大致就是输入一个数n,输出第n个只由0,2,4,6,8组成的数)
题目
Time Limit: 2 sec / Memory Limit: 1024 MB
Score: 300 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 (0, 2, 4, 6, and 8).
For example, 0, 68, and 2024 are good integers.
You are given an integer N. Find the N-th smallest good integer.
Constraints
- 1≤N≤10^12
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the -th smallest good integer N
SAMPLES
Sample Input 1
8
Sample Output 1
24
The good integers in ascending order are .The eighth smallest is , which should be printed.0,2,4,6,8,20,22,24,26,28,…24
Sample Input 2
133
Sample Output 2
2024
Sample Input 3
31415926535
Sample Output 3
2006628868244228
解题过程
首先,数据范围已经到1e12,要开个long long以防爆int
其次,根据题目,0,2,4,6,8这五个数之间的组成,可以类比普通两位数的组成。对于普通的两位数,每位上由10个数字组成,取余是末位数,而整除10可以获得位数。对于这五个数组成的数的排序,整除5,即可获得这个数一共有几位数。
以上说的有点混乱,总的来说,就是利用递归来从后往前输出,递归结束标志就是到达第一位,将第一位输出出来。
代码段
#include<bits/stdc++.h>
using namespace std;
long long n;
int a[100];
void dg(long long n)
{
if(!(n/5))
{
cout<<n*2;
return ;
}
dg(n/5);
cout<<n%5*2;
}
signed main()
{
cin>>n;
n--;
dg(n);
}
总结
本题我主要用的递归方法,利用都是每位5个数和偶数的特点,通过取余乘2来实现结果的输出,注意一定不要忘了开long long!!!