首页 > 试题广场 >

Digital Roots

[编程题]Digital Roots
  • 热度指数:7350 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    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 integer may consist of a large number of digits. (1 <= input value <= 10^9)


输出描述:
    For each integer in the input, output its digital root on a separate line of the output.
示例1

输入

24
39

输出

6
3
头像 三7
发表于 2021-11-30 21:19:16
递归求解 ` #include<iostream> using namespace std; #include<string> int digui(int x){ string str=to_string(x);//将输入的数转换成字符串 int result 展开全文
头像 whoway
发表于 2020-12-07 09:47:15
一、用gets出错的 编译条件:C++(clang++11) #include<bits/stdc++.h> using namespace std; static const int maxn=1e5+5; char solve[maxn]; int main() { l 展开全文
头像 还是想躺平的小饼干很胆小
发表于 2023-02-24 21:42:14
#include<iostream> #include<math.h> using namespace std; #include<string> int dr(int x){ string str=to_string(x); int result 展开全文
头像 天乔巴夏、
发表于 2022-01-11 16:40:58
#include<iostream> using namespace std; int main() { int n; while(cin >> n) { int x = n; while(x / 10 != 0) { int t = x, sum 展开全文
头像 KoukiAlpha
发表于 2023-01-13 22:21:50
/* 解释一下题目: 就是给你一个数让你求数的数根!什么叫数根? 比如24这个数的数根,就是把24各个位的数加起来!2+4=6 而且6是个位数,于是它就是24的数根。 然后39这个数一样的方法,3+9=12 此时我们发现12不是个位数,所以它不是数根,继续让它各个 位数相加,1+2=3,3是个位数所 展开全文
头像 KoukiAlpha
发表于 2023-01-13 22:45:08
#include <iostream> using namespace std; int cal(int n){ int root = 0; if(n < 9) return n; label: while(n > 0){ roo 展开全文
头像 牛客575349009号
发表于 2023-03-03 19:46:14
#include <stdio.h> int main() { int num,sum; while(scanf("%d",&num)!=EOF){ if(num<10) printf("%d\n",num); 展开全文
头像 路人萌Miranda
发表于 2022-03-12 17:44:47
用字符串解决问题 #include #include #include using namespace std; int main() { string n; while (getline(cin, n)) { if (n == "ENDOFINPUT") { break; } else { 展开全文
头像 ddd
发表于 2023-03-27 17:28:52
#include <iostream> using namespace std; // 999 int fun(int num) { while (num >= 10) { int sum = 0; while (num > 0) { 展开全文
头像 小苕
发表于 2023-03-18 10:00:30
#include <stdio.h> #include <string.h> int root(int number) { int cnt = 0; while (number != 0) { //获取各个位的数值 cnt += (nu 展开全文

问题信息

难度:
67条回答 7778浏览

热门推荐

通过挑战的用户

查看代码