📌  相关文章
📜  将数字分为两部分

📅  最后修改于: 2021-04-30 03:17:55             🧑  作者: Mango

给定一个包含数字4的整数N至少一次。任务是将数字分为x1x2两部分,以便:

  • x1 + x2 = N。
  • 并且所有部分都不包含数字4

请注意,可能会有多个答案。

例子:

方法:由于number可能太大,因此将number作为字符串。将其分为两个字符串:

  • 对于字符串1,找到它的字符串更改为3数字4的所有位置,我们也可以将其更改为另一个号码。
  • 对于第二个字符串,在数字4的所有位置放置1,在从数字4的第一个位置到字符串末尾的所有其余位置放置0。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print the two parts
void twoParts(string str)
{
    int flag = 0;
    string a = "";
  
    // Find the position of 4
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == '4') {
            str[i] = '3';
            a += '1';
            flag = 1;
        }
  
        // If current character is not '4'
        // but appears after the first
        // occurrence of '4'
        else if (flag)
            a += '0';
    }
  
    // Print both the parts
    cout << str << " " << a;
}
  
// Driver code
int main()
{
    string str = "9441";
    twoParts(str);
  
    return 0;
}


Java
// Java implementation of the approach 
class GfG
{
  
    // Function to print the two parts 
    static void twoParts(String str) 
    { 
        int flag = 0; 
        String a = "";
        char[] gfg = str.toCharArray();
      
        // Find the position of 4 
        for (int i = 0; i < str.length(); i++) 
        { 
            if (gfg[i] == '4') 
            { 
                gfg[i] = '3'; 
                a += '1'; 
                flag = 1; 
            } 
      
            // If current character is not '4' 
            // but appears after the first 
            // occurrence of '4' 
            else if (flag != 0) 
                a += '0'; 
        }
          
        str = new String(gfg);
      
        // Print both the parts 
        System.out.print(str + " " + a); 
    } 
  
    // Driver code
    public static void main(String []args)
    {
        String str = "9441"; 
        twoParts(str); 
    }
}
  
// This code is contributed by Rituraj Jain


Python3
# Python3 implementation of the approach 
  
# Function to print the two parts 
def twoParts(string) :
      
    flag = 0; 
    a = ""; 
  
    # Find the position of 4 
    for i in range(len(string)) :
          
        if (string[i] == '4') :
            string[i] = '3'; 
            a += '1'; 
            flag = 1; 
          
        # If current character is not '4' 
        # but appears after the first 
        # occurrence of '4' 
        elif (flag) :
            a += '0';
      
    string = "".join(string);
      
    # Print both the parts 
    print(string, a); 
  
# Driver code 
if __name__ == "__main__" : 
  
    string = "9441"; 
      
    twoParts(list(string)); 
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach 
using System;
  
class GfG
{
  
    // Function to print the two parts 
    static void twoParts(string str) 
    { 
        int flag = 0; 
        string a = "";
        char[] gfg = str.ToCharArray();
      
        // Find the position of 4 
        for (int i = 0; i < str.Length; i++) 
        { 
            if (gfg[i] == '4') 
            { 
                gfg[i] = '3'; 
                a += '1'; 
                flag = 1; 
            } 
      
            // If current character is not '4' 
            // but appears after the first 
            // occurrence of '4' 
            else if (flag != 0) 
                a += '0'; 
        }
          
        str = new String(gfg);
      
        // Print both the parts 
        Console.WriteLine(str + " " + a); 
    } 
  
    // Driver code
    static void Main()
    {
        string str = "9441"; 
        twoParts(str); 
    }
}
  
// This code is contributed by mits


PHP


输出:
9331 110