📌  相关文章
📜  最小位数,仅数字为4和7,且给定总和

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

幸运数字是正整数,其十进制表示形式仅包含幸运数字4和7。
最小的幸运数字的位数总和等于n。

例子:

Input : sum = 11
Output : 47
Sum of digits in 47 is 11 and 47
is the smallest number with given sum.

Input :  sum = 10
Output : -1    

该方法基于以下事实:

  1. 由于数字仅是4和7,因此给定的数字总和可以写为a * 4 + b * 7 =和,其中a和b是分别代表4s和7s数的一些正整数(大于或等于0)。
  2. 由于我们需要找到最小数目,因此结果总是以以下形式出现:首先是全4,然后是全7,即44…477…7。

我们基本上需要找到’a’和’b’的值。我们使用以下事实找到这些值:

  1. 如果总和是4的倍数,则结果全为4。
  2. 如果总和是7的倍数,则结果全为7。
  3. 如果总和不是4或7的倍数,那么我们可以减去其中之一,直到总和变成另一个的倍数。
C++
// C++ program to find smallest number
// with given sum of digits.
#include 
using namespace std;
 
// Prints minimum number with given digit
// sum and only allowed digits as 4 and 7.
void findMin(int sum)
{
    int a = 0, b = 0;
    while (sum > 0)
    {
        // Cases where all remaining digits
        // are 4 or 7 (Remaining sum of digits
        // should be multiple of 4 or 7)
        if (sum % 7 == 0)
        {
            b++;
            sum -= 7;
        }
        else if (sum % 4 == 0)
        {
            a++;
            sum -= 4;
        }
 
        // If both 4s and 7s are there
        // in digit sum, we subtract a 4.
        else
        {
            a++;
            sum -= 4;
        }
    }
 
    if (sum < 0)
    {
        printf("-1n");
        return;
    }
 
    for (int i=0; i


Java
// Java program to find smallest number
// with given sum of digits.
import java.io.*;
 
class GFG {
     
    // Prints minimum number with given digit
    // sum and only allowed digits as 4 and 7.
    static void findMin(int sum)
    {
        int a = 0, b = 0;
        while (sum > 0)
        {
            // Cases where all remaining digits
            // are 4 or 7 (Remaining sum of digits
            // should be multiple of 4 or 7)
            if (sum % 7 == 0)
            {
                b++;
                sum -= 7;
            }
            else if (sum % 4 == 0)
            {
                a++;
                sum -= 4;
            }
     
            // If both 4s and 7s are there
            // in digit sum, we subtract a 4.
            else
            {
                a++;
                sum -= 4;
            }
        }
     
        if (sum < 0)
        {
            System.out.print("-1n");
            return;
        }
     
        for (int i = 0; i < a; i++)
            System.out.print("4");
             
        for (int i = 0; i < b; i++)
            System.out.print("7");
             
        System.out.println();
    }
     
    // Driver code
    public static void main(String args[])
                            throws IOException
    {
        findMin(15);
    }
}
 
/* This code is contributed by Nikita tiwari.*/


Python
# Python program to find smallest number
# with given sum of digits.
 
# Prints minimum number with given digit
# sum and only allowed digits as 4 and 7.
def findMin(s):
    a, b = 0, 0
    while (s > 0):
         
        # Cases where all remaining digits
        # are 4 or 7 (Remaining sum of digits
        # should be multiple of 4 or 7)
        if (s % 7 == 0):
            b += 1
            s -= 7
        elif (s % 4 == 0):
            a += 1
            s -= 4
 
        # If both 4s and 7s are there
        # in digit sum, we subtract a 4.
        else:
            a += 1
            s -= 4
 
    string = ""
    if (s < 0):
        string = "-1"
        return string
     
     
    string += "4" * a
    string += "7" * b
     
    return string
 
# Driver code
print findMin(15)
 
# This code is contributed by Sachin Bisht


C#
// C# program to find smallest number
// with given sum of digits.
using System;
 
class GFG {
     
    // Prints minimum number with given digit
    // sum and only allowed digits as 4 and 7.
    static void findMin(int sum)
    {
        int a = 0, b = 0;
        while (sum > 0)
        {
             
            // Cases where all remaining digits
            // are 4 or 7 (Remaining sum of digits
            // should be multiple of 4 or 7)
            if (sum % 7 == 0)
            {
                b++;
                sum -= 7;
            }
            else if (sum % 4 == 0)
            {
                a++;
                sum -= 4;
            }
     
            // If both 4s and 7s are there
            // in digit sum, we subtract a 4.
            else
            {
                a++;
                sum -= 4;
            }
        }
     
        if (sum < 0)
        {
            Console.Write("-1n");
            return;
        }
     
        for (int i = 0; i < a; i++)
            Console.Write("4");
             
        for (int i = 0; i < b; i++)
            Console.Write("7");
             
        Console.WriteLine();
    }
     
    // Driver code
    public static void Main()
    {
        findMin(15);
    }
}
 
// This code is contributed by Nitin Mittal.


PHP
 0)
    {
         
        // Cases where all remaining digits
        // are 4 or 7 (Remaining sum of digits
        // should be multiple of 4 or 7)
        if ($sum % 7 == 0)
        {
            $b++;
            $sum -= 7;
        }
        else if ($sum % 4 == 0)
        {
            $a++;
            $sum -= 4;
        }
 
        // If both 4s and 7s are there
        // in digit sum, we subtract a 4.
        else
        {
            $a++;
            $sum -= 4;
        }
    }
 
    if ($sum < 0)
    {
        echo("-1n");
        return;
    }
 
    for ($i = 0; $i < $a; $i++)
        echo("4");
 
    for ($i = 0; $i < $b; $i++)
        echo("7");
 
    echo("\n");
}
 
    // Driver code
    findMin(15);
     
// This code is contributed by nitin mittal
?>


Javascript


输出:

447

时间复杂度: O(sum)。