【程序24】
题目:有一分数序列:21,32,53,85,138,2113,…求出那个数列的前20项之和.
1,程序解析:请抓住分子与分母的变化规律.
2,程序源代码:
main()
{
int n,t,number=20;
float a=2,b=1,s=0;
for(n=1;n<=number;n++)
{
s=s+ab;
t=a;a=a+b;b=t;这部分是 程序的关键,请读者猜猜t的作用
}
printf(“sum is %9,6f\n”,s);
}
【程序25】
题目:求1+2!+3!+…+20!的和
1,程序解析:此程序只是 把累加变成了累乘.
2,程序源代码:
main()
{
float n,s=0,t=1;
for(n=1;n<=20;n++)
{
t*=n;
s+=t;
}
printf(“1+2!+3!..+20!=%e\n”,s);
}
【程序26】
题目:操纵递归方式求5!.
1,程序解析:递归程式:fn=fn_14!
2,程序源代码:
#i nclude “stdio.h”
main()
{
int i;
int fact();
for(i=0;i<5;i++)
printf(“\40:%d!=%d\n”,i,fact(i));
}
int fact(j)
int j;
{
int sum;
if(j==0)
sum=1;
else
sum=jfact(j-1);
return sum;
}
【程序27】
题目:操纵递归函数调用方式,将所输入的5个字符,以相反次序打印出来.
1,程序解析:
2,程序源代码:
#i nclude “stdio.h”
main()
{
int i=5;
void palin(int n);
printf(“\40:”);
palin(i);
printf(“\n”);
}
void palin(n)
int n;
{
char next;
if(n<=1)
{
next=gainchar();
printf(“\n\0:”);
putchar(next);
}
else
{
next=gainchar();
palin(n-1);
putchar(next);
}
}
【程序28】
题目:有5个人坐在一路,问第五个人几 岁?他讲比第4个人大2岁.问第4个人岁数,他讲比第3个人大2岁.问第三个人,又讲比第2人大两岁.问第2个人,讲比第一个人大两岁.末了问第一个人,他讲是 10岁.叨教第五个人多大?
1,程序解析:操纵递归的方式,递归分为回推和递推两个期间.要想明白第五个人岁数,需明白第四人的岁数,依次类推,推到第一人(10岁),再往回推.
2,程序源代码:
age(n)
int n;
{
int c;
if(n==1) c=10;
else c=age(n-1)+2;
return©;
}
main()
{ printf(“%d”,age(5));
}
【程序29】
题目:给一个不多于5位的正整数,要求:一、求它是 几位数,二、逆序打印出列位数字.
1,程序解析:学会分化出每一名数,似下讲明:(这个地方是 一种简单的算法,师专数002班*鑫供应)
2,程序源代码:
main( )
{
long a,b,c,d,e,x;
scanf(“%ld”,&x);
a=x10000;分化出万位
b=x%100001000;分化出千位
c=x%1000100;分化出百位
d=x%10010;分化出十位
e=x%10;分化出个位
if (a!=0) printf(“there are 5,%ld %ld %ld %ld %ld\n”,e,d,c,b,a);
else if (b!=0) printf(“there are 4,%ld %ld %ld %ld\n”,e,d,c,b);
else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c);
else if (d!=0) printf(“there are 2,%ld %ld\n”,e,d);
else if (e!=0) printf(" there are 1,%ld\n",e);
}
【程序30】
题目:一个5位数,判断它是 不是 回文数.即12321是 回文数,个位与万位同样,十位与千位同样.
1,程序解析:同29例
2,程序源代码:
main( )
{
long ge,shi,qian,wan,x;
scanf(“%ld”,&x);
wan=x10000;
qian=x%100001000;
shi=x%10010;
ge=x%10;
if (gewan&&shiqian)个位等于万位同时且十位等于千位
printf(“this number is a huiwen\n”);
else
printf(“this number is not a huiwen\n”);
}
【程序31】
题目:请输入星期几的第一个字母来判断一下是 星期几,介入第一个字母同样,那么接着判断第二个字母.
1,程序解析:用状况语句对比好,介入第一个字母同样,那么判断用状况语句或if语句判断第二个字母.
2,程序源代码:
#i nclude
void main()
{
char letter;
printf(“please input the first letter of someday\n”);
while ((letter=gainch())!=‘Y’)当所按字母为Y时才终了
{ switch (letter)
{case ‘S’:printf(“please input second letter\n”);
if((letter=gainch())‘a’)
printf(“saturday\n”);
else if ((letter=gainch())‘u’)
printf(“sunday\n”);
else printf(“data error\n”);
break;
case ‘F’:printf(“friday\n”);break;
case ‘M’:printf(“monday\n”);break;
case ‘T’:printf(“please input second letter\n”);
if((letter=gainch())‘u’)
printf(“tuesday\n”);
else if ((letter=gainch())‘h’)
printf(“thursday\n”);
else printf(“data error\n”);
break;
case ‘W’:printf(“wednesday\n”);break;
default:printf(“data error\n”);
}}}
【程序32】
题目:Press any key to change color,do you desire to try it.Please hurry up!
1,程序解析:
2,程序源代码:
#i nclude
void main(void)
{
int color;
for (color = 0;color < 8;color++)
{
textbackground(color);设置文本的背景颜色
cprintf(“This is color %d\r\n”,color);
cprintf(“Press any key to continue\r\n”);
gainch();输入字符看不见
}}
【程序33】
题目:进修gotoxy()与clrscr()函数
1,程序解析:
2,程序源代码:
#i nclude
void main(void)
{
clrscr();清屏函数
textbackground(2);
gotoxy(1,5);定位函数
cprintf(“Output at row 5 column 1\n”);
textbackground(3);
gotoxy(20,10);
cprintf(“Output at row 10 column 20\n”);
}
【程序34】
题目:锻炼函数调用
1,程序解析:
2,程序源代码:
#i nclude
void hello_world(void)
{
printf(“Hello,world!\n”);
}
void three_hellos(void)
{
int counter;
for (counter = 1;counter <= 3;counter++)
hello_world();调用此函数
}
void main(void)
{
three_hellos();调用此函数
}