📜  给定单位数字的整数之和,最多N

📅  最后修改于: 2021-05-28 03:44:07             🧑  作者: Mango

给定两个整数ND ,任务是查找从1N的所有整数之和,其单位数字为D。
例子:

天真的方法:

  • 1N遍历。
  • 如果数字的单位位数为D,则将数字加到总和上
  • 最后打印sum的值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long int
 
// Function to return the required sum
ll getSum(int n, int d)
{
    ll sum = 0;
    for (int i = 1; i <= n; i++) {
 
        // If the unit digit is d
        if (i % 10 == d)
            sum += i;
    }
    return sum;
}
 
// Driver code
int main()
{
    int n = 30, d = 3;
    cout << getSum(n, d);
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class solution
{
 
// Function to return the required sum
static long getSum(int n, int d)
{
    long sum = 0;
    for (int i = 1; i <= n; i++) {
 
        // If the unit digit is d
        if (i % 10 == d)
            sum += i;
    }
    return sum;
}
 
// Driver code
public static void main(String args[])
{
    int n = 30, d = 3;
    System.out.println(getSum(n, d));
    
}
}


Python3
# Python3 implementation of the approach
 
# Function to return the required sum
def getSum(n, d) :
    sum = 0;
    for i in range(n + 1) :
 
        # If the unit digit is d
        if (i % 10 == d) :
            sum += i
    return sum
 
# Driver code
if __name__ == "__main__" :
 
    n , d = 30, 3
    print(getSum(n, d))
 
# This code is contributed by Ryuga
C# // C# implementation of the approach
using System;
class gfg
{
  // Function to return the required sum
 public static int getSum(int n, int d)
 {
    int sum = 0;
    for (int i = 1; i <= n; i++) {

        // If the unit digit is d
        if (i % 10 == d)
            sum += i;
    }
    return sum;
}

// Driver code
 public static int Main()
 { 
    int n = 30, d = 3;
    Console.WriteLine( getSum(n, d));
    return 0;
 }
}


PHP


Javascript


C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long int
 
// Function to return the required sum
ll getSum(int n, int d)
{
    ll sum = 0;
    while (d <= n)
    {
        sum += d;
        d += 10;
    }
    return sum;
}
 
// Driver code
int main()
{
    int n = 30, d = 3;
    cout << getSum(n, d);
    return 0;
}


Java
// Java implementation of the approach
class Solution
{
 
  
// Function to return the required sum
static long getSum(int n, int d)
{
    long sum = 0;
    while (d <= n) {
        sum += d;
        d += 10;
    }
    return sum;
}
  
// Driver code
public static void main(String args[])
{
    int n = 30, d = 3;
    System.out.print(getSum(n, d));
     
}
}
//contributed by Arnab Kundu


Python3
# Python3 implementation of the approach
# Function to return the required sum
def getSum(n, d):
    sum = 0
    while (d <= n):
        sum += d
        d += 10
    return sum
 
# Driver code
n = 30
d = 3
print(getSum(n, d))
 
# This code is contributed
# by sahishelangia


C#
// C# implementation of the approach
using System;
class GFG
{
 
// Function to return the required sum
static long getSum(int n, int d)
{
    long sum = 0;
    while (d <= n)
    {
        sum += d;
        d += 10;
    }
    return sum;
}
 
// Driver code
public static void Main()
{
    int n = 30, d = 3;
    Console.Write(getSum(n, d));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP


Javascript


输出:
39

高效的方法:D 更新sum = sum + DD = D + 10 。最后打印总和
下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
#define ll long long int
 
// Function to return the required sum
ll getSum(int n, int d)
{
    ll sum = 0;
    while (d <= n)
    {
        sum += d;
        d += 10;
    }
    return sum;
}
 
// Driver code
int main()
{
    int n = 30, d = 3;
    cout << getSum(n, d);
    return 0;
}

Java

// Java implementation of the approach
class Solution
{
 
  
// Function to return the required sum
static long getSum(int n, int d)
{
    long sum = 0;
    while (d <= n) {
        sum += d;
        d += 10;
    }
    return sum;
}
  
// Driver code
public static void main(String args[])
{
    int n = 30, d = 3;
    System.out.print(getSum(n, d));
     
}
}
//contributed by Arnab Kundu

Python3

# Python3 implementation of the approach
# Function to return the required sum
def getSum(n, d):
    sum = 0
    while (d <= n):
        sum += d
        d += 10
    return sum
 
# Driver code
n = 30
d = 3
print(getSum(n, d))
 
# This code is contributed
# by sahishelangia

C#

// C# implementation of the approach
using System;
class GFG
{
 
// Function to return the required sum
static long getSum(int n, int d)
{
    long sum = 0;
    while (d <= n)
    {
        sum += d;
        d += 10;
    }
    return sum;
}
 
// Driver code
public static void Main()
{
    int n = 30, d = 3;
    Console.Write(getSum(n, d));
}
}
 
// This code is contributed
// by Akanksha Rai

的PHP


Java脚本


输出:
39

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。