📌  相关文章
📜  通过用它们的和替换相邻的一对数字来最小化形成的数字

📅  最后修改于: 2022-05-13 01:56:05.392000             🧑  作者: Mango

通过用它们的和替换相邻的一对数字来最小化形成的数字

给定字符串s表示一个数字。任务是找到将s的两个连续数字替换为它们的和后可以形成的最小数字。

例子:

方法:这个问题可以通过使用贪心方法来解决。使用以下观察:

请按照以下步骤使用上述观察解决给定的问题。

  • 首先假设存在至少一对和小于 10的连续元素。
    • 从后面迭代数字字符串。
    • 找出总和小于10的第一对连续数字。
    • 用它们的总和替换它们并返回字符串
  • 现在,处理没有一对连续元素的总和小于 10的情况。
    • 从开始迭代数字字符串。
    • 和等于或大于10的第一对连续数字是最佳答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum number after
// doing given operation
void findSmallestNumber(string str, int N)
{
    string ans = "";
    bool found = false;
 
    // Iterate through the string in
    // reverse order
    for (int i = N - 1; i >= 1; i--) {
        int a = (str[i] - '0');
        int b = (str[i - 1] - '0');
        if (a + b < 10) {
            found = true;
            ans = str.substr(0, i - 1);
            int sum = a + b;
            int last = sum % 10;
            char l = last + '0';
            sum /= 10;
 
            if (sum != 0) {
                char p = sum + '0';
                ans.push_back(p);
            }
            ans.push_back(l);
            ans += str.substr(i + 1,
                              N - i - 1);
            cout << ans;
            break;
        }
    }
 
    if (!found) {
        int sum = (str[0] - '0')
                  + (str[1] - '0');
        ans = (char)(sum / 10 + '0');
        sum %= 10;
        ans += (sum + '0');
        ans += str.substr(2, N - 2);
        cout << ans;
    }
}
 
// Driver code
int main()
{
    string s = "56773";
    int N = s.length();
 
    // Function Call
    findSmallestNumber(s, N);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG
{
 
  // Function to find minimum number after
  // doing given operation
  static void findSmallestNumber(String str, int N)
  {
    String ans = "";
    boolean found = false;
 
    // Iterate through the string in
    // reverse order
    for (int i = N - 1; i >= 1; i--) {
      int a = (str.charAt(i) - '0');
      int b = (str.charAt(i-1)- '0');
      if (a + b < 10) {
        found = true;
        ans = str.substring(0, i - 1);
        int sum = a + b;
        int last = sum % 10;
        char l = (char)(last + '0');
        sum /= 10;
 
        if (sum != 0) {
          char p = (char)(sum + '0');
          ans += p;
        }
        ans += l;
        ans += str.substring(i + 1,
                             N - i - 1);
        System.out.println(ans);
        break;
      }
    }
 
    if (!found) {
      int sum = (str.charAt(0) - '0')
        + (str.charAt(1) - '0');
      Integer num = sum / 10;
      ans = num.toString();
      Integer summ = sum % 10;
      ans += summ.toString();
      ans += str.substring(2, N );
      System.out.println(ans);
    }
  }
 
// Driver Code
public static void main(String args[])
{
    String s = "56773";
    int N = s.length();
 
    // Function Call
    findSmallestNumber(s, N);
}
}
 
// This code is contributed by sanjoy_62.


Python3
# Python program for the above approach
 
# Function to find minimum number after
# doing given operation
def findSmallestNumber(str, N):
    ans = ""
    found = False
 
    # Iterate through the string in
    # reverse order
    for i in range(N - 1, 1, -1):
        a = (ord(str[i]) - ord('0'))
        b = (ord(str[i - 1]) - ord('0'))
        if (a + b < 10):
            found = True
            ans = str[0: i - 1]
            sum = a + b
            last = sum % 10
            l = (str)(last)
            sum = sum // 10
 
            if (sum != 0):
                p = (str)(sum)
                ans += p
 
            ans += (l)
            ans += str[i + 1: i + 1 + N - i - 1]
            print(ans)
            break
 
    if (not found):
        sum = (ord(str[0]) - ord('0')) + (ord(str[1]) - ord('0'))
        ans = f'{sum // 10}'
 
        sum %= 10
        ans += f'{sum}'
        ans += str[2: 2 + N - 2]
        print(ans)
 
# Driver code
s = "56773"
N = len(s)
 
# Function Call
findSmallestNumber(s, N)
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
using System.Collections;
class GFG
{
  // Function to find minimum number after
  // doing given operation
  static void findSmallestNumber(string str, int N)
  {
    string ans = "";
    bool found = false;
 
    // Iterate through the string in
    // reverse order
    for (int i = N - 1; i >= 1; i--) {
      int a = (str[i] - '0');
      int b = (str[i - 1] - '0');
      if (a + b < 10) {
        found = true;
        ans = str.Substring(0, i - 1);
        int sum = a + b;
        int last = sum % 10;
        char l = (char)(last + '0');
        sum /= 10;
 
        if (sum != 0) {
          char p = (char)(sum + '0');
          ans += p;
        }
        ans += l;
        ans += str.Substring(i + 1,
                             N - i - 1);
        Console.Write(ans);
        break;
      }
    }
 
    if (!found) {
      int sum = (str[0] - '0')
        + (str[1] - '0');
      int num = sum / 10;
      ans = num.ToString();
      sum %= 10;
      ans += sum.ToString();
      ans += str.Substring(2, N - 2);
      Console.Write(ans);
    }
  }
 
  // Driver code
  public static void Main()
  {
    string s = "56773";
    int N = s.Length;
 
    // Function Call
    findSmallestNumber(s, N);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
11773

时间复杂度: O(N) 其中 N 是字符串的大小
辅助空间: O(N)