求一个数的位数之和,直到和变成一位数

给定一个数字 N ,我们需要找到它的数字之和,以便:

null
If n < 10        digSum(n) = nElse             digSum(n) = Sum(digSum(n))

例如:

Input : 1234Output : 1Explanation : The sum of 1+2+3+4 = 10,               digSum(x) == 10              Hence ans will be 1+0 = 1Input : 5674Output : 4 

A. 蛮力 方法是将所有数字相加,直到总和小于10。 流程图:

图片[1]-求一个数的位数之和,直到和变成一位数-yiteyi-C++库

下面是寻找总和的蛮力程序。

C++

// C++ program to find sum of
// digits of a number until
// sum becomes single digit.
#include<bits/stdc++.h>
using namespace std;
int digSum( int n)
{
int sum = 0;
// Loop to do sum while
// sum is not less than
// or equal to 9
while (n > 0 || sum > 9)
{
if (n == 0)
{
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
return sum;
}
// Driver program to test the above function
int main()
{
int n = 1234;
cout << digSum(n);
return 0;
}


JAVA

// Java program to find sum of
// digits of a number until
// sum becomes single digit.
import java.util.*;
public class GfG {
static int digSum( int n)
{
int sum = 0 ;
// Loop to do sum while
// sum is not less than
// or equal to 9
while (n > 0 || sum > 9 )
{
if (n == 0 ) {
n = sum;
sum = 0 ;
}
sum += n % 10 ;
n /= 10 ;
}
return sum;
}
// Driver code
public static void main(String argc[])
{
int n = 1234 ;
System.out.println(digSum(n));
}
}
// This code is contributed by Gitanjali.


python

# Python program to find sum of
# digits of a number until
# sum becomes single digit.
import math
# method to find sum of digits
# of a number until sum becomes
# single digit
def digSum( n):
sum = 0
while (n > 0 or sum > 9 ):
if (n = = 0 ):
n = sum
sum = 0
sum + = n % 10
n / = 10
return sum
# Driver method
n = 1234
print (digSum(n))
# This code is contributed by Gitanjali.


C#

// C# program to find sum of
// digits of a number until
// sum becomes single digit.
using System;
class GFG {
static int digSum( int n)
{
int sum = 0;
// Loop to do sum while
// sum is not less than
// or equal to 9
while (n > 0 || sum > 9)
{
if (n == 0)
{
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
return sum;
}
// Driver code
public static void Main()
{
int n = 1234;
Console.Write(digSum(n));
}
}
// This code is contributed by nitin mittal


PHP

<?php
// PHP program to find sum of
// digits of a number until
// sum becomes single digit.
function digSum( $n )
{
$sum = 0;
// Loop to do sum while
// sum is not less than
// or equal to 9
while ( $n > 0 || $sum > 9)
{
if ( $n == 0)
{
$n = $sum ;
$sum = 0;
}
$sum += $n % 10;
$n = (int) $n / 10;
}
return $sum ;
}
// Driver Code
$n = 1234;
echo digSum( $n );
// This code is contributed
// by aj_36
?>


Javascript

<script>
// Javascript program to find sum of
// digits of a number until
// sum becomes single digit.
let n = 1234;
//Function to get sum of digits
function getSum(n) {
let sum = 0;
while (n > 0 || sum > 9) {
if (n == 0) {
n = sum;
sum = 0;
}
sum = sum + n % 10;
n = Math.floor(n / 10);
}
return sum;
}
//function call
document.write(getSum(n));
//This code is contributed by Surbhi Tyagi
</script>


输出:

1

所以,另一个挑战是“你能在O(1)运行时不使用任何循环/递归吗?”

对 存在一个 简单优雅的O(1)解决方案 也为了这个。答案很简单:-

If n == 0   return 0;If n % 9 == 0          digSum(n) = 9Else                   digSum(n) = n % 9 

上述逻辑是如何运作的?

这种方法背后的逻辑是:

要检查一个数字是否可被9整除,请将数字的位数相加,然后检查总和是否可被9整除。如果是,那么这个数字可以被9整除,否则不是。

我们取27,即(2+7=9),因此可以被9整除。 如果一个数n可以被9整除,那么它的数字之和直到变成一个数字时总是9。例如 设n=2880 数字之和=2+8+8=18:18=1+8=9

因此 数字的形式可以是9x或9x+k。对于第一种情况,答案总是9。对于第二种情况,始终是k,这是剩下的余数。

这个问题被广泛称为数字根问题。

你可能会发现这篇维基百科文章很有用。-> https://en.wikipedia.org/wiki/Digital_root

以下是上述理念的实施情况:

C++

#include<bits/stdc++.h>
using namespace std;
int digSum( int n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
// Driver program to test the above function
int main()
{
int n = 9999;
cout<<digSum(n);
return 0;
}


JAVA

import java.io.*;
class GFG {
static int digSum( int n)
{
if (n == 0 )
return 0 ;
return (n % 9 == 0 ) ? 9 : (n % 9 );
}
// Driver program to test the above function
public static void main (String[] args)
{
int n = 9999 ;
System.out.println(digSum(n));
}
}
// This code is contributed by anuj_67.


Python3

def digSum(n):
if (n = = 0 ):
return 0
if (n % 9 = = 0 ):
return 9
else :
return (n % 9 )
# Driver program to test the above function
n = 9999
print (digSum(n))
# This code is contributed by
# Smitha Dinesh Semwal


C#

using System;
class GFG
{
static int digSum( int n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
// Driver Code
public static void Main ()
{
int n = 9999;
Console.Write(digSum(n));
}
}
// This code is contributed by aj_36


PHP

<?php
function digSum( $n )
{
if ( $n == 0)
return 0;
return ( $n % 9 == 0) ? 9 : ( $n % 9);
}
// Driver program to test the above function
$n = 9999;
echo digSum( $n );
//This code is contributed by anuj_67.
?>


Javascript

<script>
function digSum(n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
// Driver code
n = 9999;
document.write(digSum(n));
// This code is contributed by code_hunt
</script>


输出:

9

相关帖子: https://www.geeksforgeeks.org/digital-rootrepeated-digital-sum-given-integer/

本文由 阿尤什·坎杜里 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

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