C语言编程,用递归法将一个正整数n的每个数字都拆分出来,数字前后都标记“*”。

例如,输入486,应输出字符串"*4*8*6*"(数字之间加一个星号)。n的位数不确定,可以是任意位数的int的合法范围内的正整数。

给你个代码:

#include<stdio.h>
void f(int n)
{
if (n)
{
if(n<10)
{
printf("*%d*",n);
return;
}
f(n / 10);
printf("%d*", n % 10);
}
}
int main()

int n;
printf("输入一个正整数:");
scanf("%d", &n);
f(n);
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-10-30
#include<stdio.h>
#include<stdlib.h>

int main()
{
int n;

scanf("%d",&n);
output(n);
printf("\n");
system("PAUSE");
return EXIT_SUCCESS;
}
void output(int n)
{
if(0!=n)
{
output(n/10);
printf("*%d",n%10);
}
}追问

这个代码最后输出的时候少了个*,比如486,最后是*4*8*6,但是题目要求的是*4*8*6*,所以没有采纳,但是还是谢谢您了!

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网