📜  小于N且数字总和大于N的数字总和的最大数字

📅  最后修改于: 2021-05-06 19:22:02             🧑  作者: Mango

给定一个整数N ,任务是找到小于N的最大数字,以使它的数字之和大于N的数字之和。如果不满足任何条件,则打印-1
例子:

方法:N-11循环,然后检查任何数字的总和是否大于N的总和。满足条件的第一个数字是必需的数字。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the sum of the digits of n
int sumOfDigits(int n)
{
    int res = 0;
 
    // Loop for each digit of the number
    while (n > 0) {
        res += n % 10;
        n /= 10;
    }
 
    return res;
}
 
// Function to return the greatest
// number less than n such that
// the sum of its digits is greater
// than the sum of the digits of n
int findNumber(int n)
{
 
    // Starting from n-1
    int i = n - 1;
 
    // Check until 1
    while (i > 0) {
 
        // If i satisfies the given condition
        if (sumOfDigits(i) > sumOfDigits(n))
            return i;
        i--;
    }
 
    // If the condition is not satisfied
    return -1;
}
 
// Driver code
int main()
{
    int n = 824;
    cout << findNumber(n);
 
    return 0;
}


Java
//Java implementation of the approach
 
import java.io.*;
 
class GFG {
    // Function to return the sum of the digits of n
static int sumOfDigits(int n)
{
    int res = 0;
 
    // Loop for each digit of the number
    while (n > 0) {
        res += n % 10;
        n /= 10;
    }
 
    return res;
}
 
// Function to return the greatest
// number less than n such that
// the sum of its digits is greater
// than the sum of the digits of n
static int findNumber(int n)
{
 
    // Starting from n-1
    int i = n - 1;
 
    // Check until 1
    while (i > 0) {
 
        // If i satisfies the given condition
        if (sumOfDigits(i) > sumOfDigits(n))
            return i;
        i--;
    }
 
    // If the condition is not satisfied
    return -1;
}
 
// Driver code
    public static void main (String[] args) {
 
    int n = 824;
    System.out.println (findNumber(n));
    }
//This code is contributed by akt_mit   
}


Python3
# Python3 implementation of the approach
 
# Function to return the sum
# of the digits of n
def sumOfDigits(n) :
 
    res = 0;
 
    # Loop for each digit of the number
    while (n > 0) :
        res += n % 10
        n /= 10
 
    return res;
 
# Function to return the greatest
# number less than n such that
# the sum of its digits is greater
# than the sum of the digits of n
def findNumber(n) :
 
    # Starting from n-1
    i = n - 1;
 
    # Check until 1
    while (i > 0) :
 
        # If i satisfies the given condition
        if (sumOfDigits(i) > sumOfDigits(n)) :
            return i
             
        i -= 1
 
    # If the condition is not satisfied
    return -1;
 
# Driver code
if __name__ == "__main__" :
     
    n = 824;
    print(findNumber(n))
 
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
 
class GFG
{
// Function to return the sum
// of the digits of n
static int sumOfDigits(int n)
{
    int res = 0;
 
    // Loop for each digit of
    // the number
    while (n > 0)
    {
        res += n % 10;
        n /= 10;
    }
 
    return res;
}
 
// Function to return the greatest
// number less than n such that
// the sum of its digits is greater
// than the sum of the digits of n
static int findNumber(int n)
{
 
    // Starting from n-1
    int i = n - 1;
 
    // Check until 1
    while (i > 0)
    {
 
        // If i satisfies the given condition
        if (sumOfDigits(i) > sumOfDigits(n))
            return i;
        i--;
    }
 
    // If the condition is
    // not satisfied
    return -1;
}
 
// Driver code
static public void Main ()
{
    int n = 824;
    Console.WriteLine (findNumber(n));
}
}
 
// This code is contributed by @Tushil


PHP
 0)
    {
        $res += $n % 10;
        $n /= 10;
    }
 
    return $res;
}
 
// Function to return the greatest
// number less than n such that
// the sum of its digits is greater
// than the sum of the digits of n
function findNumber($n)
{
 
    // Starting from n-1
    $i = $n - 1;
 
    // Check until 1
    while ($i > 0)
    {
 
        // If i satisfies the given condition
        if (sumOfDigits($i) > sumOfDigits($n))
            return $i;
        $i--;
    }
 
    // If the condition is not satisfied
    return -1;
}
 
// Driver code
$n = 824;
 
echo findNumber($n);
     
// This code is contributed by Mukul singh
?>


Javascript


输出:
819