📌  相关文章
📜  加上9的最小数获得N

📅  最后修改于: 2021-05-08 18:39:45             🧑  作者: Mango

给定一个整数N ,任务是找到要加N所需的最小数量的数字。 (所有此类数字必须以9表示)

例子:

方法:

  • 检查N的一个数字,根据一个数字可以很容易地找到需要加的最小数字。
  • 如果一个人的数字是9:答案将作为1号码本身有9个为个位数的数字。
  • 如果一个人的数字是:
    1. 1: 9必须加9次,即(9 * 9 = 81)。
    2. 2: 9必须相加8次,即(9 * 8 = 72)。
    3. 3: 9必须相加7次,即(9 * 7 = 63)。
    4. 4: 9必须加6次,即(9 * 6 = 54)。
    5. 5: 9必须加5次,即(9 * 5 = 45)。
    6. 6: 9必须加4次,即(9 * 4 = 36)。
    7. 7: 9必须加3次,即(9 * 3 = 27)。
    8. 8: 9必须加2次,即(9 * 2 = 18)。
    9. 0: 9必须加10次,即(9 * 10 = 90)。
  • 此处观察到的是,仅需要为上述所有情况添加最小倍数。这是因为说一个数字为4时,可以使用6个数字中的所有9个(一个位),并且可以从N中减去结果,即为M。现在, M将在自己的位置为0 。因此,只需将5个数字用作9 ,将第六个数字用作(M + 9)即可

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find minimum count
// of numbers(with one's digit 9)
// that sum up to N
int findMin(int N)
{
    // Fetch one's digit
    int digit = N % 10;
 
    // Apply Cases mentioned in approach
    switch (digit) {
    case 0:
        if (N >= 90)
            return 10;
        break;
    case 1:
        if (N >= 81)
            return 9;
        break;
    case 2:
        if (N >= 72)
            return 8;
        break;
    case 3:
        if (N >= 63)
            return 7;
        break;
    case 4:
        if (N >= 54)
            return 6;
        break;
    case 5:
        if (N >= 45)
            return 5;
        break;
    case 6:
        if (N >= 36)
            return 4;
        break;
    case 7:
        if (N >= 27)
            return 3;
        break;
    case 8:
        if (N >= 18)
            return 2;
        break;
    case 9:
        if (N >= 9)
            return 1;
        break;
    }
 
    // If no possible answer exists
    return -1;
}
 
// Driver code
int main()
{
    int N = 27;
 
    cout << findMin(N);
}


Java
// Java implementation of the approach
class GFG
{
     
    // Function to find minimum count
    // of numbers(with one's digit 9)
    // that sum up to N
    static int findMin(int N)
    {
        // Fetch one's digit
        int digit = N % 10;
     
        // Apply Cases mentioned in approach
        switch (digit)
        {
            case 0:
                if (N >= 90)
                    return 10;
                break;
            case 1:
                if (N >= 81)
                    return 9;
                break;
            case 2:
                if (N >= 72)
                    return 8;
                break;
            case 3:
                if (N >= 63)
                    return 7;
                break;
            case 4:
                if (N >= 54)
                    return 6;
                break;
            case 5:
                if (N >= 45)
                    return 5;
                break;
            case 6:
                if (N >= 36)
                    return 4;
                break;
            case 7:
                if (N >= 27)
                    return 3;
                break;
            case 8:
                if (N >= 18)
                    return 2;
                break;
            case 9:
                if (N >= 9)
                    return 1;
                break;
        }
     
        // If no possible answer exists
        return -1;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int N = 27;
     
        System.out.println(findMin(N));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
 
# Function to find minimum count
# of numbers(with one's digit 9)
# that sum up to N
def findMin(N: int):
 
    # Fetch one's digit
    digit = N % 10
 
    # Apply Cases mentioned in approach
    if digit == 0 and N >= 90:
        return 10
    elif digit == 1 and N >= 81:
        return 9
    elif digit == 2 and N >= 72:
        return 8
    elif digit == 3 and N >= 63:
        return 7
    elif digit == 4 and N >= 54:
        return 6
    elif digit == 5 and N >= 45:
        return 5
    elif digit == 6 and N >= 36:
        return 4
    elif digit == 7 and N >= 27:
        return 3
    elif digit == 8 and N >= 18:
        return 2
    elif digit == 9 and N >= 9:
        return 1
 
    # If no possible answer exists
    return -1
 
# Driver Code
if __name__ == "__main__":
    N = 27
    print(findMin(N))
 
# This code is contributed by
# sanjeev2552


C#
// C# implementation of the approach
using System;        
     
class GFG
{
     
    // Function to find minimum count
    // of numbers(with one's digit 9)
    // that sum up to N
    static int findMin(int N)
    {
        // Fetch one's digit
        int digit = N % 10;
     
        // Apply Cases mentioned in approach
        switch (digit)
        {
            case 0:
                if (N >= 90)
                    return 10;
                break;
            case 1:
                if (N >= 81)
                    return 9;
                break;
            case 2:
                if (N >= 72)
                    return 8;
                break;
            case 3:
                if (N >= 63)
                    return 7;
                break;
            case 4:
                if (N >= 54)
                    return 6;
                break;
            case 5:
                if (N >= 45)
                    return 5;
                break;
            case 6:
                if (N >= 36)
                    return 4;
                break;
            case 7:
                if (N >= 27)
                    return 3;
                break;
            case 8:
                if (N >= 18)
                    return 2;
                break;
            case 9:
                if (N >= 9)
                    return 1;
                break;
        }
     
        // If no possible answer exists
        return -1;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int N = 27;
     
        Console.WriteLine(findMin(N));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3