📄 c语言经典程序100例.htm
字号:
<BR>【程序20】<BR>题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在<BR> 第10次落地时,共经过多少米?第10次反弹多高?<BR>1.程序分析:见下面注释<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "stdio.h"<BR>main()<BR>{<BR>float sn=100.0,hn=sn/2;<BR>int
n;<BR>for(n=2;n<=10;n++)<BR>{<BR>sn=sn+2*hn;/*第n次落地时共经过的米数*/<BR>hn=hn/2;
/*第n次反跳高度*/<BR>}<BR>printf("the total of road is %f\n",sn);<BR>printf("the tenth
is %f meter\n",hn);<BR>getch();<BR>}</P>
<P><BR> 经典c程序100例==21--30<BR>【程序21】<BR>题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个<BR> 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下<BR> 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。<BR>1.程序分析:采取逆向思维的方法,从后往前推断。<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int
day,x1,x2;<BR>day=9;<BR>x2=1;<BR>while(day>0)<BR>{<BR>x1=(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/<BR>x2=x1;<BR>day--;<BR>}<BR>printf("the
total is
%d\n",x1);<BR>getch();<BR>}<BR>==============================================================<BR>【程序22】<BR>题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定<BR> 比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出<BR> 三队赛手的名单。
<BR>1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,<BR> 则表明此数不是素数,反之是素数。
<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include
"conio.h"<BR>main()<BR>{<BR>char
i,j,k;/*i是a的对手,j是b的对手,k是c的对手*/<BR>for(i='x';i<='z';i++)<BR>for(j='x';j<='z';j++)<BR>{<BR>if(i!=j)<BR>for(k='x';k<='z';k++)<BR>{<BR>if(i!=k&&j!=k)<BR>{<BR>if(i!='x'&&k!='x'&&k!='z')<BR>printf("order
is
a--%c\tb--%c\tc--%c\n",i,j,k);<BR>}<BR>}<BR>}<BR>getch();<BR>}<BR>==============================================================<BR>【程序23】
<BR>题目:打印出如下图案(菱形)<BR>*<BR>***<BR>*****<BR>*******<BR>*****<BR>***<BR>*<BR>1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重<BR> for循环,第一层控制行,第二层控制列。
<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int
i,j,k;<BR>for(i=0;i<=3;i++)<BR>{<BR>for(j=0;j<=2-i;j++)<BR>printf("
");<BR>for(k=0;k<=2*i;k++)<BR>printf("*");<BR>printf("\n");<BR>}<BR>for(i=0;i<=2;i++)<BR>{<BR>for(j=0;j<=i;j++)<BR>printf("
");<BR>for(k=0;k<=4-2*i;k++)<BR>printf("*");<BR>printf("\n");<BR>}<BR>getch();<BR>}<BR>==============================================================<BR>【程序24】
<BR>题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。<BR>1.程序分析:请抓住分子与分母的变化规律。
<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int
n,t,number=20;<BR>float
a=2,b=1,s=0;<BR>for(n=1;n<=number;n++)<BR>{<BR>s=s+a/b;<BR>t=a;a=a+b;b=t;/*这部分是程序的关键,请读者猜猜t的作用*/<BR>}<BR>printf("sum
is
%9.6f\n",s);<BR>getch();<BR>}<BR>==============================================================<BR>【程序25】
<BR>题目:求1+2!+3!+...+20!的和<BR>1.程序分析:此程序只是把累加变成了累乘。 <BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>float
n,s=0,t=1;<BR>for(n=1;n<=20;n++)<BR>{<BR>t*=n;<BR>s+=t;<BR>}<BR>printf("1+2!+3!...+20!=%e\n",s);<BR>getch();<BR>}<BR>==============================================================<BR>【程序26】
<BR>题目:利用递归方法求5!。<BR>1.程序分析:递归公式:fn=fn_1*4!<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int i;<BR>int
fact();<BR>for(i=0;i<5;i++)<BR>printf("\40:%d!=%d\n",i,fact(i));<BR>getch();<BR>}<BR>int
fact(j)<BR>int j;<BR>{<BR>int
sum;<BR>if(j==0)<BR>sum=1;<BR>else<BR>sum=j*fact(j-1);<BR>return
sum;<BR>}<BR>==============================================================<BR>【程序27】
<BR>题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。<BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int i=5;<BR>void palin(int
n);<BR>printf("\40:");<BR>palin(i);<BR>printf("\n");<BR>getch();<BR>}<BR>void
palin(n)<BR>int n;<BR>{<BR>char
next;<BR>if(n<=1)<BR>{<BR>next=getchar();<BR>printf("\n\0:");<BR>putchar(next);<BR>}<BR>else<BR>{<BR>next=getchar();<BR>palin(n-1);<BR>putchar(next);<BR>}<BR>}<BR>==============================================================<BR>【程序28】
<BR>题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第<BR> 3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后
<BR> 问第一个人,他说是10岁。请问第五个人多大?<BR>1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道<BR> 第四人的岁数,依次类推,推到第一人(10岁),再往回推。<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>age(n)<BR>int n;<BR>{<BR>int c;<BR>if(n==1)
c=10;<BR>else
c=age(n-1)+2;<BR>return(c);<BR>}<BR>main()<BR>{<BR>printf("%d",age(5));<BR>getch();<BR>}<BR>==============================================================<BR>【程序29】
<BR>题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。<BR>1.
程序分析:学会分解出每一位数,如下解释:(这里是一种简单的算法,师专数002班赵鑫提供) <BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>main( )<BR>{<BR>long
a,b,c,d,e,x;<BR>scanf("%ld",&x);<BR>a=x/10000;/*分解出万位*/<BR>b=x%10000/1000;/*分解出千位*/<BR>c=x%1000/100;/*分解出百位*/<BR>d=x%100/10;/*分解出十位*/<BR>e=x%10;/*分解出个位*/<BR>if
(a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a);<BR>else if (b!=0)
printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);<BR>else if (c!=0) printf("
there are 3,%ld %ld %ld\n",e,d,c);<BR>else if (d!=0) printf("there are 2, %ld
%ld\n",e,d);<BR>else if (e!=0) printf(" there are
1,%ld\n",e);<BR>getch();<BR>}<BR>==============================================================<BR>【程序30】
<BR>题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。 <BR>1.程序分析:同29例<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>main( )<BR>{<BR>long
ge,shi,qian,wan,x;<BR>scanf("%ld",&x);<BR>wan=x/10000;<BR>qian=x%10000/1000;<BR>shi=x%100/10;<BR>ge=x%10;<BR>if(ge==wan&&shi==qian)/*个位等于万位并且十位等于千位*/<BR>printf("this
number is a huiwen\n");<BR>else<BR>printf("this number is not a
huiwen\n");<BR>getch();<BR>}</P>
<P><BR> 经典c程序100例==31--40<BR>【程序31】<BR>题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续<BR> 判断第二个字母。<BR>1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>void main()<BR>{<BR>char
letter;<BR>printf("please input the first letter of
someday\n");<BR>while((letter=getch())!='Y')/*当所按字母为Y时才结束*/<BR>{<BR>switch
(letter)<BR>{<BR>case 'S':printf("please input second
letter\n");<BR>if((letter=getch())=='a')<BR>printf("saturday\n");<BR>else if
((letter=getch())=='u')<BR>printf("sunday\n");<BR>else printf("data
error\n");<BR>break;<BR>case 'F':printf("friday\n");break;<BR>case
'M':printf("monday\n");break;<BR>case 'T':printf("please input second
letter\n");<BR>if((letter=getch())=='u')<BR>printf("tuesday\n");<BR>else if
((letter=getch())=='h')<BR>printf("thursday\n");<BR>else printf("data
error\n");<BR>break;<BR>case 'W':printf("wednesday\n");break;<BR>default:
printf("data
error\n");<BR>}<BR>}<BR>getch();<BR>}<BR>==============================================================<BR>【程序32】<BR>题目:Press
any key to change color, do you want to try it. Please hurry
up!<BR>1.程序分析: <BR>2.程序源代码:<BR>#include "conio.h"<BR>#include
"stdio.h"<BR>void main(void)<BR>{<BR>int color;<BR>for (color = 0; color < 8;
color++)<BR>{ <BR>textbackground(color);/*设置文本的背景颜色*/<BR>cprintf("This is color
%d\r\n", color);<BR>cprintf("Press any key to
continue\r\n");<BR>getch();/*输入字符看不见*/<BR>}<BR>}<BR>==============================================================<BR>【程序33】<BR>题目:学习gotoxy()与clrscr()函数 <BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"conio.h"<BR>#include "stdio.h"<BR>void
main(void)<BR>{<BR>clrscr();/*清屏函数*/<BR>textbackground(2);<BR>gotoxy(1,
5);/*定位函数*/<BR>cprintf("Output at row 5 column
1\n");<BR>textbackground(3);<BR>gotoxy(20, 10);<BR>cprintf("Output at row 10
column
20\n");<BR>getch();<BR>}<BR>==============================================================<BR>【程序34】<BR>题目:练习函数调用<BR>1.
程序分析: <BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include "conio.h"<BR>void
hello_world(void)<BR>{<BR>printf("Hello, world!\n");<BR>}<BR>void
three_hellos(void)<BR>{<BR>int counter;<BR>for (counter = 1; counter <= 3;
counter++)<BR>hello_world();/*调用此函数*/<BR>}<BR>void
main(void)<BR>{<BR>three_hellos();/*调用此函数*/<BR>getch();<BR>}<BR>==============================================================<BR>【程序35】<BR>题目:文本颜色设置<BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>void main(void)<BR>{<BR>int color;<BR>for
(color = 1; color < 16;
color++)<BR>{<BR>textcolor(color);/*设置文本颜色*/<BR>cprintf("This is color %d\r\n",
color);<BR>}<BR>textcolor(128 + 15);<BR>cprintf("This is
blinking\r\n");<BR>getch();<BR>}<BR>==============================================================<BR>【程序36】<BR>题目:求100之内的素数 <BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "math.h"<BR>#define N 101<BR>main()<BR>{<BR>int
i,j,line,a[N];<BR>for(i=2;i<N;i++)
a[i]=i;<BR>for(i=2;i<sqrt(N);i++)<BR>for(j=i+1;j<N;j++)<BR>{<BR>if(a[i]!=0&&a[j]!=0)<BR>if(a[j]%a[i]==0)<BR>a[j]=0;<BR>}<BR>printf("\n");<BR>for(i=2,line=0;i<N;i++)<BR>{<BR>if(a[i]!=0)<BR>{<BR>printf("%5d",a[i]);<BR>line++;<BR>}<BR>if(line==10)<BR>{<BR>printf("\n");<BR>line=0;<BR>}<BR>}<BR>getch();<BR>}<BR>==============================================================<BR>【程序37】<BR>题目:对10个数进行排序<BR>1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,<BR> 下次类推,即用第二个元素与后8个进行比较,并进行交换。
<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include "conio.h"<BR>#define N
10<BR>main()<BR>{<BR>int i,j,min,tem,a[N];<BR>/*input data*/<BR>printf("please
input ten
num:\n");<BR>for(i=0;i<N;i++)<BR>{<BR>printf("a[%d]=",i);<BR>scanf("%d",&a[i]);<BR>}<BR>printf("\n");<BR>for(i=0;i<N;i++)<BR>printf("%5d",a[i]);<BR>printf("\n");<BR>/*sort
ten
num*/<BR>for(i=0;i<N-1;i++)<BR>{<BR>min=i;<BR>for(j=i+1;j<N;j++)<BR>if(a[min]>a[j])<BR>min=j;<BR>tem=a[i];<BR>a[i]=a[min];<BR>a[min]=tem;<BR>}<BR>/*output
data*/<BR>printf("After sorted
\n");<BR>for(i=0;i<N;i++)<BR>printf("%5d",a[i]);<BR>getch();<BR>}<BR>==============================================================<BR>【程序38】<BR>题目:求一个3*3矩阵对角线元素之和
<BR>1.程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -