给定数字N,任务是找到不小于N的最小数字,该数字的所有数字均为奇数。
例子:
Input: N = 1345
Output: 1351
1351 is the smallest number not
less than N, whose all digits are odd.
Input: N = 2397
Output: 3111
3111 is the smallest number not
less than N, whose all digits are odd.
幼稚的方法:幼稚的方法是保持从N进行迭代,直到找到一个所有数字都为奇数的数字。
下面是上述方法的实现:
C++
// CPP program to print the smallest
// integer not less than N with all odd digits
#include
using namespace std;
// function to check if all digits
// are odd of a given number
int check_digits(int n)
{
// iterate for all digits
while (n) {
if ((n % 10) % 2 == 0) // if digit is even
return 0;
n /= 10;
}
// all digits are odd
return 1;
}
// function to return the smallest number
// with all digits odd
int smallest_number(int n)
{
// iterate till we find a
// number with all digits odd
for (int i = n;; i++)
if (check_digits(i))
return i;
}
// Driver Code
int main()
{
int N = 2397;
cout << smallest_number(N);
return 0;
}
Java
// Java program to print the smallest
// integer not less than N with all odd digits
class Geeks {
// function to check if all digits
// are odd of a given number
static int check_digits(int n)
{
// iterate for all digits
while (n > 0) {
if ((n % 10) % 2 == 0) // if digit is even
return 0;
n /= 10;
}
// all digits are odd
return 1;
}
// function to return the smallest number
// with all digits odd
static int smallest_number(int n)
{
// iterate till we find a
// number with all digits odd
for (int i = n;; i++)
if (check_digits(i) > 0)
return i;
}
// Driver Code
public static void main(String args[])
{
int N = 2397;
System.out.println(smallest_number(N));
}
}
// This code is contributed by Kirti_Mangal
Python3
# Python 3 program to print the smallest
# integer not less than N with all odd digits
# function to check if all digits
# are odd of a given number
def check_digits(n):
# iterate for all digits
while (n):
# if digit is even
if ((n % 10) % 2 == 0):
return 0
n = int(n / 10)
# all digits are odd
return 1
# function to return the smallest
# number with all digits odd
def smallest_number(n):
# iterate till we find a
# number with all digits odd
i = n
while(1):
if (check_digits(i)):
return i
i += 1
# Driver Code
if __name__ == '__main__':
N = 2397
print(smallest_number(N))
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to print the smallest
// integer not less than N with all
// odd digits
using System;
class GFG
{
// function to check if all digits
// are odd of a given number
static int check_digits(int n)
{
// iterate for all digits
while (n != 0)
{
if ((n % 10) % 2 == 0) // if digit is even
return 0;
n /= 10;
}
// all digits are odd
return 1;
}
// function to return the smallest
// number with all digits odd
static int smallest_number(int n)
{
// iterate till we find a
// number with all digits odd
for (int i = n;; i++)
if (check_digits(i) == 1)
return i;
}
// Driver Code
static void Main()
{
int N = 2397;
Console.WriteLine(smallest_number(N));
}
}
// This code is contributed by ANKITRAI1
PHP
1)
{
// if digit is even
if (($n % 10) % 2 == 0)
return 0;
$n = (int)$n / 10;
}
// all digits are odd
return 1;
}
// function to return the smallest
// number with all digits odd
function smallest_number( $n)
{
// iterate till we find a
// number with all digits odd
for ($i = $n;; $i++)
if (check_digits($i))
return $i;
}
// Driver Code
$N = 2397;
echo smallest_number($N);
// This code is contributed by ajit
?>
C++
// CPP program to print the smallest
// integer not less than N with all odd digits
#include
using namespace std;
// function to return the smallest number
// with all digits odd
int smallestNumber(int n)
{
int num = 0;
string s = "";
int duplicate = n;
// convert the number to string to
// perform operations
while (n) {
s = char(n % 10 + 48) + s;
n /= 10;
}
int index = -1;
// find out the first even number
for (int i = 0; i < s.length(); i++) {
int digit = s[i] - '0';
if ((digit & 1) == 0) {
index = i;
break;
}
}
// if no even numbers are there, than n is the answer
if (index == -1)
return duplicate;
// add all digits till first even
for (int i = 0; i < index; i++)
num = num * 10 + (s[i] - '0');
// increase the even digit by 1
num = num * 10 + (s[index] - '0' + 1);
// add 1 to the right of the even number
for (int i = index + 1; i < s.length(); i++)
num = num * 10 + 1;
return num;
}
// Driver Code
int main()
{
int N = 2397;
cout << smallestNumber(N);
return 0;
}
Java
//Java program to print the smallest
// integer not less than N with all odd digits
public class GFG {
// function to return the smallest number
// with all digits odd
static int smallestNumber(int n) {
int num = 0;
String s = "";
int duplicate = n;
// convert the number to string to
// perform operations
while (n > 0) {
s = (char) (n % 10 + 48) + s;
n /= 10;
}
int index = -1;
// find out the first even number
for (int i = 0; i < s.length(); i++) {
int digit = s.charAt(i) - '0';
if ((digit & 1) == 0) {
index = i;
break;
}
}
// if no even numbers are there, than n is the answer
if (index == -1) {
return duplicate;
}
// add all digits till first even
for (int i = 0; i < index; i++) {
num = num * 10 + (s.charAt(i) - '0');
}
// increase the even digit by 1
num = num * 10 + (s.charAt(index) - '0' + 1);
// add 1 to the right of the even number
for (int i = index + 1; i < s.length(); i++) {
num = num * 10 + 1;
}
return num;
}
// Driver Code
static public void main(String[] args) {
int N = 2397;
System.out.println(smallestNumber(N));
}
}
/*This code is contributed by PrinciRaj1992*/
Python 3
# Python 3 program to print the smallest
# integer not less than N with all odd digits
# function to return the smallest
# number with all digits odd
def smallestNumber(n):
num = 0
s = ""
duplicate = n
# convert the number to string to
# perform operations
while (n):
s = chr(n % 10 + 48) + s
n //= 10
index = -1
# find out the first even number
for i in range(len( s)):
digit = ord(s[i]) - ord('0')
if ((digit & 1) == 0) :
index = i
break
# if no even numbers are there,
# than n is the answer
if (index == -1):
return duplicate
# add all digits till first even
for i in range( index):
num = num * 10 + (ord(s[i]) -
ord('0'))
# increase the even digit by 1
num = num * 10 + (ord(s[index]) -
ord('0') + 1)
# add 1 to the right of the
# even number
for i in range(index + 1 , len(s)):
num = num * 10 + 1
return num
# Driver Code
if __name__ == "__main__":
N = 2397
print(smallestNumber(N))
# This code is contributed by ita_c
C#
// C# program to print the smallest
// integer not less than N with all odd digits
using System;
public class GFG{
// function to return the smallest number
// with all digits odd
static int smallestNumber(int n) {
int num = 0;
String s = "";
int duplicate = n;
// convert the number to string to
// perform operations
while (n > 0) {
s = (char) (n % 10 + 48) + s;
n /= 10;
}
int index = -1;
// find out the first even number
for (int i = 0; i < s.Length; i++) {
int digit = s[i] - '0';
if ((digit & 1) == 0) {
index = i;
break;
}
}
// if no even numbers are there, than n is the answer
if (index == -1) {
return duplicate;
}
// add all digits till first even
for (int i = 0; i < index; i++) {
num = num * 10 + (s[i] - '0');
}
// increase the even digit by 1
num = num * 10 + (s[index] - '0' + 1);
// add 1 to the right of the even number
for (int i = index + 1; i < s.Length; i++) {
num = num * 10 + 1;
}
return num;
}
// Driver Code
static public void Main() {
int N = 2397;
Console.WriteLine(smallestNumber(N));
}
}
// This code is contributed by PrinciRaj1992
输出:
3111
时间复杂度: O(N)
高效的方法:我们可以通过将N中的第一个偶数数字增加一个,然后用最小的奇数数字(即1)替换该奇数数字右边的所有数字来找到数字。如果N中没有偶数,则N本身就是最小的数字。例如,考虑N =213。将N中的第一个偶数数字递增,即2到3,然后将其右边的所有数字都替换为1。因此,我们所需的数字将为311。
下面是有效方法的实现:
C++
// CPP program to print the smallest
// integer not less than N with all odd digits
#include
using namespace std;
// function to return the smallest number
// with all digits odd
int smallestNumber(int n)
{
int num = 0;
string s = "";
int duplicate = n;
// convert the number to string to
// perform operations
while (n) {
s = char(n % 10 + 48) + s;
n /= 10;
}
int index = -1;
// find out the first even number
for (int i = 0; i < s.length(); i++) {
int digit = s[i] - '0';
if ((digit & 1) == 0) {
index = i;
break;
}
}
// if no even numbers are there, than n is the answer
if (index == -1)
return duplicate;
// add all digits till first even
for (int i = 0; i < index; i++)
num = num * 10 + (s[i] - '0');
// increase the even digit by 1
num = num * 10 + (s[index] - '0' + 1);
// add 1 to the right of the even number
for (int i = index + 1; i < s.length(); i++)
num = num * 10 + 1;
return num;
}
// Driver Code
int main()
{
int N = 2397;
cout << smallestNumber(N);
return 0;
}
Java
//Java program to print the smallest
// integer not less than N with all odd digits
public class GFG {
// function to return the smallest number
// with all digits odd
static int smallestNumber(int n) {
int num = 0;
String s = "";
int duplicate = n;
// convert the number to string to
// perform operations
while (n > 0) {
s = (char) (n % 10 + 48) + s;
n /= 10;
}
int index = -1;
// find out the first even number
for (int i = 0; i < s.length(); i++) {
int digit = s.charAt(i) - '0';
if ((digit & 1) == 0) {
index = i;
break;
}
}
// if no even numbers are there, than n is the answer
if (index == -1) {
return duplicate;
}
// add all digits till first even
for (int i = 0; i < index; i++) {
num = num * 10 + (s.charAt(i) - '0');
}
// increase the even digit by 1
num = num * 10 + (s.charAt(index) - '0' + 1);
// add 1 to the right of the even number
for (int i = index + 1; i < s.length(); i++) {
num = num * 10 + 1;
}
return num;
}
// Driver Code
static public void main(String[] args) {
int N = 2397;
System.out.println(smallestNumber(N));
}
}
/*This code is contributed by PrinciRaj1992*/
的Python 3
# Python 3 program to print the smallest
# integer not less than N with all odd digits
# function to return the smallest
# number with all digits odd
def smallestNumber(n):
num = 0
s = ""
duplicate = n
# convert the number to string to
# perform operations
while (n):
s = chr(n % 10 + 48) + s
n //= 10
index = -1
# find out the first even number
for i in range(len( s)):
digit = ord(s[i]) - ord('0')
if ((digit & 1) == 0) :
index = i
break
# if no even numbers are there,
# than n is the answer
if (index == -1):
return duplicate
# add all digits till first even
for i in range( index):
num = num * 10 + (ord(s[i]) -
ord('0'))
# increase the even digit by 1
num = num * 10 + (ord(s[index]) -
ord('0') + 1)
# add 1 to the right of the
# even number
for i in range(index + 1 , len(s)):
num = num * 10 + 1
return num
# Driver Code
if __name__ == "__main__":
N = 2397
print(smallestNumber(N))
# This code is contributed by ita_c
C#
// C# program to print the smallest
// integer not less than N with all odd digits
using System;
public class GFG{
// function to return the smallest number
// with all digits odd
static int smallestNumber(int n) {
int num = 0;
String s = "";
int duplicate = n;
// convert the number to string to
// perform operations
while (n > 0) {
s = (char) (n % 10 + 48) + s;
n /= 10;
}
int index = -1;
// find out the first even number
for (int i = 0; i < s.Length; i++) {
int digit = s[i] - '0';
if ((digit & 1) == 0) {
index = i;
break;
}
}
// if no even numbers are there, than n is the answer
if (index == -1) {
return duplicate;
}
// add all digits till first even
for (int i = 0; i < index; i++) {
num = num * 10 + (s[i] - '0');
}
// increase the even digit by 1
num = num * 10 + (s[index] - '0' + 1);
// add 1 to the right of the even number
for (int i = index + 1; i < s.Length; i++) {
num = num * 10 + 1;
}
return num;
}
// Driver Code
static public void Main() {
int N = 2397;
Console.WriteLine(smallestNumber(N));
}
}
// This code is contributed by PrinciRaj1992
输出:
3111
时间复杂度: O(M),其中M是N中的位数。