您的位置:首页>软件开发>C/C++>

C语言程序开发经典实例之六

[ 来源:csai | 更新日期:2007-7-15 20:20:11 | 评论 0 条 | 我要投稿 ]
1.程序分析:递归公式:fnfn_1*4!

2.程序源代码:

#include "stdio.h"
main()
{
int i;
int fact();
for(i0;i<_5i br="br" /> printf("40:%d!%dn",i,fact(i));
}
int fact(j)
int j;
{
int sum;
if(j0)
sum1;
else
sumj*fact(j-1);
return sum;
}

【程序27】

题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。

1.程序分析:

2.程序源代码:
#include "stdio.h"
main() 字串9
{
int i5;
void palin(int n);
printf("40:");
palin(i);
printf("n");
}
void palin(n)
int n;
{
char next;
if(n<_1 br="br" /> {
nextgetchar();
printf("n:");
putchar(next);
}
else
{
nextgetchar();
palin(n-1);
putchar(next);
}
}

【程序28】

题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?

字串3



1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。

2.程序源代码:

age(n)
int n;
{
int c;
if(n1) c10;
else cage(n-1)+2;
return(c);
}
main()
{ printf("%d",age(5));
}

【程序29】

题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

1. 程序分析:学会分解出每一位数,如下解释:(这里是一种简单的算法,师专数002班赵鑫提供)

2.程序源代码:

main( )
{
字串2

long a,b,c,d,e,x;
scanf("%ld",&x);
ax/10000;/*分解出万位*/
bx%10000/1000;/*分解出千位*/
cx%1000/100;/*分解出百位*/
dx%100/10;/*分解出十位*/
ex%10;/*分解出个位*/
if (a!0) printf("there are 5, %ld %ld %ld %ld %ldn",e,d,c,b,a);
else if (b!0) printf("there are 4, %ld %ld %ld %ldn",e,d,c,b);
else if (c!0) printf(" there are 3,%ld %ld %ldn",e,d,c);
else if (d!0) printf("there are 2, %ld %ldn",e,d);
else if (e!0) printf(" there are 1,%ldn",e);
}

【程序30】

题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。   

1.程序分析:同29例

2.程序源代码: 字串9

main( )
{
long ge,shi,qian,wan,x;
scanf("%ld",&x);
wanx/10000;
qianx%10000/1000;
shix%100/10;
gex%10;
if (gewan&&shiqian)/*个位等于万位并且十位等于千位*/
printf("this number is a huiwenn");
else
printf("this number is not a huiwenn");
}

Tags:
责任编辑:
您的评论
用户名: 新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为