您的位置:首页 > 其它

Digital Roots数字根 12

2015-11-25 20:18 393 查看
Digital Roots数字根
Problem Description
The digital rootof a positive integer is found by summing the digits of the integer. If theresulting value is a single digit then that digit is the digital root. If theresulting value contains two or more digits,
those digits are summed and theprocess is repeated. This is continued as long as necessary to obtain a singledigit.

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.
一个正整数的数字根总结了该整数的位数找到。如果结果值是一位数字,然后该数字是数字根。如果结果值包含两个或多个数字,这些数字相加并重复该过程。这是,只要有必要继续获得单个数字。
例如,考虑的正整数24.添加的2和4产生的值为6。由于6是一位数字,6是24.数字根现在考虑正整数39.添加3和9的产量12.自12不是一个单独的数字,该过程必须重复。添加1和2 yeilds3,一个单一的数字,也为39的数字根。

Input
The input filewill contain a list of positive integers, one per line. The end of the inputwill be indicated by an integer value of zero.
输入文件将包含正整数,每行一个的列表。输入的结束将通过0整数值表示

Output
For each integerin the input, output its digital root on a separate line of the output.
对于输入,输出它的数字根对输出的一个单独的行每个整数。
Sample Input
24
39
0

Sample Output
6
3
代码如下:
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int divide(int b);
void main(){
	int a,temp;
	do{
	scanf("%d",&a);
	temp=divide(a);	
	if(temp>10){
	 temp=divide(temp);
	 printf("%d的数字根是%d\n",a,temp);
	}else{
		printf("%d的数字根是%d\n",a,temp);
	 }
	}while(a!=0);
}
int divide(int b){
	int m,n,sum;
	m=b%10;
	n=b/10%10;
	sum=m+n;
	return sum;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: