给定数字n,找出仅由偶数数字(0、2、4、6、8)组成的第n个正数。很少有偶数构成的数字是0、2、4、6、8、20、22、24……。
例子 :
Input : 2
Output : 2
Second number made of 0, 2, 4, 6, 8 is 2
Input : 10
Output : 28
天真的方法
天真的方法是从0开始,检查它是否仅由{0,2,4,4,6,8}组成,然后在找到第n个这样的数字时停止。
C++
// Simple C++ program to find
// n-th number made of even
// digits only
#include
using namespace std;
// function to calculate nth
// number made of even digits only
int findNthEvenDigitNumber(int n )
{
// variable to note how
// many such numbers have
// been found till now
int count = 0;
for (int i = 0 ; ; i++)
{
int curr = i;
// bool variable to check if
// 1, 3, 5, 7, 9 is there or not
bool isCurrEvenDigit = true ;
// checking each digit
// of the number
while (curr != 0)
{
// If 1, 3, 5, 7, 9 is found
// temp is changed to false
if (curr % 10 == 1 || curr % 10 == 3 ||
curr % 10 == 5 || curr % 10 == 7 ||
curr % 10 == 9)
isCurrEvenDigit = false;
curr = curr / 10;
}
// temp is true it means that it
// does not have 1, 3, 5, 7, 9
if (isCurrEvenDigit == true)
count++;
// If nth such number is
// found return it
if (count == n)
return i;
}
}
// Driver Code
int main()
{
cout << findNthEvenDigitNumber(2)
<< endl;
cout << findNthEvenDigitNumber(10)
<< endl;
return 0;
}
Java
// Simple Java program to
// find the n-th number made
// of even digits only
class GFG
{
// function to calculate nth
// number made of even digits only
static int findNthEvenDigitNumber(int n )
{
// variable to note how
// many such numbers have
// been found till now
int count = 0;
for (int i = 0 ; ; i++)
{
int curr = i;
// bool variable to check if
// 1, 3, 5, 7, 9 is there or not
boolean isCurrEvenDigit = true ;
// checking each digit
// of the number
while (curr != 0)
{
// If 1, 3, 5, 7, 9 is found
// temp is changed to false
if (curr % 10 == 1 || curr % 10 == 3 ||
curr % 10 == 5 || curr % 10 == 7 ||
curr % 10 == 9)
isCurrEvenDigit = false;
curr = curr / 10;
}
// temp is true it means that it
// does not have 1, 3, 5, 7, 9
if (isCurrEvenDigit == true)
count++;
// If nth such number
// is found return it
if (count == n)
return i;
}
}
// Driver Code
public static void main (String[] args)
{
System.out.println(findNthEvenDigitNumber(2));
System.out.println(findNthEvenDigitNumber(10));
}
}
Python3
# Simple Python3 program to find nth
# number made of even digits only
# function to calculate nth number
# made of even digits only
def findNthEvenDigitNumber(n):
# variable to note how many such
# numbers have been found till now
count = 0;
i = 0;
while (True):
curr = i;
# bool variable to check if
# 1, 3, 5, 7, 9 is there or not
isCurrEvenDigit = True;
# checking each digit of the number
while (curr != 0):
# If 1, 3, 5, 7, 9 is found
# temp is changed to false
if (curr % 10 == 1 or curr % 10 == 3 or
curr % 10 == 5 or curr % 10 == 7 or
curr % 10 == 9):
isCurrEvenDigit = False;
curr = curr // 10;
# temp is true it means that it
# does not have 1, 3, 5, 7, 9
if (isCurrEvenDigit == True):
count += 1;
# If nth such number is found,
# return it
if (count == n):
return i;
i += 1;
# Driver Code
print(findNthEvenDigitNumber(2));
print(findNthEvenDigitNumber(10));
# This code is contributed by mits
C#
// Simple C# program to
// find the n-th number
// made of even digits only
using System;
class GFG
{
// function to calculate nth
// number made of even digits only
static int findNthEvenDigitNumber(int n )
{
// variable to note how
// many such numbers have
// been found till now
int count = 0;
for (int i = 0 ; ; i++)
{
int curr = i;
// bool variable to check if
// 1, 3, 5, 7, 9 is there or not
bool isCurrEvenDigit = true ;
// checking each digit
// of the number
while (curr != 0)
{
// If 1, 3, 5, 7, 9 is found
// temp is changed to false
if (curr % 10 == 1 || curr % 10 == 3 ||
curr % 10 == 5 || curr % 10 == 7 ||
curr % 10 == 9 )
isCurrEvenDigit = false;
curr = curr / 10;
}
// temp is true it means that it
// does not have 1, 3, 5, 7, 9
if (isCurrEvenDigit == true)
count++;
// If nth such number
// is found return it
if (count == n)
return i;
}
}
// Driver code
public static void Main ()
{
Console.WriteLine(findNthEvenDigitNumber(2));
Console.WriteLine(findNthEvenDigitNumber(10));
}
}
// This article is contributed by vt_m.
PHP
Javascript
C++
// Efficient C++ program to
// find n-th number made of
// even digits only
#include
using namespace std;
// function to find nth number
// made of even digits only
int findNthEvenDigitNumber(int n)
{
// If n=1 return 0
if (n == 1)
return 0;
// vector to store the digits
// when converted into base 5
vector< int> v;
// Reduce n to n-1 to exclude 0
n = n - 1;
// Reduce n to base 5
// number and store digits
while (n > 0)
{
// pushing the digits
// into vector
v.push_back(n % 5);
n = n / 5;
}
// variable to represent the
// number after converting it
// to base 5. Since the digits
// are be in reverse order,
// we traverse vector from back
int result = 0;
for (int i = v.size() - 1; i >= 0; i--)
{
result = result * 10;
result = result + v[i];
}
// return 2*result (to convert
// digits 0, 1, 2, 3, 4 to
// 0, 2, 4, 6, 8.
return 2*result;
}
// Driver Code
int main()
{
cout << findNthEvenDigitNumber(2)
<< endl;
cout << findNthEvenDigitNumber(10)
<< endl;
return 0;
}
Java
import java.util.*;
// Efficient Java program to
// find n-th number made of
// even digits only
class GFG {
// function to find nth number
// made of even digits only
static int findNthEvenDigitNumber(int n) {
// If n=1 return 0
if (n == 1) {
return 0;
}
// vector to store the digits
// when converted into base 5
Vector< Integer> v = new Vector<>();
// Reduce n to n-1 to exclude 0
n = n - 1;
// Reduce n to base 5
// number and store digits
while (n > 0) {
// pushing the digits
// into vector
v.add(n % 5);
n = n / 5;
}
// variable to represent the
// number after converting it
// to base 5. Since the digits
// are be in reverse order,
// we traverse vector from back
int result = 0;
for (int i = v.size() - 1; i >= 0; i--) {
result = result * 10;
result = result + v.get(i);
}
// return 2*result (to convert
// digits 0, 1, 2, 3, 4 to
// 0, 2, 4, 6, 8.
return 2 * result;
}
// Driver Code
public static void main(String[] args) {
System.out.println(findNthEvenDigitNumber(2));
System.out.println(findNthEvenDigitNumber(10));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Efficient Python 3 program to find n-th
# number made of even digits only
# function to find nth number made of
# even digits only
def findNthEvenDigitNumber( n):
# If n = 1 return 0
if (n == 1):
return 0
# vector to store the digits
# when converted into base 5
v = []
# Reduce n to n-1 to exclude 0
n = n - 1
# Reduce n to base 5 number and
# store digits
while (n > 0):
# pushing the digits into vector
v.append(n % 5)
n = n // 5
# variable to represent the number
# after converting it to base 5.
# Since the digits are be in reverse
# order, we traverse vector from back
result = 0
for i in range(len(v) - 1, -1, -1):
result = result * 10
result = result + v[i]
# return 2*result (to convert
# digits 0, 1, 2, 3, 4 to
# 0, 2, 4, 6, 8.
return 2 * result
# Driver Code
if __name__ == "__main__":
print(findNthEvenDigitNumber(2))
print(findNthEvenDigitNumber(10))
# This code is contributed by ita_c
C#
// Efficient C# program to
// find n-th number made of
// even digits only
using System;
using System.Collections;
class GFG {
// function to find nth number
// made of even digits only
static int findNthEvenDigitNumber(int n)
{
// If n=1 return 0
if (n == 1)
{
return 0;
}
// vector to store the digits
// when converted into base 5
ArrayList v = new ArrayList();
// Reduce n to n-1 to exclude 0
n = n - 1;
// Reduce n to base 5
// number and store digits
while (n > 0)
{
// pushing the digits
// into vector
v.Add(n % 5);
n = n / 5;
}
// variable to represent the
// number after converting it
// to base 5. Since the digits
// are be in reverse order,
// we traverse vector from back
int result = 0;
for (int i = v.Count - 1; i >= 0; i--)
{
result = result * 10;
result = result + (int)v[i];
}
// return 2*result (to convert
// digits 0, 1, 2, 3, 4 to
// 0, 2, 4, 6, 8.
return 2 * result;
}
// Driver Code
public static void Main()
{
Console.WriteLine(findNthEvenDigitNumber(2));
Console.WriteLine(findNthEvenDigitNumber(10));
}
}
// This code is contributed by 29AjayKumar
PHP
0)
{
// pushing the digits
// into vector
array_push($v, $n % 5);
$n = (int)($n / 5);
}
// variable to represent the number
// after converting it to base 5.
// Since the digits are be in
// reverse order, we traverse vector
// from back
$result = 0;
for ($i = count($v) - 1; $i >= 0; $i--)
{
$result = $result * 10;
$result = $result + $v[$i];
}
// return 2*result (to convert
// digits 0, 1, 2, 3, 4 to
// 0, 2, 4, 6, 8.
return 2 * $result;
}
// Driver Code
echo findNthEvenDigitNumber(2) . "\n";
echo findNthEvenDigitNumber(10) . "\n"
// This code is contributed by mits
?>
输出 :
2
28
高效方法
我们需要找到由5个数字组成的数字,即0、2、4、6和8。将数字转换为以5为底的数字时,它只会由数字{0、1、2、3、4}构成。可以清楚地看到,所需数字集{0,2,4,6,6,8}中的每个数字都是以5为基数的数字集的相应索引中数字的两倍。因此,要找到仅由偶数数字组成的第n个数字,请执行以下提到的步骤
步骤1:将n转换为n-1,以排除零。
步骤2:将n转换为5个基本十进制数字。
步骤3:将上面找到的数字乘以2。这是必需的数字
C++
// Efficient C++ program to
// find n-th number made of
// even digits only
#include
using namespace std;
// function to find nth number
// made of even digits only
int findNthEvenDigitNumber(int n)
{
// If n=1 return 0
if (n == 1)
return 0;
// vector to store the digits
// when converted into base 5
vector< int> v;
// Reduce n to n-1 to exclude 0
n = n - 1;
// Reduce n to base 5
// number and store digits
while (n > 0)
{
// pushing the digits
// into vector
v.push_back(n % 5);
n = n / 5;
}
// variable to represent the
// number after converting it
// to base 5. Since the digits
// are be in reverse order,
// we traverse vector from back
int result = 0;
for (int i = v.size() - 1; i >= 0; i--)
{
result = result * 10;
result = result + v[i];
}
// return 2*result (to convert
// digits 0, 1, 2, 3, 4 to
// 0, 2, 4, 6, 8.
return 2*result;
}
// Driver Code
int main()
{
cout << findNthEvenDigitNumber(2)
<< endl;
cout << findNthEvenDigitNumber(10)
<< endl;
return 0;
}
Java
import java.util.*;
// Efficient Java program to
// find n-th number made of
// even digits only
class GFG {
// function to find nth number
// made of even digits only
static int findNthEvenDigitNumber(int n) {
// If n=1 return 0
if (n == 1) {
return 0;
}
// vector to store the digits
// when converted into base 5
Vector< Integer> v = new Vector<>();
// Reduce n to n-1 to exclude 0
n = n - 1;
// Reduce n to base 5
// number and store digits
while (n > 0) {
// pushing the digits
// into vector
v.add(n % 5);
n = n / 5;
}
// variable to represent the
// number after converting it
// to base 5. Since the digits
// are be in reverse order,
// we traverse vector from back
int result = 0;
for (int i = v.size() - 1; i >= 0; i--) {
result = result * 10;
result = result + v.get(i);
}
// return 2*result (to convert
// digits 0, 1, 2, 3, 4 to
// 0, 2, 4, 6, 8.
return 2 * result;
}
// Driver Code
public static void main(String[] args) {
System.out.println(findNthEvenDigitNumber(2));
System.out.println(findNthEvenDigitNumber(10));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Efficient Python 3 program to find n-th
# number made of even digits only
# function to find nth number made of
# even digits only
def findNthEvenDigitNumber( n):
# If n = 1 return 0
if (n == 1):
return 0
# vector to store the digits
# when converted into base 5
v = []
# Reduce n to n-1 to exclude 0
n = n - 1
# Reduce n to base 5 number and
# store digits
while (n > 0):
# pushing the digits into vector
v.append(n % 5)
n = n // 5
# variable to represent the number
# after converting it to base 5.
# Since the digits are be in reverse
# order, we traverse vector from back
result = 0
for i in range(len(v) - 1, -1, -1):
result = result * 10
result = result + v[i]
# return 2*result (to convert
# digits 0, 1, 2, 3, 4 to
# 0, 2, 4, 6, 8.
return 2 * result
# Driver Code
if __name__ == "__main__":
print(findNthEvenDigitNumber(2))
print(findNthEvenDigitNumber(10))
# This code is contributed by ita_c
C#
// Efficient C# program to
// find n-th number made of
// even digits only
using System;
using System.Collections;
class GFG {
// function to find nth number
// made of even digits only
static int findNthEvenDigitNumber(int n)
{
// If n=1 return 0
if (n == 1)
{
return 0;
}
// vector to store the digits
// when converted into base 5
ArrayList v = new ArrayList();
// Reduce n to n-1 to exclude 0
n = n - 1;
// Reduce n to base 5
// number and store digits
while (n > 0)
{
// pushing the digits
// into vector
v.Add(n % 5);
n = n / 5;
}
// variable to represent the
// number after converting it
// to base 5. Since the digits
// are be in reverse order,
// we traverse vector from back
int result = 0;
for (int i = v.Count - 1; i >= 0; i--)
{
result = result * 10;
result = result + (int)v[i];
}
// return 2*result (to convert
// digits 0, 1, 2, 3, 4 to
// 0, 2, 4, 6, 8.
return 2 * result;
}
// Driver Code
public static void Main()
{
Console.WriteLine(findNthEvenDigitNumber(2));
Console.WriteLine(findNthEvenDigitNumber(10));
}
}
// This code is contributed by 29AjayKumar
的PHP
0)
{
// pushing the digits
// into vector
array_push($v, $n % 5);
$n = (int)($n / 5);
}
// variable to represent the number
// after converting it to base 5.
// Since the digits are be in
// reverse order, we traverse vector
// from back
$result = 0;
for ($i = count($v) - 1; $i >= 0; $i--)
{
$result = $result * 10;
$result = $result + $v[$i];
}
// return 2*result (to convert
// digits 0, 1, 2, 3, 4 to
// 0, 2, 4, 6, 8.
return 2 * $result;
}
// Driver Code
echo findNthEvenDigitNumber(2) . "\n";
echo findNthEvenDigitNumber(10) . "\n"
// This code is contributed by mits
?>
输出 :
2
28
时间复杂度: O(log 5 (n))