📜  第n个数字之和为10

📅  最后修改于: 2021-04-27 20:17:17             🧑  作者: Mango

给定一个整数值n,找出总和为10的第n个正整数。
例子:

Input: n = 2
Output: 28
The first number with sum of digits as
10 is 19. Second number is 28.

Input: 15
Output: 154

方法1(简单):
我们遍历所有数字。对于每个数字,我们找到数字的总和。当我们找到第n个数字总和为10的数字时,我们停止。

C++
// Simple CPP program to find n-th number
// with sum of digits as 10.
#include 
using namespace std;
 
int findNth(int n)
{
    int count = 0;
 
    for (int curr = 1;; curr++) {
 
        // Find sum of digits in current no.
        int sum = 0;
        for (int x = curr; x > 0; x = x / 10)
            sum = sum + x % 10;
 
        // If sum is 10, we increment count
        if (sum == 10)
            count++;
 
        // If count becomes n, we return current
        // number.
        if (count == n)
            return curr;
    }
    return -1;
}
 
int main()
{
    printf("%d\n", findNth(5));
    return 0;
}


Java
// Java program to find n-th number
// with sum of digits as 10.
 
import java.util.*;
import java.lang.*;
 
public class GFG {
    public static int findNth(int n)
    {
        int count = 0;
        for (int curr = 1;; curr++) {
 
            // Find sum of digits in current no.
            int sum = 0;
            for (int x = curr; x > 0; x = x / 10)
                sum = sum + x % 10;
 
            // If sum is 10, we increment count
            if (sum == 10)
                count++;
 
            // If count becomes n, we return current
            // number.
            if (count == n)
                return curr;
        }
    }
 
    public static void main(String[] args)
    {
        System.out.print(findNth(5));
    }
}
 
// Contributed by _omg


Python3
# Python3 program to find n-th number
# with sum of digits as 10.
import itertools
 
# function to find required number
def findNth(n):
 
    count = 0
 
    for curr in itertools.count():
        # Find sum of digits in current no.
        sum = 0
        x = curr
        while(x):
            sum = sum + x % 10
            x = x // 10
 
        # If sum is 10, we increment count
        if (sum == 10):
            count = count + 1
 
        # If count becomes n, we return current
        # number.
        if (count == n):
            return curr
 
    return -1
 
# Driver program
if __name__=='__main__':
    print(findNth(5))
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# program to find n-th number
// with sum of digits as 10.
using System;
 
class GFG {
    public static int findNth(int n)
    {
        int count = 0;
        for (int curr = 1;; curr++) {
 
            // Find sum of digits in current no.
            int sum = 0;
            for (int x = curr; x > 0; x = x / 10)
                sum = sum + x % 10;
 
            // If sum is 10, we increment count
            if (sum == 10)
                count++;
 
            // If count becomes n, we
            // return current number.
            if (count == n)
                return curr;
        }
    }
 
    // Driver Code
    static public void Main()
    {
        Console.WriteLine(findNth(5));
    }
}
 
// This code is contributed
// by Sach_Code


PHP
 0; $x = $x / 10)
            $sum = $sum + $x % 10;
 
        // If sum is 10, we increment
        // count
        if ($sum == 10)
            $count++;
 
        // If count becomes n, we return
        // current number.
        if ($count == $n)
            return $curr;
    }
    return -1;
}
 
// Driver Code
echo findNth(5);
 
// This code is contributed by Sach .
?>


C++
// Simple CPP program to find n-th number
// with sum of digits as 10.
#include 
using namespace std;
 
int findNth(int n)
{
    int count = 0;
 
    for (int curr = 19;; curr += 9) {
 
        // Find sum of digits in current no.
        int sum = 0;
        for (int x = curr; x > 0; x = x / 10)
            sum = sum + x % 10;
 
        // If sum is 10, we increment count
        if (sum == 10)
            count++;
 
        // If count becomes n, we return current
        // number.
        if (count == n)
            return curr;
    }
    return -1;
}
 
int main()
{
    printf("%d\n", findNth(5));
    return 0;
}


Java
// Java program to find n-th number
// with sum of digits as 10.
 
import java.util.*;
import java.lang.*;
 
public class GFG {
    public static int findNth(int n)
    {
        int count = 0;
 
        for (int curr = 19;; curr += 9) {
 
            // Find sum of digits in current no.
            int sum = 0;
            for (int x = curr; x > 0; x = x / 10)
                sum = sum + x % 10;
 
            // If sum is 10, we increment count
            if (sum == 10)
                count++;
 
            // If count becomes n, we return current
            // number.
            if (count == n)
                return curr;
        }
    }
 
    public static void main(String[] args)
    {
        System.out.print(findNth(5));
    }
}
 
// Contributed by _omg


Python3
# Python3 program to find n-th
# number with sum of digits as 10.
def findNth(n):
    count = 0;
     
    curr = 19;
 
    while (True):
 
        # Find sum of digits in
        # current no.
        sum = 0;
        x = curr;
        while (x > 0):
            sum = sum + x % 10;
            x = int(x / 10);
 
        # If sum is 10, we increment
        # count
        if (sum == 10):
            count+= 1;
 
        # If count becomes n, we return
        # current number.
        if (count == n):
            return curr;
         
        curr += 9;
 
    return -1;
 
# Driver Code
print(findNth(5));
 
# This code is contributed
# by mits


C#
// C# program to find n-th number
// with sum of digits as 10.
using System;
 
class GFG {
    public static int findNth(int n)
    {
        int count = 0;
 
        for (int curr = 19;; curr += 9) {
 
            // Find sum of digits in
            // current no.
            int sum = 0;
            for (int x = curr;
                 x > 0; x = x / 10)
                sum = sum + x % 10;
 
            // If sum is 10, we increment
            // count
            if (sum == 10)
                count++;
 
            // If count becomes n, we return
            // current number.
            if (count == n)
                return curr;
        }
    }
 
    // Driver Code
    static public void Main()
    {
        Console.WriteLine(findNth(5));
    }
}
 
// This code is contributed
// by Sach_Code


PHP
 0;
             $x = (int)$x / 10)
            $sum = $sum + $x % 10;
 
        // If sum is 10, we increment
        // count
        if ($sum == 10)
            $count++;
 
        // If count becomes n, we return
        // current number.
        if ($count == $n)
            return $curr;
    }
    return -1;
}
 
// Driver Code
echo findNth(5);
 
// This code is contributed
// by Sach_Code
?>


输出
55

方法2(高效):
如果我们仔细看一下,我们会发现9的所有倍数都以算术级数19、28、37、46、55、64、73、82、91、100、109等形式存在。
但是,上述系列中有些数字的总和不是10,例如100。因此,与其一一检查,不如从19开始,再以9递增。

C++

// Simple CPP program to find n-th number
// with sum of digits as 10.
#include 
using namespace std;
 
int findNth(int n)
{
    int count = 0;
 
    for (int curr = 19;; curr += 9) {
 
        // Find sum of digits in current no.
        int sum = 0;
        for (int x = curr; x > 0; x = x / 10)
            sum = sum + x % 10;
 
        // If sum is 10, we increment count
        if (sum == 10)
            count++;
 
        // If count becomes n, we return current
        // number.
        if (count == n)
            return curr;
    }
    return -1;
}
 
int main()
{
    printf("%d\n", findNth(5));
    return 0;
}

Java

// Java program to find n-th number
// with sum of digits as 10.
 
import java.util.*;
import java.lang.*;
 
public class GFG {
    public static int findNth(int n)
    {
        int count = 0;
 
        for (int curr = 19;; curr += 9) {
 
            // Find sum of digits in current no.
            int sum = 0;
            for (int x = curr; x > 0; x = x / 10)
                sum = sum + x % 10;
 
            // If sum is 10, we increment count
            if (sum == 10)
                count++;
 
            // If count becomes n, we return current
            // number.
            if (count == n)
                return curr;
        }
    }
 
    public static void main(String[] args)
    {
        System.out.print(findNth(5));
    }
}
 
// Contributed by _omg

Python3

# Python3 program to find n-th
# number with sum of digits as 10.
def findNth(n):
    count = 0;
     
    curr = 19;
 
    while (True):
 
        # Find sum of digits in
        # current no.
        sum = 0;
        x = curr;
        while (x > 0):
            sum = sum + x % 10;
            x = int(x / 10);
 
        # If sum is 10, we increment
        # count
        if (sum == 10):
            count+= 1;
 
        # If count becomes n, we return
        # current number.
        if (count == n):
            return curr;
         
        curr += 9;
 
    return -1;
 
# Driver Code
print(findNth(5));
 
# This code is contributed
# by mits

C#

// C# program to find n-th number
// with sum of digits as 10.
using System;
 
class GFG {
    public static int findNth(int n)
    {
        int count = 0;
 
        for (int curr = 19;; curr += 9) {
 
            // Find sum of digits in
            // current no.
            int sum = 0;
            for (int x = curr;
                 x > 0; x = x / 10)
                sum = sum + x % 10;
 
            // If sum is 10, we increment
            // count
            if (sum == 10)
                count++;
 
            // If count becomes n, we return
            // current number.
            if (count == n)
                return curr;
        }
    }
 
    // Driver Code
    static public void Main()
    {
        Console.WriteLine(findNth(5));
    }
}
 
// This code is contributed
// by Sach_Code

的PHP

 0;
             $x = (int)$x / 10)
            $sum = $sum + $x % 10;
 
        // If sum is 10, we increment
        // count
        if ($sum == 10)
            $count++;
 
        // If count becomes n, we return
        // current number.
        if ($count == $n)
            return $curr;
    }
    return -1;
}
 
// Driver Code
echo findNth(5);
 
// This code is contributed
// by Sach_Code
?>
输出
55