📌  相关文章
📜  最接近N的最小数字仅由奇数位组成

📅  最后修改于: 2021-05-04 07:11:16             🧑  作者: Mango

给定整数N ,任务是找到最接近N的仅具有奇数位的数字。如果存在多个这样的数字,请打印最小值。

例子:

天真的方法:解决问题的最简单方法是迭代并找到小于和大于N的最接近的数字,仅将奇数作为其数字。请按照以下步骤解决问题:

  1. 如果N的所有数字都是奇数,则打印N作为答案。
  2. 找到最接近的较小和较大的数字,然后返回与N最小的数字。如果发现两者都与N等距,则打印较小的数字。

时间复杂度:O(10 (len(N)– 1) )
辅助空间: O(1)

高效方法:请按照以下步骤优化上述方法:

  1. 如果N的所有数字都是奇数,则返回N。
  2. 通过以下步骤有效地计算最接近N的较小数字:
    • 从左到右找到N中第一个偶数位的位置,即pos
    • 如果第一个偶数为0 ,则将0替换为9 ,然后遍历所有前面的数字;对于每个数字,如果找到的是1 ,则将其替换为9 。否则,将数字减2并返回数字。
    • 如果没有发现前面的数字超过1 ,则将最高有效数字替换为0
    • 如果第一个偶数数字不为零,则将该数字减1。将所有后续数字替换为9
  3. 通过以下步骤计算最接近N的较大数:
    • 找到第一个偶数数字的位置,即pos
    • pos处的数字加1。
    • 遍历后续数字并将其递增1。
  4. 将获得的各个最接近的数字的绝对差与N进行比较,并打印出差异最小的数字。
  5. 如果发现两个差异相等,则打印较小的数字。

下面是上述方法的实现:

Python
# Python program to implement
# the above approach
  
# Function to return the smaller
# number closest to N made up of
# only odd digits
def closest_smaller(N):
  
    N = str(N)
  
    l = list(map(int, N))
    length = len(l)
      
    # Stores the position of
    # first even digit of N
    pos = -1
  
    # Iterate through each digit of N
    for i in range(length):
          
        # Check for even digit.
        if l[i] % 2 == 0:
            pos = i
            break
  
    # If the first even digit is 0
    if l[pos] == 0:
  
        # Replace 0 with 9
        l[pos] = 9
  
        # Iterate over preceding
        # digits
        for i in range(pos - 1, -1, -1):
  
            # If current digit is 1
            if l[i] == 1:
  
                # Check if it is the 
                # first digit or not
                if i == 0:
                      
                    # Append leading 0's
                    l[i] = 0
                    break
                      
                # Otherwise, replace by 9
                l[i] = 9
                  
            # Otherwise
            else:
  
                # Decrease its value by 2
                l[i] -= 2
                break
  
    # If the first even digit exceeds 0
    else:
          
        # Reduce the digit by 1
        l[pos] -= 1
  
    # Replace all succeeding digits by 9
    for i in range(pos + 1, length):
        l[i] = 9
  
    # Remove leading 0s
    if l[0] == 0:
        l.pop(0)
      
      
    result = ''.join(map(str, l))
    return result
      
# Function to return the greater
# number closest to N made up of
# only odd digits
def closest_greater(N):
  
    N = str(N)
  
    l = list(map(int, N))
    length = len(l)
  
    # Stores the position of
    # first even digit of N
    pos = -1
  
    # Iterate over each digit
    # of N
    for i in range(length):
          
        # If even digit is found
        if l[i] % 2 == 0:
            pos = i
            break
              
    # Increase value of first 
    # even digit by 1
    l[pos] += 1
  
    for i in range(pos + 1, length):
        l[i] = 1
  
    result = ''.join(map(str, l))
    return result
  
# Function to check if all 
# digits of N are odd or not
def check_all_digits_odd(N):
  
    N = str(N)
  
    l = list(map(int, N))
    length = len(l)
  
    # Stores the position of
    # first even digit of N
    pos = -1
  
    # Iterating over each digit
    # of N
    for i in range(length):
          
        # If even digit is found
        if l[i] % 2 == 0:
            pos = i
            break
              
    # If no even digit is found
    if pos == -1:
        return True
    return False
  
# Function to return the 
# closest number to N
# having odd digits only
def closestNumber(N):
      
    # If all digits of N are odd
    if check_all_digits_odd(N):
        print(N)
    else:
          
        # Find smaller number 
        # closest to N
        l = int(closest_smaller(N))
          
        # Find greater number 
        # closest to N
        r = int(closest_greater(N))
          
        # Print the number with least
        # absolute difference
        if abs(N - l) <= abs(N - r):
            print(l)
        else:
            print(r)
  
# Driver Code.
if __name__ == '__main__':
      
    N = 110
    closestNumber(N)


输出:
111

时间复杂度: O(len(N))
辅助空间: O(len(N))