A-A+

有以下程序: include <stdio.h> int sub(int n) { retu

2022-08-12 18:46:52 问答库 阅读 197 次

问题详情

有以下程序: include <stdio.h> int sub(int n) { return(n/10 + n% 10); } main() { int x,y; seanf(" %d" , &x); y = sub (sub(sub (x))); printf(" %d n";,y); } 若运行时输入:1234<回车>,程序的输出结果是【 】。


请帮忙给出正确答案和分析,谢谢!

参考答案

正确答案:10
函数sub递归调用的返回值被作为再次调用sub函数的实参传给函数sub的形参,共进行3次递归调用。第1次调用sub(1234)的返回值为1234/10+1234%10=127;第2次调用sub(127)的返回值为127/10+127%10=19;第3次调用sub(19)的返回值为19/10+19%10=10。所以程序的输出为10。

考点:程序