c語言小白必學算法?【程序26】題目:利用遞歸方法求5!,我來為大家科普一下關于c語言小白必學算法?以下内容希望對你有幫助!
【程序26】
題目:利用遞歸方法求5!。
1.程序分析:遞歸公式:fn=fn_1*4!
2.程序源代碼:
#include "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=j*fact(j-1);
return sum;
}
==============================================================
【程序27】
題目:利用遞歸函數調用方式,将所輸入的5個字符,以相反順序打印出來。
1.程序分析:
2.程序源代碼:
#include "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=getchar();
printf("\n\0:");
putchar(next);
}
else
{
next=getchar();
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(c);
}
main()
{ printf("%d",age(5));
}
==============================================================
【程序29】
題目:給一個不多于5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。
1. 程序分析:學會分解出每一位數,如下解釋:(這裡是一種簡單的算法,師專數002班趙鑫提供)
2.程序源代碼:
main( )
{
long a,b,c,d,e,x;
scanf("%ld",&x);
a=x/10000;/*分解出萬位*/
b=x000/1000;/*分解出千位*/
c=x00/100;/*分解出百位*/
d=x0/10;/*分解出十位*/
e=x;/*分解出個位*/
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=x/10000;
qian=x000/1000;
shi=x0/10;
ge=x;
if (ge==wan&&shi==qian)/*個位等于萬位并且十位等于千位*/
printf("this number is a huiwen\n");
else
printf("this number is not a huiwen\n");
}
【程序31】
題目:請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續
判斷第二個字母。
1.程序分析:用情況語句比較好,如果第一個字母一樣,則判斷用情況語句或if語句判斷第二個字母。
2.程序源代碼:
#include
void main()
{
char letter;
printf("please input the first letter of someday\n");
while ((letter=getch())!='Y')/*當所按字母為Y時才結束*/
{ switch (letter)
{case 'S':printf("please input second letter\n");
if((letter=getch())=='a')
printf("saturday\n");
else if ((letter=getch())=='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=getch())=='u')
printf("tuesday\n");
else if ((letter=getch())=='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 want to try it. Please hurry up!
1.程序分析:
2.程序源代碼:
#include
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");
getch();/*輸入字符看不見*/
}
}
==============================================================
【程序33】
題目:學習gotoxy()與clrscr()函數
1.程序分析:
2.程序源代碼:
#include
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.程序源代碼:
#include
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();/*調用此函數*/
}
==============================================================
【程序35】
題目:文本顔色設置
1.程序分析:
2.程序源代碼:
#include
void main(void)
{
int color;
for (color = 1; color < 16; color )
{
textcolor(color);/*設置文本顔色*/
cprintf("This is color %d\r\n", color);
}
textcolor(128 15);
cprintf("This is blinking\r\n");
}
==============================================================
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!