B. Sum of Digits
来源:互联网 发布:淘宝如何解绑银行卡 编辑:程序博客网 时间:2024/04/29 11:26
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit?
The first line contains the only integer n (0 ≤ n ≤ 10100000). It is guaranteed that n doesn't contain any leading zeroes.
Print the number of times a number can be replaced by the sum of its digits until it only contains one digit.
0
0
10
1
991
3
In the first sample the number already is one-digit — Herald can't cast a spell.
The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.
The third test contains number 991. As one casts a spell the following transformations take place: 991 → 19 → 10 → 1. After three transformations the number becomes one-digit.
解题说明:此题就是不断对数字按位进行累加,直到变成一个数字为止。由于输入数字很大,应该用字符串存储,这里要用到sprintf 函数,把变量打印到字符串中,从而获得数字的字符形式,这样就不需要手工转换。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include<set>#include <algorithm>using namespace std;char s[100100];int main() {int n,cnt,i,res;scanf("%s",s);cnt=0;while (s[1]) {cnt++;res=0;for(i=0;s[i];i++){res+=s[i]-'0';}sprintf(s,"%d",res);}printf("%d\n",cnt);return 0;}
- B. Sum of Digits
- B. Maximize Sum of Digits
- codeforces 770B Maximize Sum of Digits
- CF770-B-Maximize Sum of Digits
- HDU3022:Sum of Digits
- HDU3022:Sum of Digits
- Sum of Digits
- Lucky Sum of Digits
- Ural 1658 Sum of Digits
- Timus 1658. Sum of Digits
- ural 1658. Sum of Digits
- URAL 1658. Sum of Digits
- A. Lucky Sum of Digits
- Ural 1658 Sum of Digits
- 1952-Lucky Sum of Digits
- [Haskell] CodeWars|Sum of Digits
- B Quality of Check Digits
- codeforces—— 770B —— Maximize Sum of Digits
- 两个调试打印函数
- 寻找·服务!
- 用来简化开发任务的20个JavaScript类库
- JAVA 程序中Log4j输出详细错误堆栈信息
- openDrive外链
- B. Sum of Digits
- UG NX7.5 采用VS2008调试方法
- 【初探移动前端开发02】移动设备的页面
- 部署在Tomcat上应用程序的远程调试
- My97DatePicker 时间控件使用
- [AS 功能代码教程10]数据结构排序算法
- Android trace 文件含义
- 给初学C#的朋友一些建议。
- 回调函数浅析