数字之和为十的第n个数

给定一个整数值n,求出其和为10的第n个正整数。 例如:

null
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 <bits/stdc++.h>
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" , 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

<?php
// Simple PHP program to find n-th
// number with sum of digits as 10.
function findNth( $n )
{
$count = 0;
for ( $curr = 1; ; $curr ++)
{
// Find sum of digits in
// current no.
$sum = 0;
for ( $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;
}
// Driver Code
echo findNth(5);
// This code is contributed by Sach .
?>


Javascript

<script>
// Simple JavaScript program to find n-th number
// with sum of digits as 10.
function findNth(n)
{
let count = 0;
for (let curr = 1;; curr++) {
// Find sum of digits in current no.
let sum = 0;
for (let x = curr; x > 0; x = Math.floor(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;
}
document.write(findNth(5));
// This code is contributed by Surbhi Tyagi.
</script>


输出

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 <bits/stdc++.h>
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" , 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

<?php
// Simple PHP program to find n-th
// number with sum of digits as 10.
function findNth( $n )
{
$count = 0;
for ( $curr = 19; ; $curr += 9)
{
// Find sum of digits in
// current no.
$sum = 0;
for ( $x = $curr ; $x > 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
?>


Javascript

<script>
// Simple Javascript program to find n-th
// number with sum of digits as 10.
function findNth(n)
{
let count = 0;
for (let curr = 19; ;curr += 9)
{
// Find sum of digits in
// current no.
let sum = 0;
for (let x = curr; x > 0;
x = parseInt(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
document.write(findNth(5));
// This code is contributed
// by gfgking
</script>


输出

55

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享