RainMoun 2017-03-06 08:50 采纳率: 0%
浏览 1138

刷杭电oj的时候遇到个问题

Problem Description
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.
 

Input
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.
 

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

Sample Input
24
39
0
 

Sample Output
6
3

这是我的程序,C语言的:
#include
#include
void calculate(int x){
int digit = 1;
int i = 1;
int newx = 0;
while(x / pow(10,i) != 0){
digit++;
}
for(int j = digit;j > 0;j--){
newx += (int)(x % pow(10,j));
x = x / pow(10,j);
}
if(newx / 10 == 0 ) printf("%d",newx);
else {
x = newx;
calculate(x);
}
}

int main(){
int num;
int digit = 1;
int i = 1;
scanf("%d",&num);
calculate(num);
return 0;

}

出错信息贴在下面

  • 写回答

2条回答 默认 最新

  • RainMoun 2017-03-06 08:55
    关注

    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘