📜  最大数字不大于N,且所有数字都是奇数

📅  最后修改于: 2021-04-27 20:05:47             🧑  作者: Mango

给定一个数字N ,任务是找到不大于N且其所有数字均为奇数的最大数字。
例子:

天真的方法:从N迭代到0 ,然后找到所有数字均为奇数的第一个数字。如果跳过偶数,仍然可以优化此方法,因为每个偶数的右边都有一个偶数位。
下面是上述方法的实现:

C++
// CPP program to print the largest integer
// not greater than N with all odd digits
#include 
using namespace std;
 
// Function to check if all digits
// of a number are odd
bool allOddDigits(int n)
{
    // iterate for all digits
    while (n) {
 
        // if digit is even
        if ((n % 10) % 2 == 0)
            return false;
        n /= 10;
    }
 
    // all digits are odd
    return true;
}
 
// function to return the largest number
// with all digits odd
int largestNumber(int n)
{
    if (n % 2 == 0)
        n--;
 
    // iterate till we find a
    // number with all digits odd
    for (int i = n;; i -= 2)
        if (allOddDigits(i))
            return i;
}
 
// Driver Code
int main()
{
    int N = 23;
    cout << largestNumber(N);
    return 0;
}


Java
// Java program to print the largest integer
// not greater than N with all odd digits
 
public class GFG{
 
        // Function to check if all digits
        // of a number are odd
        static boolean allOddDigits(int n)
        {
            // iterate for all digits
            while (n != 0) {
           
                // if digit is even
                if ((n % 10) % 2 == 0)
                    return false;
                n /= 10;
            }
           
            // all digits are odd
            return true;
        }
           
        // function to return the largest number
        // with all digits odd
        static int largestNumber(int n)
        {
            if (n % 2 == 0)
                n--;
           
            // iterate till we find a
            // number with all digits odd
            for (int i = n;; i -= 2)
                if (allOddDigits(i))
                    return i;
        }
 
     public static void main(String []args){
          
        int N = 23;
        System.out.println(largestNumber(N));
  
    }
    // This code is contributed by ANKITRAI1
      
}


Python3
# Python 3 program to print the largest
# integer not greater than N with all
# odd digits
 
# Function to check if all digits
# of a number are odd
def allOddDigits(n):
     
    # iterate for all digits
    while (n):
 
        # if digit is even
        if ((n % 10) % 2 == 0):
            return False
        n = int(n / 10)
 
    # all digits are odd
    return True
 
# function to return the largest
# number with all digits odd
def largestNumber(n):
    if (n % 2 == 0):
        n -= 1
 
    # iterate till we find a
    # number with all digits odd
    i = n
    while(1):
        if (allOddDigits(i)):
            return i
        i -= 2
 
# Driver Code
if __name__ =='__main__':
    N = 23
    print(largestNumber(N))
     
# This code is contributed by
# Shashank_Sharma


C#
// C# program to print the largest
// integer not greater than N with
// all odd digits
using System;
 
class GFG
{
 
// Function to check if all
// digits of a number are odd
static bool allOddDigits(int n)
{
    // iterate for all digits
    while (n != 0)
    {
 
        // if digit is even
        if ((n % 10) % 2 == 0)
            return false;
        n /= 10;
    }
 
    // all digits are odd
    return true;
}
 
// function to return the largest
// number with all digits odd
static int largestNumber(int n)
{
    if (n % 2 == 0)
        n--;
 
    // iterate till we find a
    // number with all digits odd
    for (int i = n;; i -= 2)
        if (allOddDigits(i))
            return i;
}
 
// Driver Code
public static void Main()
{
    int N = 23;
 
    Console.WriteLine(largestNumber(N));
}
}
 
// This code is contributed by anuj_67


PHP
 1)
    {
 
        // if digit is even
        if (($n % 10) % 2 == 0)
            return false;
        $n = (int)$n / 10;
    }
 
    // all digits are odd
    return true;
}
 
// function to return the largest
// number with all digits odd
function largestNumber($n)
{
    if ($n % 2 == 0)
        $n--;
 
    // iterate till we find a
    // number with all digits odd
    for ($i = $n;; $i= ($i - 2))
        if (allOddDigits($i))
            return $i;
}
 
// Driver Code
$N = 23;
echo largestNumber($N);
 
// This code is contributed by ajit
?>


Javascript


C++
// CPP program to print the largest integer
// not greater than N with all odd digits
#include 
using namespace std;
 
// function to return the largest number
// with all digits odd
int largestNumber(int n)
{
    string s = "";
    int duplicate = n;
 
    // convert the number to a string for
    // easy operations
    while (n) {
        s = char(n % 10 + 48) + s;
        n /= 10;
    }
 
    int index = -1;
 
    // find first even digit
    for (int i = 0; i < s.length(); i++) {
        if (((s[i] - '0') % 2 & 1) == 0) {
            index = i;
            break;
        }
    }
 
    // if no even digit, then N is the answer
    if (index == -1)
        return duplicate;
 
    int num = 0;
 
    // till first even digit, add all odd numbers
    for (int i = 0; i < index; i++)
        num = num * 10 + (s[i] - '0');
 
    // decrease 1 from the even digit
    num = num * 10 + (s[index] - '0' - 1);
 
    // add 9 in the rest of the digits
    for (int i = index + 1; i < s.length(); i++)
        num = num * 10 + 9;
 
    return num;
}
 
// Driver Code
int main()
{
    int N = 24578;
 
    cout << largestNumber(N);
 
    return 0;
}


Java
// Java program to print the largest integer
// not greater than N with all odd digits
 
class GFG
{
     
// function to return the largest number
// with all digits odd
static int largestNumber(int n)
{
    String s = "";
    int duplicate = n;
 
    // convert the number to a string for
    // easy operations
    while (n > 0)
    {
        s = (char)(n % 10 + 48) + s;
        n /= 10;
    }
 
    int index = -1;
 
    // find first even digit
    for (int i = 0; i < s.length(); i++)
    {
        if (((int)(s.charAt(i) - '0') % 2 & 1) == 0)
        {
            index = i;
            break;
        }
    }
 
    // if no even digit, then N is the answer
    if (index == -1)
        return duplicate;
 
    int num = 0;
 
    // till first even digit, add all odd numbers
    for (int i = 0; i < index; i++)
        num = num * 10 + (int)(s.charAt(i) - '0');
 
    // decrease 1 from the even digit
    num = num * 10 + ((int)s.charAt(index) - (int)('0') - 1);
 
    // add 9 in the rest of the digits
    for (int i = index + 1; i < s.length(); i++)
        num = num * 10 + 9;
 
    return num;
}
 
// Driver Code
public static void main (String[] args)
{
    int N = 24578;
 
    System.out.println(largestNumber(N));
}
}
 
// This code is contributed by mits


Python3
# Python3 program to print the largest integer
# not greater than N with all odd digits
 
# function to return the largest number
# with all digits odd
def largestNumber(n):
 
    s = ""
    duplicate = n
 
    # convert the number to a string for
    # easy operations
    while (n):
        s = chr(n % 10 + 48) + s
        n //= 10
 
    index = -1
 
    # find first even digit
    for i in range(len(s)):
        if (((ord(s[i]) -
              ord('0')) % 2 & 1) == 0):
            index = i
            break
         
    # if no even digit, then N is the answer
    if (index == -1):
        return duplicate
 
    num = 0
 
    # till first even digit,
    # add all odd numbers
    for i in range(index):
        num = num * 10 + (ord(s[i]) - ord('0'))
 
    # decrease 1 from the even digit
    num = num * 10 + (ord(s[index]) -
                      ord('0') - 1)
 
    # add 9 in the rest of the digits
    for i in range(index + 1, len(s)):
        num = num * 10 + 9
 
    return num
 
# Driver Code
N = 24578
print(largestNumber(N))
 
# This code is contributed by mohit kumar


C#
// C# program to print the largest integer
// not greater than N with all odd digits
using System;
 
class GFG
{
     
// function to return the largest number
// with all digits odd
static int largestNumber(int n)
{
    string s = "";
    int duplicate = n;
 
    // convert the number to a string for
    // easy operations
    while (n > 0)
    {
        s = (char)(n % 10 + 48) + s;
        n /= 10;
    }
 
    int index = -1;
 
    // find first even digit
    for (int i = 0; i < s.Length; i++)
    {
        if (((int)(s[i] - '0') % 2 & 1) == 0)
        {
            index = i;
            break;
        }
    }
 
    // if no even digit, then N is the answer
    if (index == -1)
        return duplicate;
 
    int num = 0;
 
    // till first even digit, add all odd numbers
    for (int i = 0; i < index; i++)
        num = num * 10 + (int)(s[i] - '0');
 
    // decrease 1 from the even digit
    num = num * 10 + ((int)s[index] - (int)('0') - 1);
 
    // add 9 in the rest of the digits
    for (int i = index + 1; i < s.Length; i++)
        num = num * 10 + 9;
 
    return num;
}
 
// Driver Code
static void Main()
{
    int N = 24578;
 
    Console.WriteLine(largestNumber(N));
}
}
 
// This code is contributed by mits


Javascript


输出:
19

时间复杂度: O(N)
高效的方法:我们可以通过将N中的第一个偶数数字减一,然后用最大的奇数数字(即9)替换所有其他数字来获得所需的数字。例如,如果N = 24578,X = 19999 。如果N中没有偶数,则N就是数字本身。
下面是上述方法的实现:

C++

// CPP program to print the largest integer
// not greater than N with all odd digits
#include 
using namespace std;
 
// function to return the largest number
// with all digits odd
int largestNumber(int n)
{
    string s = "";
    int duplicate = n;
 
    // convert the number to a string for
    // easy operations
    while (n) {
        s = char(n % 10 + 48) + s;
        n /= 10;
    }
 
    int index = -1;
 
    // find first even digit
    for (int i = 0; i < s.length(); i++) {
        if (((s[i] - '0') % 2 & 1) == 0) {
            index = i;
            break;
        }
    }
 
    // if no even digit, then N is the answer
    if (index == -1)
        return duplicate;
 
    int num = 0;
 
    // till first even digit, add all odd numbers
    for (int i = 0; i < index; i++)
        num = num * 10 + (s[i] - '0');
 
    // decrease 1 from the even digit
    num = num * 10 + (s[index] - '0' - 1);
 
    // add 9 in the rest of the digits
    for (int i = index + 1; i < s.length(); i++)
        num = num * 10 + 9;
 
    return num;
}
 
// Driver Code
int main()
{
    int N = 24578;
 
    cout << largestNumber(N);
 
    return 0;
}

Java

// Java program to print the largest integer
// not greater than N with all odd digits
 
class GFG
{
     
// function to return the largest number
// with all digits odd
static int largestNumber(int n)
{
    String s = "";
    int duplicate = n;
 
    // convert the number to a string for
    // easy operations
    while (n > 0)
    {
        s = (char)(n % 10 + 48) + s;
        n /= 10;
    }
 
    int index = -1;
 
    // find first even digit
    for (int i = 0; i < s.length(); i++)
    {
        if (((int)(s.charAt(i) - '0') % 2 & 1) == 0)
        {
            index = i;
            break;
        }
    }
 
    // if no even digit, then N is the answer
    if (index == -1)
        return duplicate;
 
    int num = 0;
 
    // till first even digit, add all odd numbers
    for (int i = 0; i < index; i++)
        num = num * 10 + (int)(s.charAt(i) - '0');
 
    // decrease 1 from the even digit
    num = num * 10 + ((int)s.charAt(index) - (int)('0') - 1);
 
    // add 9 in the rest of the digits
    for (int i = index + 1; i < s.length(); i++)
        num = num * 10 + 9;
 
    return num;
}
 
// Driver Code
public static void main (String[] args)
{
    int N = 24578;
 
    System.out.println(largestNumber(N));
}
}
 
// This code is contributed by mits

Python3

# Python3 program to print the largest integer
# not greater than N with all odd digits
 
# function to return the largest number
# with all digits odd
def largestNumber(n):
 
    s = ""
    duplicate = n
 
    # convert the number to a string for
    # easy operations
    while (n):
        s = chr(n % 10 + 48) + s
        n //= 10
 
    index = -1
 
    # find first even digit
    for i in range(len(s)):
        if (((ord(s[i]) -
              ord('0')) % 2 & 1) == 0):
            index = i
            break
         
    # if no even digit, then N is the answer
    if (index == -1):
        return duplicate
 
    num = 0
 
    # till first even digit,
    # add all odd numbers
    for i in range(index):
        num = num * 10 + (ord(s[i]) - ord('0'))
 
    # decrease 1 from the even digit
    num = num * 10 + (ord(s[index]) -
                      ord('0') - 1)
 
    # add 9 in the rest of the digits
    for i in range(index + 1, len(s)):
        num = num * 10 + 9
 
    return num
 
# Driver Code
N = 24578
print(largestNumber(N))
 
# This code is contributed by mohit kumar

C#

// C# program to print the largest integer
// not greater than N with all odd digits
using System;
 
class GFG
{
     
// function to return the largest number
// with all digits odd
static int largestNumber(int n)
{
    string s = "";
    int duplicate = n;
 
    // convert the number to a string for
    // easy operations
    while (n > 0)
    {
        s = (char)(n % 10 + 48) + s;
        n /= 10;
    }
 
    int index = -1;
 
    // find first even digit
    for (int i = 0; i < s.Length; i++)
    {
        if (((int)(s[i] - '0') % 2 & 1) == 0)
        {
            index = i;
            break;
        }
    }
 
    // if no even digit, then N is the answer
    if (index == -1)
        return duplicate;
 
    int num = 0;
 
    // till first even digit, add all odd numbers
    for (int i = 0; i < index; i++)
        num = num * 10 + (int)(s[i] - '0');
 
    // decrease 1 from the even digit
    num = num * 10 + ((int)s[index] - (int)('0') - 1);
 
    // add 9 in the rest of the digits
    for (int i = index + 1; i < s.Length; i++)
        num = num * 10 + 9;
 
    return num;
}
 
// Driver Code
static void Main()
{
    int N = 24578;
 
    Console.WriteLine(largestNumber(N));
}
}
 
// This code is contributed by mits

Java脚本


输出:
19999

时间复杂度: O(M),其中M是位数