📌  相关文章
📜  给定数字 N 中具有偶数位和的最小奇数

📅  最后修改于: 2021-09-07 02:26:02             🧑  作者: Mango

给定一个字符串str形式的大量数字。任务是通过从给定的字符串str 中删除零个或多个字符来找到其数字总和为偶数的最小奇数,其中可以重新排列数字。

例子

方法:
仔细观察,凭直觉,可以理解为最小的奇数的位数是2。而且这个数中的每一位都是奇数,因为两个奇数之和总是偶数。因此,解决这个问题的思路是遍历给定的字符串并将每个奇数存储在一个数组中。这个数组可以排序,前两位数字一起组成最小的奇数,其数字之和为偶数。

下面是上述方法的实现。

C++
// C++ program to find the smallest odd number
// with even sum of digits from the given number N
#include
using namespace std;
 
// Function to find the smallest odd number
// whose sum of digits is even from the given string
int smallest(string s)
{
    // Converting the given string
    // to a list of digits
    vector a(s.length());
    for(int i = 0; i < s.length(); i++)
        a[i] = s[i]-'0';
     
    // An empty array to store the digits
    vector b;
     
    // For loop to iterate through each digit
    for(int i = 0; i < a.size(); i++)
    {
         
        // If the given digit is odd then
        // the digit is appended to the array b
        if((a[i]) % 2 != 0)
            b.push_back(a[i]);
    }
             
    // Sorting the list of digits
    sort(b.begin(),b.end());
     
    // If the size of the list is greater than 1
    // then a 2 digit smallest odd number is returned
    // Since the sum of two odd digits is always even
    if(b.size() > 1)
        return (b[0])*10 + (b[1]);
     
    // Else, -1 is returned
    return -1;
}
     
// Driver code
int main()
{
    cout << (smallest("15470"));
}
 
// This code is contributed by Surendra_Gangwar


Java
// Java program to find the smallest
// odd number with even sum of digits
// from the given number N
import java.util.*;
class GFG{
     
// Function to find the smallest
// odd number whose sum of digits
// is even from the given string
public static int smallest(String s)
{
     
    // Converting the given string
    // to a list of digits
    int[] a = new int[s.length()];
    for(int i = 0; i < s.length(); i++)
        a[i] = s.charAt(i) - '0';
     
    // An empty array to store the digits
    Vector b = new Vector();
     
    // For loop to iterate through each digit
    for(int i = 0; i < a.length; i++)
    {
         
        // If the given digit is odd
        // then the digit is appended
        // to the array b
        if(a[i] % 2 != 0)
            b.add(a[i]);
    }
             
    // Sorting the list of digits
    Collections.sort(b);
     
    // If the size of the list is greater
    // than 1 then a 2 digit smallest odd
    // number is returned. Since the sum
    // of two odd digits is always even
    if(b.size() > 1)
        return (b.get(0)) * 10 + (b.get(1));
     
    // Else, -1 is returned
    return -1;
}
 
// Driver code
public static void main(String[] args)
{
    System.out.print(smallest("15470"));
}
}
 
// This code is contributed by divyeshrabadiya07


Python
# Python program to find the smallest odd number
# with even sum of digits from the given number N
 
# Function to find the smallest odd number
# whose sum of digits is even from the given string
def smallest(s):
     
    # Converting the given string
    # to a list of digits
    a = list(s)
     
    # An empty array to store the digits
    b = []
     
    # For loop to iterate through each digit
    for i in range(len(a)):
         
        # If the given digit is odd then
        # the digit is appended to the array b
        if(int(a[i])%2 != 0):
            b.append(a[i])
             
    # Sorting the list of digits
    b = sorted(b)
     
    # If the size of the list is greater than 1
    # then a 2 digit smallest odd number is returned
    # Since the sum of two odd digits is always even
    if(len(b)>1):
        return int(b[0])*10 + int(b[1])
     
    # Else, -1 is returned   
    return -1
 
 
# Driver code
if __name__ == "__main__":
    print(smallest("15470"))


C#
// C# program to find the smallest
// odd number with even sum of digits
// from the given number N
using System;
using System.Collections;
 
class GFG{
      
// Function to find the smallest
// odd number whose sum of digits
// is even from the given string
public static int smallest(string s)
{
     
    // Converting the given string
    // to a list of digits
    int[] a = new int[s.Length];
     
    for(int i = 0; i < s.Length; i++)
        a[i] = (int)(s[i] - '0');
      
    // An empty array to store the digits
    ArrayList b = new ArrayList();
      
    // For loop to iterate through each digit
    for(int i = 0; i < a.Length; i++)
    {
          
        // If the given digit is odd
        // then the digit is appended
        // to the array b
        if (a[i] % 2 != 0)
            b.Add(a[i]);
    }
              
    // Sorting the list of digits
    b.Sort();
      
    // If the size of the list is greater
    // than 1 then a 2 digit smallest odd
    // number is returned. Since the sum
    // of two odd digits is always even
    if (b.Count > 1)
        return ((int)b[0] * 10 + (int)b[1]);
      
    // Else, -1 is returned
    return -1;
}
  
// Driver code
public static void Main(string[] args)
{
    Console.Write(smallest("15470"));
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
15

时间复杂度: O(N),其中 N =字符串的长度。