📌  相关文章
📜  与N的乘积等于N的数的总和的最小数字

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

给定一个整数N,任务是找到最小的正整数,当其乘以N,有总和的数字等于N个数字之和。

例子:

方法:按照以下步骤解决问题:

  1. 由于N可以很大,因此将N的输入作为字符串。计算N的数字总和并将其存储在变量中,例如S。
  2. 由于答案需要从数字11开始超过10,因此将其乘以N并将其存储在变量中,例如res
  3. 计算res的位数之和,并检查res的位数之和是否等于S。如果发现为真,则打印整数。

下面是上述方法的实现:

C
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum
// integer having sum of digits
// of a number multiplied by n
// equal to sum of digits of n
void find_num(string n)
{
   
    // Initialize answer
    int ans = 0;
    int sumOfDigitsN = 0;
   
    // Find sum of digits of N
    for(int c = 0; c < n.length(); c++)
    {
        sumOfDigitsN += n - '0';
         
    }
    int x=11;
       
    while(true)
    {
        // Multiply N with x
        int newNum = x * stoi(n);
        int tempSumDigits = 0;
        string temp = to_string(newNum);
       
        // Sum of digits of the new number
        for(int c = 0; c < temp.length(); c++)
        {
            tempSumDigits += temp - '0';
        }
       
        // If condition satisfies
        if (tempSumDigits == sumOfDigitsN)
        {
            ans = x;
            break;
        }
        //increase x
          x++;
    }
   
    // Print answer
    cout << ans << endl;
}
 
// Driver Code
int main()
{
    string N = "3029";
   
    // Funciton call
    find_num(N);
    return 0;
}
 
// This code is contributed by Sunil Sakariya


Java
// Java program for the above approach
 
class GFG {
 
    // Function to find the minimum
    // integer having sum of digits
    // of a number multiplied by n
    // equal to sum of digits of n
    static void find_num(String n)
    {
        // Initialize answer
        int ans = 0;
 
        // Convert string to
        // character array
        char[] digitsOfN
            = n.toCharArray();
 
        int sumOfDigitsN = 0;
 
        // Find sum of digits of N
        for (char c : digitsOfN) {
 
            sumOfDigitsN
                += Integer.parseInt(
                    Character.toString(c));
        }
 
        for (int x = 11; x > 0; x++) {
 
            // Multiply N with x
            int newNum
                = x * Integer.parseInt(n);
 
            int tempSumDigits = 0;
 
            char[] temp
                = Integer.toString(
                             newNum)
                      .toCharArray();
 
            // Sum of digits of the new number
            for (char c : temp) {
                tempSumDigits
                    += Integer.parseInt(
                        Character.toString(c));
            }
 
            // If condition satisfies
            if (tempSumDigits == sumOfDigitsN) {
                ans = x;
                break;
            }
        }
 
        // Print answer
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String N = "3029";
 
        // Funciton call
        find_num(N);
    }
}


Python3
# Python3 program for the above approach
 
# Function to find the minimum
# integer having sum of digits
# of a number multiplied by n
# equal to sum of digits of n
def find_num(n):
     
    # Initialize answer
    ans = 0
 
    # Convert string to
    # character array
    digitsOfN = str(n)
 
    sumOfDigitsN = 0
 
    # Find sum of digits of N
    for c in digitsOfN:
        sumOfDigitsN += int(c)
 
    for x in range(11, 50):
         
        # Multiply N with x
        newNum = x * int(n)
 
        tempSumDigits = 0
 
        temp = str(newNum)
 
        # Sum of digits of the new number
        for c in temp:
            tempSumDigits += int(c)
        #print(tempSumDigits,newNum)
 
        # If condition satisfies
        if (tempSumDigits == sumOfDigitsN):
            ans = x
            break
 
    # Print answer
    print(ans)
 
# Driver Code
if __name__ == '__main__':
     
    N = "3029"
 
    # Funciton call
    find_num(N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach 
using System;
using System.Globalization;
 
class GFG{
  
// Function to find the minimum
// integer having sum of digits
// of a number multiplied by n
// equal to sum of digits of n
static void find_num(string n)
{
     
    // Initialize answer
    int ans = 0;
 
    // Convert string to
    // character array
    char[] digitsOfN = n.ToCharArray();
 
    int sumOfDigitsN = 0;
 
    // Find sum of digits of N
    foreach(char c in digitsOfN)
    {
        sumOfDigitsN += Int32.Parse(
                Char.ToString(c));
    }
 
    for(int x = 11; x > 0; x++)
    {
         
        // Multiply N with x
        int newNum = x * Int32.Parse(n);
 
        int tempSumDigits = 0;
         
        string str = newNum.ToString();
        char[] temp = str.ToCharArray();
 
        // Sum of digits of the new number
        foreach(char c in temp)
        {
            tempSumDigits += Int32.Parse(
                    Char.ToString(c));
        }
 
        // If condition satisfies
        if (tempSumDigits == sumOfDigitsN)
        {
            ans = x;
            break;
        }
    }
 
    // Print answer
    Console.WriteLine(ans);
}
 
// Driver Code
public static void Main()
{
    string N = "3029";
     
    // Funciton call
    find_num(N);
}
}
 
// This code is contributed by susmitakundugoaldanga


输出:
37

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