什么是“Digital Roots"?

来源:互联网 发布:黑人歧视黄种人知乎 编辑:程序博客网 时间:2024/05/16 17:35

 

/*
*Digital Roots
*
*Background
*The digital root of a positive integer is found by summing the digits of the int
*eger. If the resulting value is a single digit then that digit is the digital ro
*ot. If the resulting value contains two or more digits, those digits are summed
*and the process is repeated. This is continued as long as necessary to obtain a
*single digit.
*
*For example, consider the positive integer 24. Adding the 2 and the 4 yields a v
*alue of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider
*the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a sin
*gle digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a sing
*le digit and also the digital root of 39.
*/


#include<stdio.h>

int
zhou (int n)
{
  if (n < 10) /*如果n小于10了,也就得出计算结果了*/
    return n;
  else
    return zhou (sep (n));
}

int
sep (int n) /*返回n各位数之和*/
{
  int sum = 0;
  while (n) /*sum记录一个数的各位数之和*/
    {
      sum += n % 10;
      n = n / 10;
    }
  return sum;
}

int
main ()
{
  int n;
  printf ("Enter an Integer number:");
  scanf ("%d", &n);
  printf ("The digit root of %d is %d\n", n, zhou (n));
  return 0;
}
0 0
原创粉丝点击