📌  相关文章
📜  需要以 7 结尾的最少数字数才能求和为给定数字

📅  最后修改于: 2021-10-27 07:23:25             🧑  作者: Mango

给定一个整数N ,任务是找到以7结尾的数字的最小计数,使得这些数字的总和为N
例子:

方法:

  • 这里的第一个观察是每个大于或等于70的数字总是可以写成所有以7结尾的数字的总和。例如,对于82 ,最后一位数字是2 ,因此至少需要 6 个以 7 结尾的数字,即 (7 * 6 = 42)。可以创建一个数组hasharr[] ,其中hasharr[i]表示所需的最小数字数,最后一位数字为7,因此结果总和的最后一位数字为i
  • 如果数字小于70,则必须检查N是否小于以数字 7 7结尾的最小数字的总和。如果是,则不可能并打印-1 ,否则如果它大于或等于可能。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
const int TEN = 10;
 
// Function to return the count of
// minimum numbers ending with 7
// required such that the sum
// of these numbers is n
int minCount(int n)
{
 
    // hasharr[i] will store the minimum
    // numbers ending with 7 so that it
    // sums to number ending with digit i
    int hasharr[TEN] = { 10, 3, 6, 9, 2, 5, 8, 1, 4, 7 };
 
    // Its always possible to write numbers > 69
    // to write as numbers ending with 7
    if (n > 69)
        return hasharr[n % TEN];
    else {
 
        // If the number is atleast equal to the
        // sum of minimum numbers ending with 7
        if (n >= hasharr[n % TEN] * 7)
            return (hasharr[n % TEN]);
        else
            return -1;
    }
}
 
// Driver code
int main()
{
    int n = 38;
 
    cout << minCount(n);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG {
     
// Function to return the count of
// minimum numbers ending with 7
// required such that the sum
// of these numbers is n
static int minCount(int n)
{
     
    // hasharr[i] will store the minimum
    // numbers ending with 7 so that it
    // sums to number ending with digit i
    int[] hasharr = { 10, 3, 6, 9, 2,
                       5, 8, 1, 4, 7 };
 
    // Its always possible to write 
    // numbers > 69 to write as
    // numbers ending with 7
    if (n > 69)
        return hasharr[n % 10];
    else
    {
         
        // If the number is atleast equal
        // to the sum of minimum numbers
        // ending with 7
        if (n >= hasharr[n % 10] * 7)
            return (hasharr[n % 10]);
        else
            return -1;
    }
}
 
// Driver code
public static void main (String[] args)
{
    int n = 38;
     
    System.out.println(minCount(n));
}
}
 
// This code is contributed by spp____


Python3
# Python3 implementation of the above approach
 
# Function to return the count of
# minimum numbers ending with 7
# required such that the sum
# of these numbers is n
def minCount(n):
     
    # hasharr[i] will store the minimum
    # numbers ending with 7 so that it
    # sums to number ending with digit i
    hasharr = [ 10, 3, 6, 9, 2,
                 5, 8, 1, 4, 7 ]
 
    # Its always possible to write 
    # numbers > 69 to write as
    # numbers ending with 7
    if (n > 69):
        return hasharr[n % 10]
    else:
         
        # If the number is atleast equal
        # to the sum of minimum numbers
        # ending with 7
        if (n >= hasharr[n % 10] * 7):
            return hasharr[n % 10]
        else:
            return -1
 
# Driver code
n = 38;
 
print(minCount(n))
 
# This code is contributed by spp____


C#
// C# implementation of the above approach
using System;
 
class GFG{
     
// Function to return the count of
// minimum numbers ending with 7
// required such that the sum
// of these numbers is n
static int minCount(int n)
{
     
    // hasharr[i] will store the minimum
    // numbers ending with 7 so that it
    // sums to number ending with digit i
    int[] hasharr = { 10, 3, 6, 9, 2,
                       5, 8, 1, 4, 7 };
 
    // Its always possible to write
    // numbers > 69 to write as
    // numbers ending with 7
    if (n > 69)
        return hasharr[n % 10];
    else
    {
 
        // If the number is atleast equal 
        // to the sum of minimum numbers
        // ending with 7
        if (n >= hasharr[n % 10] * 7)
            return (hasharr[n % 10]);
        else
            return -1;
    }
}
 
// Driver code
public static void Main (String[] args)
{
    int n = 38;
     
    Console.WriteLine(minCount(n));
}
}
 
// This code is contributed by spp____


Javascript


输出:
4

时间复杂度: O(1)

辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程