📌  相关文章
📜  找到一个数字x,以使x及其数字的总和等于给定的n。

📅  最后修改于: 2021-04-29 17:04:02             🧑  作者: Mango

给定一个正数n。我们需要找到一个数字x,以使x与自身的位数之和等于n。
如果没有,则打印-1。

例子:

Input : n = 21
Output : x = 15
Explanation : x + its digit sum = 15 + 1 + 5 = 21 

Input : n = 5
Output : -1

我们从1迭代到n,对于每个中间数字x,找到其数字总和,然后将其加到x,如果等于n,则x将是我们所需的答案。

// iterate from 1 to n. For every no.
    // check if its digit sum with it is
    // equal to n.
    for (int i = 0; i <= n; i++)
        if (i + digSum(i)  == n)
            return i;

    return -1;
C++
// CPP program to find x such that x +
// digSum(x) is equal to n.
#include 
using namespace std;
  
// utility function for digit sum
int digSum(int n)
{
    int sum = 0, rem = 0;
    while (n) {
        rem = n % 10;
        sum += rem;
        n /= 10;
    }
    return sum;
}
  
// function for finding x
int findX(int n)
{
    // iterate from 1 to n. For every no.
    // check if its digit sum with it is
    // equal to n.
    for (int i = 0; i <= n; i++) 
        if (i + digSum(i) == n)
            return i;
      
    // if no such i found return -1
    return -1;
}
  
// driver function
int main()
{
    int n = 43;
    cout << "x = " << findX(n);
    return 0;
}


Java
// Java program to find x such that x +
// digSum(x) is equal to n.
class GFG
{
      
    // utility function for digit sum
    static int digSum(int n)
    {
        int sum = 0, rem = 0;
          
        while (n>0) {
            rem = n % 10;
            sum += rem;
            n /= 10;
        }
          
        return sum;
    }
      
    // function for finding x
    static int findX(int n)
    {
          
        // iterate from 1 to n. For every no.
        // check if its digit sum with it is
        // equal to n.
        for (int i = 0; i <= n; i++) 
            if (i + digSum(i) == n)
                return i;
          
        // if no such i found return -1
        return -1;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int n = 43;
          
        System.out.println("x = "+findX(n));
    }
}
  
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to find 
# x such that dx + igSum(x) 
# is equal to n.
  
# utility function 
# for digit sum
def digSum(n):
    sum = 0;
    rem = 0;
    while(n):
        rem = n % 10;
        sum = sum + rem;
        n = int(n / 10);
    return sum;
  
# function for finding x
def findX(n):
      
    # iterate from 1 to n.
    # For every no.
    # check if its digit
    # sum with it is# equal to n.
    for i in range(n + 1):
        if (i + digSum(i) == n):
            return i;
      
    # if no such i
    # found return -1
    return -1;
  
# Driver Code
n = 43;
print("x = ", findX(n));
  
# This code is contributed by mits


C#
// C# program to find x such that 
// x + digSum(x) is equal to n.
using System;
  
class GFG {
      
    // utility function for digit sum
    static int digSum(int n)
    {
        int sum = 0, rem = 0;
          
        while (n > 0) 
        {
            rem = n % 10;
            sum += rem;
            n /= 10;
        }
          
        return sum;
    }
      
    // function for finding x
    static int findX(int n)
    {
          
        // iterate from 1 to n. For every no.
        // check if its digit sum with it is
        // equal to n.
        for (int i = 0; i <= n; i++) 
            if (i + digSum(i) == n)
                return i;
          
        // if no such i found return -1
        return -1;
    }
      
    // Driver code
    public static void Main()
    {
        int n = 43;
          
        Console.Write("x = " + findX(n));
    }
}
  
// This code is contributed by vt_m.


PHP


输出:

x = 35