【九度OJ】题目1124:Digital Roots 解题报告

标签(空格分隔): 九度OJ


原题地址:http://ac.jobdu.com/problem.php?pid=1124

题目描述:

The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. 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 value 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 single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.

输入:

The input file will contain a list of positive integers, one per line.
The end of the input will be indicated by an integer value of zero.

输出:

For each integer in the input, output its digital root on a separate line of the output.

样例输入:

24
39
0

样例输出:

6
3

提示:

The integer may consist of a large number of digits.

Ways

这个题目的意思是,已知有个数字,把各位的数字加起来,如果和>=10,那么重复这个操作,直至为个位数。

另外特别提醒,有可能输入一个很大的数字,也就是没法直接使用int接受这个数字。

那么好,我用一个char接受这个数字,开了10000位的空间,应该够用。

第一遍循环,我求出了各位的和,现在一估算,这个数字不会大于100000,那么我之后就可以用int来操作了。23333

底下的步骤就是老一套,求余,把各位数字相加,然后循环。

前几次WA,原因是判断answer是不是个位数的时候,要注意answer>=10为条件,之前没写等号,故出错。

#include <stdio.h>
#include <string.h> int main() {
char str[10000];
while (scanf("%s", str) != EOF) {
int answer = 0;
if (strcmp(str, "0") == 0) {
break;
}
int len = strlen(str);
for (int i = 0; i < len; i++) {
answer += str[i] - '0';
}
while (answer >= 10) {//是大于等于,不是只有大于
int temp = answer;
answer = 0;//归零
while (temp > 0) {
answer += temp % 10;
temp /= 10;
}
}
printf("%d\n", answer);
} return 0;
}

另外根据上一篇文章的经验,可以使用sprintf函数。方法如下。

#include <stdio.h>
#include <string.h> int main() {
char str[10000];
while (scanf("%s", str) != EOF) {
if (strcmp(str, "0") == 0) {
break;
}
int answer = 10;//技巧
while (answer >= 10) {//是大于等于,不是只有大于
answer = 0;//每次循环归零
for (int i = 0; str[i] != 0; i++) {
answer += str[i] - '0';
}
sprintf(str, "%d", answer);//真的很方便啊!!!!
}
printf("%d\n", answer);
} return 0;
}

Date

2017 年 3 月 5 日

【九度OJ】题目1124:Digital Roots 解题报告的更多相关文章

  1. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  7. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  8. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

  9. 九度OJ题目1003:A+B

    while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...

随机推荐

  1. snakmake 小练习

    最近在学习snakemake 用于生信流程管理,现在用一个snakemake 来完成小任务:将在某一文件夹下的多个bam文件截取一部分,然后建立索引,在提取出fastq序列,最后比对回基因组. 需要两 ...

  2. 毕业设计之zabbix集合

    lnmp环境请查看https://www.cnblogs.com/betterquan/p/12285956.html 但是!!!注意php的编译: https://www.zabbix.com/do ...

  3. 60-Lowest Common Ancestor of a Binary Search Tree

    Lowest Common Ancestor of a Binary Search Tree My Submissions QuestionEditorial Solution Total Accep ...

  4. adblock plus-看下图你就懂

  5. 在 vscode.dev 中直接运行 Python !纯浏览器环境,无后端!

    其实有挺长一段时间没有写自己的 VS Code 插件了! 还是要感谢我们 DevDiv 组的 Flexible Friday 活动,让我可以在工作日研究自己感兴趣的项目. Flexible Frida ...

  6. C++中Try Catch中的继承

    1.C++中Try Catch简介:我们编译运行程序出错的时候,编译器就会抛出异常.抛出异常要比终止程序灵活许多. 而C++异常是指在程序运行时发生的反常行为,这些行为超出了函数正常功能的范围.当程序 ...

  7. day06 视图层

    day06 视图层 今日内容 视图层 小白必会三板斧 JsonResponse form表单发送文件 FBV与CBV FBV基于函数的视图 CBV基于类的视图 模板层 模板语法的传值 模板语法之过滤器 ...

  8. day34 前端基础之JavaScript

    day34 前端基础之JavaScript ECMAScript 6 尽管 ECMAScript 是一个重要的标准,但它并不是 JavaScript 唯一的部分,当然,也不是唯一被标准化的部分.实际上 ...

  9. Excel 数据验证:分类选择及输入限制

    几个简单设置让你的数据不再出错 如何快速选择某一大类中的细分小类 多级菜单 注意:引用可以创建二级目录,但是引用前应先用公式定义名称,然后引用,引用只能在本sheet操作.

  10. 案例 stm32单片机,adc的双通道+dma 内部温度

    可以这样理解 先配置adc :有几个通道就配置几个通道. 然后配置dma,dma是针对adc的,而不是针对通道的. 一开始我以为一个adc通道对应一个dma通道.(这里是错的,其实是我想复杂了) 一个 ...