【1402】数字根

描述:

一个正整数的数字根是指该数字各位数字之和,如果和是一个个位数,那么这个数字就是它的数字根,如果和是个两位或多于两位的数字,那么就继续求和直到得到个位数。
例如:数字24,把24相加,得到6,那么6就是24的数字根;又比如数字39,把数字39相加,得到12,因为12时是两位数,所以继续把12相加,得到3,于是3就是39的数字根。
The digital root of a positive integer is found by summing the digits of theinteger. If the resulting value is a single digit then that digit is thedigital root. If the resulting value contains two or more digits, those digitsare summed and the process is repeated. This is continued as long as necessaryto obtain a single digit.
For example, consider the positive integer 24. Adding the 2 and the 4 yields avalue of 6. Since 6 is a single digit, 6 is the digital root of 24. Nowconsider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 isnot a single digit, the process must be repeated. Adding the 1 and the 2 yeilds3, a single digit and also the digital root of 39.

输入:

输入一个正整数,
input a positive integer,

输出:

输出它的数字根。
Output its digital root.

输入样例:

39

输出样例:

3


#include<iostream>
using namespace std;
int main()
{
	int n,a,s=0;
	cin>>n;
	while(n>=10)
	{
		a=n%10;
		n=n/10;
		s=s+a;
	}
	s=s+n;
	while(s>=10)
	{
		a=s%10;
		n=s/10;
		s=n+a;
	}
	cout<<s<<endl;
	return(0);
}


猜你喜欢

转载自blog.csdn.net/qq_40560275/article/details/78322351