📜  最大数字总和,范围为1到n

📅  最后修改于: 2021-04-26 06:58:11             🧑  作者: Mango

给定数字n,请找到一个介于1到n之间的数字,以使其总和最大。如果有几个这样的整数,请确定其中最大的一个。

例子 :

Input:  n = 100
Output: 99  
99 is the largest number in range from
1 to 100 with maximum sum of digits.

Input: n = 48
Output: 48
Explanation: 
There are two numbers with maximum
digit sum. The numbers are 48 and 39
Since 48 > 39, it is our answer.

天真的方法是对从1到n的所有数字进行迭代,并找出哪个数字的位数之和最大。该解决方案的时间复杂度为O(n)。

一种有效的方法是从n迭代到1。对当前数字的每个数字执行以下操作,如果数字不为零,则将其减少一位,然后将所有其他数字更改为右边的九个数字。如果结果整数中的位数之和严格大于当前答案的位数之和,则使用结果整数更新答案。如果结果整数的总和与当前答案相同,则如果结果整数大于当前答案,则用结果整数更新当前答案。

如何减少一个数字并将其右边的所有其他数字更改为9?
令x为当前数字。我们可以使用以下公式找到当前数字的下一个数字。在下面的公式中,b是10的幂代表当前数字的位置。每次迭代后,我们将x减小为x / 10并将b更改为b * 10。

我们使用(x – 1)* b +(b – 1);
此行可以进一步解释为,如果数字为x = 521且b = 1,则

  • (521 – 1)* 1 +(1-1)为您提供520,这是我们需要做的事情,将位置编号减少1,并将所有其他右侧的位置编号替换为9。
  • 在x / = 10给出x为52并且b * = 10给出b为10之后,再次执行(52-1)*(10)+ 9得到519,这是我们要做的,将当前索引减少1,并将所有其他权利增加9。
C++
// CPP program to find the 
// number with maximum digit
// sum.
#include 
using namespace std;
  
// function to calculate the  
// sum of digits of a number.
int sumOfDigits(int a)
{
    int sum = 0;
    while (a)
    {
        sum += a % 10;
        a /= 10;
    }
    return sum;
}
  
// Returns the maximum number 
// with maximum sum of digits.
int findMax(int x)
{
    // initializing b as 1 and 
    // initial max sum to be of n
    int b = 1, ans = x;
  
    // iterates from right to 
    // left in a digit
    while (x)
    {
  
        // while iterating this
        // is the number from 
        // from right to left
        int cur = (x - 1) * b + (b - 1);
  
        // calls the function to 
        // check if sum of cur is
        // more then of ans
        if (sumOfDigits(cur) > sumOfDigits(ans) || 
           (sumOfDigits(cur) == sumOfDigits(ans) && 
            cur > ans))
            ans = cur;
  
        // reduces the number to one unit less
        x /= 10;
        b *= 10;
    }
  
    return ans;
}
  
// driver program
int main()
{
    int n = 521; 
    cout << findMax(n); 
    return 0;
}


Java
// Java program to find the 
// number with maximum digit
// sum.
import java.io.*;
  
class GFG {
      
    // function to calculate the  
    // sum of digits of a number.   
    static int sumOfDigits(int a)
    {
        int sum = 0;
        while (a!=0)
        {
            sum += a % 10;
            a /= 10;
        }
        return sum;
    }
      
    // Returns the maximum number 
    // with maximum sum of digits.
    static int findMax(int x)
    {
        // initializing b as 1 and 
        // initial max sum to be of n
        int b = 1, ans = x;
      
        // iterates from right to 
        // left in a digit
        while (x!=0) 
        {
      
            // while iterating this
            // is the number from 
            // from right to left
            int cur = (x - 1) * b + (b - 1);
      
            // calls the function to 
            // check if sum of cur is
            // more then of ans
            if (sumOfDigits(cur) > sumOfDigits(ans) || 
            (sumOfDigits(cur) == sumOfDigits(ans) && 
                cur > ans))
                ans = cur;
      
            // reduces the number to one unit less
            x /= 10;
            b *= 10;
        }
      
        return ans;
    }
      
    // driver program 
    public static void main (String[] args)
    {
        int n = 521; 
        System.out.println(findMax(n));
    }
}
  
/*This article is contributed by Nikita Tiwari.*/


Python3
# Python 3 program to
# find the number 
# with maximum digit
# sum.
  
  
# function to calculate 
# the sum of digits of
# a number.
def sumOfDigits(a) :
    sm = 0
    while (a!=0) :
        sm = sm + a % 10
        a = a // 10
      
    return sm
      
# Returns the maximum number
# with maximum sum of digits.
def findMax(x) :
      
    # initializing b as 1
    # and initial max sum
    # to be of n
    b = 1
    ans = x
      
    # iterates from right
    # to left in a digit
    while (x!=0) :
        # while iterating this 
        # is the number from
        # right to left
        cur = (x - 1) * b + (b - 1)
          
        # calls the function to
        # check if sum of cur is
        # more then of ans
        if (sumOfDigits(cur) > sumOfDigits(ans) or
        (sumOfDigits(cur) == sumOfDigits(ans) and
            cur > ans)) :
                ans = cur
  
        # reduces the number
        # to one unit less
        x =x // 10
        b = b * 10
      
      
    return ans
      
# driver program to test the above function
n = 521
print(findMax(n))
  
# This article is contributed by Nikita Tiwari.


C#
// C# program to find the number
// with maximum digit sum.
using System;
  
class GFG {
       
    // function to calculate the  
    // sum of digits of a number.   
    static int sumOfDigits(int a)
    {
        int sum = 0;
        while (a!=0)
        {
            sum += a % 10;
            a /= 10;
        }
        return sum;
    }
       
    // Returns the maximum number 
    // with maximum sum of digits.
    static int findMax(int x)
    {
        // initializing b as 1 and 
        // initial max sum to be of n
        int b = 1, ans = x;
       
        // iterates from right to 
        // left in a digit
        while (x!=0) 
        {
       
            // while iterating this
            // is the number from 
            // from right to left
            int cur = (x - 1) * b + (b - 1);
       
            // calls the function to 
            // check if sum of cur is
            // more then of ans
            if (sumOfDigits(cur) > sumOfDigits(ans) || 
               (sumOfDigits(cur) == sumOfDigits(ans) && 
                                            cur > ans))
                ans = cur;
       
            // reduces the number to one unit less
            x /= 10;
            b *= 10;
        }
       
        return ans;
    }
       
    // driver program 
    public static void Main()
    {
        int n = 521; 
        Console.WriteLine(findMax(n));
    }
}
   
// This article is contributed by Anant Agarwal.


PHP
 sumOfDigits($ans) || 
           (sumOfDigits($cur) == sumOfDigits($ans) && 
                                        $cur > $ans))
            $ans = $cur;
  
        // reduces the number
        // to one unit less
        $x = (int)$x / 10;
        $b *= 10;
    }
  
    return $ans;
}
  
// Driver Code
$n = 521; 
echo findMax($n); 
  
// This code is contributed by ajit
?>


输出 :

499

时间复杂度: O(m)其中m是n中的位数。