ABC 336 C - Even Digits

先看一下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!!!

  • 31
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XchenPlayer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值