来源:LeetCode 258  Add Dights

  Question:Given a non-negative integer  num , repeatedly add all its digits until the result has only one digit.

  For example:

     Given  num =  , the process is like:   + =  ,   + =  . Since    has only one digit, return it.

  Follow up:
     Could you do it without any loop/recursion in O(1) runtime?

  分析

  数字根(digital root)是自然数的一种性质,即每个自然数都有一个数字根。数根是将一自然数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相加直到其值小于10为止。例如54817的数根为7,因为5+4+8+1+7=25,25大于10则再加一次,2+5=7,7小于10,则7为54817的数字根。

  上面问题即是求一个非负整数的数字根。很容易想到下面这种方法解决问题:

#include<stdio.h>
#include<assert.h> int addDigits(int num)
{
int temp=;
while(num>=)
{
temp+=(num%);
num/=;
}
temp+=num; //不要忽略最高位数
num=temp;
if(num>=)
{
num=addDigits(num);//num仍大于10,则递归调用addDights函数
}
return num;
} int main()
{
int num;
scanf("%d",&num);
assert(num>=); //非负整数断言
printf("%d\n",addDigits(num));
return ;
}

  注意题目的延伸:要求我们不使用循环/递归复杂度O(1)

  这里用到一个求数字根的公式:    

             

  上述公式的文字表述为:0的数字根为0,9的倍数的数字根为9,其他自然数的数字根为其除以9的余数。证明过程点击这里

  

  上述公式可简单表述为:

  

  所以对于延伸的问题我们可以写出解决方法如下:

#include<stdio.h>
#include<assert.h> int addDigits(int num)
{
return +(num-)%; //直接调用公式
} int main()
{
int num;
scanf("%d",&num);
assert(num>=); //非负整数断言
printf("%d\n",addDigits(num));
return ;
}

  

数字根(digital root)的更多相关文章

  1. 如何证明一个数的数根(digital root)就是它对9的余数?

    数根就是不断地求这个数的各位数之和,直到求到个位数为止.所以数根一定和该数模9同余,但是数根又是大于零小于10的,所以数根模9的余数就是它本身,也就是说该数模9之后余数就是数根. 证明: 假设有一个n ...

  2. 树根 Digital root

    数根 (又称数字根Digital root)是自然数的一种性质.换句话说.每一个自然数都有一个数根.数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相 ...

  3. 1. 数字根(Digital Root)

    数字根(Digital Root)就是把一个自然数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.例如: 198的数字根为9(1+9+8=18,1 ...

  4. Digital root(数根)

    关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这 ...

  5. digital root问题

    问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  6. Codeforces Beta Round #10 C. Digital Root 数学

    C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...

  7. 数学 - SGU 118. Digital Root

    Digital Root Problem's Link Mean: 定义f(n)为n各位数字之和,如果n是各位数,则n个数根是f(n),否则为f(n)的数根. 现在给出n个Ai,求出A1*A2*…*A ...

  8. Digital Root 的推导

    背景 在LeetCode上遇到这道题:Add Digits 大意是给一个数,把它各位数字相加得到一个数,如果这个数小于10就返回,不然继续 addDigits(这个相加得到的数). 题目很简单,但是如 ...

  9. codeforces 10C Digital Root(非原创)

    Not long ago Billy came across such a problem, where there were given three natural numbers A, B and ...

随机推荐

  1. HDU2222 (AC自动机)

    AC自动机模板题. 被卡内存了 死活A不掉.. AC自动机参考教程: http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html ...

  2. magento添加多个产品到购物车(Add multiple products to cart )

    Step  1app\design\frontend\base\default\template\catalog\product\list.phtml<?php    $_productColl ...

  3. Lua标准库(转)

    转载地址:http://www.yiibai.com/lua/lua_standard_libraries.html Lua的标准库提供了一组丰富的功能,与C的API直接实现,建立在Lua编程语言函数 ...

  4. Install GD on mac

    Lots of bioinformatics software are based on perl. Some of them, for example, Circos, NGS toolkit... ...

  5. git不能提交jar的设置

      项目目录下 文件:.gitignore ,里面设置: *.class # Package Files # *.jar *.war *.ear 删除*.jar

  6. [转]在Eclipse中Debug 为什么显示source not found

    在Eclipse中Debug 为什么显示source not found http://zhidao.baidu.com/link?url=-jna2HB_k2FW72GPbT--5Qg2AWi3Ip ...

  7. 383. Ransom Note

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  8. Android模仿QQ空间图片上传——原理

    MainActivity.class package com.example.imagedemotext; import java.io.ByteArrayOutputStream; import j ...

  9. 启动tomcat不出现命令窗口

    有个软件要安装在U盘中,B/S结构,用tomcat做应用服务器,客户要求tomcat不能注册为系统服务,启动时tomcat启动时不能出现命令行窗口,怎么实现? 根据你的问题描述,猜测你的部署系统是Wi ...

  10. 所学新知——int、char型转string 类型等

    1. 利用stringstream类 定义头文件#include<sstream> 通过 int a; char b; sstream ss,ss1; ss<<a; ss1&l ...