计算一个数字可以用其数字的总和替换的次数,直到它仅包含一个数字并且数字可能非常大。
例子:
Input : 10
Output : 1
1 + 0 = 1, so only one times
an number can be replaced by its sum .
Input : 991
Output : 3
9 + 9 + 1 = 19, 1 + 9 = 10, 1 + 0 = 1
hence 3 times the number can be replaced
by its sum.
我们已经讨论了寻找一个数字的总和,直到总和变成一位数字。
这里的问题只是上述先前问题的扩展。在这里,我们只想计算一个数字可以用其总和替换的次数,直到它只包含一位数字为止。由于数字可能非常大,为避免溢出,我们将数字输入为字符串。因此,为了计算该值,我们使用一个名为suspend_sum的变量,在该变量中,我们反复计算字符串的位数之和,然后再次将该temporary_sum转换为字符串 。重复此过程,直到字符串长度变为1为止。为了更清楚地说明这一点,请考虑编号991
9 + 9 + 1 = 19,现在19是一个字符串
1 + 9 = 10,同样10是一个字符串
1 + 0 = 1。同样,1是一个字符串,但是这里的字符串长度是1,所以循环中断。
求和运算的数量是最终答案。
以下是此方法的实现。
C++
// C++ program to count number of times we
// need to add digits to get a single digit.
#include
using namespace std;
int NumberofTimes(string str)
{
// Here the count variable store
// how many times we do sum of
// digits and temporary_sum
// always store the temporary sum
// we get at each iteration .
int temporary_sum = 0, count = 0;
// In this loop we always compute
// the sum of digits in temporary_
// sum variable and convert it
// into string str till its length
// become 1 and increase the count
// in each iteration.
while (str.length() > 1)
{
temporary_sum = 0;
// computing sum of its digits
for (int i = 0; i < str.length(); i++)
temporary_sum += ( str[ i ] - '0' ) ;
// converting temporary_sum into string
// str again .
str = to_string(temporary_sum) ;
// increase the count
count++;
}
return count;
}
// Driver program to test the above function
int main()
{
string s = "991";
cout << NumberofTimes(s);
return 0;
}
Java
// Java program to count number of times we
// need to add digits to get a single digit.
public class GFG
{
static int NumberofTimes(String str)
{
// Here the count variable store
// how many times we do sum of
// digits and temporary_sum
// always store the temporary sum
// we get at each iteration .
int temporary_sum = 0, count = 0;
// In this loop we always compute
// the sum of digits in temporary_
// sum variable and convert it
// into string str till its length
// become 1 and increase the count
// in each iteration.
while (str.length() > 1)
{
temporary_sum = 0;
// computing sum of its digits
for (int i = 0; i < str.length(); i++)
temporary_sum += ( str.charAt(i) - '0' ) ;
// converting temporary_sum into string
// str again .
str = temporary_sum + "" ;
// increase the count
count++;
}
return count;
}
// Driver program to test above functions
public static void main(String[] args)
{
String s = "991";
System.out.println(NumberofTimes(s));
}
}
/* This code is contributed by Mr. Somesh Awasthi */
Python3
# Python 3 program to count number of times we
# need to add digits to get a single digit.
def NumberofTimes(s):
# Here the count variable store
# how many times we do sum of
# digits and temporary_sum
# always store the temporary sum
# we get at each iteration .
temporary_sum = 0
count = 0
# In this loop we always compute
# the sum of digits in temporary_
# sum variable and convert it
# into string str till its length
# become 1 and increase the count
# in each iteration.
while (len(s) > 1):
temporary_sum = 0
# computing sum of its digits
for i in range(len(s)):
temporary_sum += (ord(s[ i ]) -
ord('0'))
# converting temporary_sum into
# string str again .
s = str(temporary_sum)
# increase the count
count += 1
return count
# Driver Code
if __name__ == "__main__":
s = "991"
print(NumberofTimes(s))
# This code is contributed by Ita_c
C#
// C# program to count number of
// times we need to add digits to
// get a single digit.
using System;
class GFG {
// Function to count number of
// times we need to add digits
// to get a single digit
static int NumberofTimes(String str)
{
// Here the count variable store
// how many times we do sum of
// digits and temporary_sum
// always store the temporary sum
// we get at each iteration .
int temporary_sum = 0, count = 0;
// In this loop we always compute
// the sum of digits in temporary_
// sum variable and convert it
// into string str till its length
// become 1 and increase the count
// in each iteration.
while (str.Length > 1)
{
temporary_sum = 0;
// computing sum of its digits
for (int i = 0; i < str.Length; i++)
temporary_sum += (str[i] - '0');
// converting temporary_sum
// into string str again .
str = temporary_sum + "" ;
// increase the count
count++;
}
return count;
}
// Driver code
public static void Main()
{
String s = "991";
Console.Write(NumberofTimes(s));
}
}
// This code is contributed by Nitin Mittal.
PHP
1)
{
$temporary_sum = 0;
// computing sum of its digits
for ($i = 0; $i < strlen($str); $i++)
$temporary_sum += ($str[ $i ] - '0');
// converting temporary_sum into
// string str again .
$str = (string)($temporary_sum);
// increase the count
$count++;
}
return $count;
}
// Driver Code
$s = "991";
echo NumberofTimes($s);
// This code is contributed
// by Akanksha Rai
?>
Javascript
输出:
3