声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 13351|回复: 64

[C/C++] [分享]c语言 ---- 经典百例[zz]

[复制链接]
发表于 2005-11-21 01:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
<DIV>【程序1】<BR>题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?<BR>1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去<BR>      掉不满足条件的排列。 <BR>2.程序源代码:<BR>main()<BR>{<BR>int i,j,k;<BR>printf("\n");<BR>for(i=1;i&lt;5;i++)    /*以下为三重循环*/<BR> for(j=1;j&lt;5;j++) <BR>  for (k=1;k&lt;5;k++)<BR>   {<BR>    if (i!=k&amp;&amp;i!=j&amp;&amp;j!=k)    /*确保i、j、k三位互不相同*/<BR>    printf("%d,%d,%d\n",i,j,k);<BR>   }<BR>}<BR>==============================================================<BR>【程序2】<BR>题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高<BR>   于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提<BR>   成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于<BR>   40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于<BR>   100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?<BR>1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。      <BR>2.程序源代码:<BR>main()<BR>{<BR>long int i;<BR>int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;<BR>scanf("%ld",&amp;i);<BR>bonus1=100000*0.1;bonus2=bonus1+100000*0.75;<BR>bonus4=bonus2+200000*0.5;<BR>bonus6=bonus4+200000*0.3;<BR>bonus10=bonus6+400000*0.15;<BR> if(i&lt;=100000)<BR>  bonus=i*0.1;<BR> else if(i&lt;=200000)<BR>     bonus=bonus1+(i-100000)*0.075;<BR>    else if(i&lt;=400000)<BR>        bonus=bonus2+(i-200000)*0.05;<BR>       else if(i&lt;=600000)<BR>           bonus=bonus4+(i-400000)*0.03;<BR>          else if(i&lt;=1000000)<BR>              bonus=bonus6+(i-600000)*0.015;<BR>             else<BR>              bonus=bonus10+(i-1000000)*0.01;<BR>printf("bonus=%d",bonus);<BR>} <BR><BR>==============================================================<BR>【程序3】<BR>题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?<BR>1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后<BR>      的结果满足如下条件,即是结果。请看具体分析:<BR>2.程序源代码:<BR>#include "math.h"<BR>main()<BR>{<BR>long int i,x,y,z;<BR>for (i=1;i&lt;100000;i++)<BR> { x=sqrt(i+100);   /*x为加上100后开方后的结果*/<BR>  y=sqrt(i+268);   /*y为再加上168后开方后的结果*/<BR>   if(x*x==i+100&amp;&amp;y*y==i+268)/*如果一个数的平方根的平方等于该数,这说明此数是完全平方数*/<BR>    printf("\n%ld\n",i);<BR> }<BR>}<BR>==============================================================<BR>【程序4】<BR>题目:输入某年某月某日,判断这一天是这一年的第几天?<BR>1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊<BR>      情况,闰年且输入月份大于3时需考虑多加一天。<BR>2.程序源代码:<BR>main()<BR>{<BR>int day,month,year,sum,leap;<BR>printf("\nplease input year,month,day\n");<BR>scanf("%d,%d,%d",&amp;year,&amp;month,&amp;day);<BR>switch(month)/*先计算某月以前月份的总天数*/<BR>{<BR> case 1:sum=0;break;<BR> case 2:sum=31;break;<BR> case 3:sum=59;break;<BR> case 4:sum=90;break;<BR> case 5:sum=120;break;<BR> case 6:sum=151;break;<BR> case 7:sum=181;break;<BR> case 8:sum=212;break;<BR> case 9:sum=243;break;<BR> case 10:sum=273;break;<BR> case 11:sum=304;break;<BR> case 12:sum=334;break;<BR> default:printf("data error");break;<BR>}<BR>sum=sum+day;  /*再加上某天的天数*/<BR> if(year%400==0||(year%4==0&amp;&amp;year%100!=0))/*判断是不是闰年*/<BR>  leap=1;<BR> else<BR>  leap=0;<BR>if(leap==1&amp;&amp;month&gt;2)/*如果是闰年且月份大于2,总天数应该加一天*/<BR>sum++;<BR>printf("It is the %dth day.",sum);}<BR>==============================================================<BR>【程序5】<BR>题目:输入三个整数x,y,z,请把这三个数由小到大输出。<BR>1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x&gt;y则将x与y的值进行交换,<BR>      然后再用x与z进行比较,如果x&gt;z则将x与z的值进行交换,这样能使x最小。<BR>2.程序源代码:<BR>main()<BR>{<BR>int x,y,z,t;<BR>scanf("%d%d%d",&amp;x,&amp;y,&amp;z);<BR>if (x&gt;y)<BR>{t=x;x=y;y=t;} /*交换x,y的值*/<BR>if(x&gt;z)<BR>{t=z;z=x;x=t;}/*交换x,z的值*/<BR>if(y&gt;z)<BR>{t=y;y=z;z=t;}/*交换z,y的值*/<BR>printf("small to big: %d %d %d\n",x,y,z);<BR>}<BR>==============================================================<BR>【程序6】<BR>题目:用*号输出字母C的图案。<BR>1.程序分析:可先用'*'号在纸上写出字母C,再分行输出。<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR>printf("Hello C-world!\n");<BR>printf(" ****\n");<BR>printf(" *\n");<BR>printf(" * \n");<BR>printf(" ****\n");<BR>}<BR>==============================================================<BR>【程序7】<BR>题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!<BR>1.程序分析:字符共有256个。不同字符,图形不一样。      <BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR>char a=176,b=219;<BR>printf("%c%c%c%c%c\n",b,a,a,a,b);<BR>printf("%c%c%c%c%c\n",a,b,a,b,a);<BR>printf("%c%c%c%c%c\n",a,a,b,a,a);<BR>printf("%c%c%c%c%c\n",a,b,a,b,a);<BR>printf("%c%c%c%c%c\n",b,a,a,a,b);}<BR>==============================================================<BR>【程序8】<BR>题目:输出9*9口诀。<BR>1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR> int i,j,result;<BR> printf("\n");<BR> for (i=1;i&lt;10;i++)<BR>  { for(j=1;j&lt;10;j++)<BR>    {<BR>     result=i*j;<BR>     printf("%d*%d=%-3d",i,j,result);/*-3d表示左对齐,占3位*/<BR>    }<BR>   printf("\n");/*每一行后换行*/<BR>  }<BR>}<BR>==============================================================<BR>【程序9】<BR>题目:要求输出国际象棋棋盘。<BR>1.程序分析:用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格。<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR>int i,j;<BR>for(i=0;i&lt;8;i++)<BR> {<BR>  for(j=0;j&lt;8;j++)<BR>   if((i+j)%2==0)<BR>    printf("%c%c",219,219);<BR>   else<BR>    printf(" ");<BR>   printf("\n");<BR> }<BR>}<BR>============================================================== <BR>【程序10】<BR>题目:打印楼梯,同时在楼梯上方打印两个笑脸。 <BR>1.程序分析:用i控制行,j来控制列,j根据i的变化来控制输出黑方格的个数。<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR>int i,j;<BR>printf("\1\1\n");/*输出两个笑脸*/<BR>for(i=1;i&lt;11;i++)<BR> {<BR> for(j=1;j&lt;=i;j++)<BR>   printf("%c%c",219,219);<BR> printf("\n");<BR> }<BR>}</DIV>
回复
分享到:

使用道具 举报

 楼主| 发表于 2005-11-21 01:37 | 显示全部楼层

回复:(风花雪月)[分享]c语言 ---- 经典百例[zz]

<DIV>【程序11】<BR>题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月<BR>   后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?<BR>1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....<BR>2.程序源代码:<BR>main()<BR>{<BR>long f1,f2;<BR>int i;<BR>f1=f2=1;<BR>for(i=1;i&lt;=20;i++)<BR> { printf("%12ld %12ld",f1,f2);<BR>   if(i%2==0) printf("\n");/*控制输出,每行四个*/<BR>   f1=f1+f2; /*前两个月加起来赋值给第三个月*/<BR>   f2=f1+f2; /*前两个月加起来赋值给第三个月*/<BR> }<BR>}<BR>==============================================================<BR>【程序12】<BR>题目:判断101-200之间有多少个素数,并输出所有素数。<BR>1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,<BR>      则表明此数不是素数,反之是素数。       <BR>2.程序源代码:<BR>#include "math.h"<BR>main()<BR>{<BR> int m,i,k,h=0,leap=1;<BR> printf("\n");<BR> for(m=101;m&lt;=200;m++)<BR>  { k=sqrt(m+1);<BR>   for(i=2;i&lt;=k;i++)<BR>     if(m%i==0)<BR>      {leap=0;break;}<BR>   if(leap) {printf("%-4d",m);h++;<BR>        if(h%10==0)<BR>        printf("\n");<BR>        }<BR>   leap=1;<BR>  }<BR> printf("\nThe total is %d",h);<BR>}<BR>==============================================================<BR>【程序13】<BR>题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数<BR>   本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。<BR>1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。<BR>2.程序源代码:<BR>main()<BR>{<BR>int i,j,k,n;<BR>printf("'water flower'number is:");<BR> for(n=100;n&lt;1000;n++)<BR> {<BR>  i=n/100;/*分解出百位*/<BR>  j=n/10%10;/*分解出十位*/<BR>  k=n%10;/*分解出个位*/<BR>  if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)<BR>   {<BR>   printf("%-5d",n);<BR>   }<BR> }<BR>printf("\n");<BR>}<BR>==============================================================<BR>【程序14】<BR>题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。</DIV>
<DIV>程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成: <BR>(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。<BR>(2)如果n&lt;&gt;k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,<BR> 重复执行第一步。<BR>(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。</DIV>
<DIV>2.程序源代码:<BR>/* zheng int is divided yinshu*/<BR>main()<BR>{<BR>int n,i;<BR>printf("\nplease input a number:\n");<BR>scanf("%d",&amp;n);<BR>printf("%d=",n);<BR>for(i=2;i&lt;=n;i++)<BR> {<BR>  while(n!=i)<BR>  {<BR>   if(n%i==0)<BR>   { printf("%d*",i);<BR>    n=n/i;<BR>   }<BR>   else<BR>    break;<BR>  }<BR>}<BR>printf("%d",n);}<BR>==============================================================<BR>【程序15】<BR>题目:利用条件运算符的嵌套来完成此题:学习成绩&gt;=90分的同学用A表示,60-89分之间的用B表示,<BR>   60分以下的用C表示。<BR>1.程序分析:(a&gt;b)?a:b这是条件运算符的基本例子。<BR>2.程序源代码:<BR>main()<BR>{<BR> int score;<BR> char grade;<BR> printf("please input a score\n");<BR> scanf("%d",&amp;score);<BR> grade=score&gt;=90?'A':(score&gt;=60?'B':'C');<BR> printf("%d belongs to %c",score,grade);<BR>}<BR>==============================================================<BR>【程序16】<BR>题目:输入两个正整数m和n,求其最大公约数和最小公倍数。<BR>1.程序分析:利用辗除法。</DIV>
<DIV>2.程序源代码:<BR>main()<BR>{<BR> int a,b,num1,num2,temp;<BR> printf("please input two numbers:\n");<BR> scanf("%d,%d",&amp;num1,&amp;num2);<BR> if(num1<BR> { temp=num1;<BR>  num1=num2; <BR>  num2=temp;<BR> }<BR>a=num1;b=num2;<BR>while(b!=0)/*利用辗除法,直到b为0为止*/<BR> {<BR>  temp=a%b;<BR>  a=b;<BR>  b=temp;<BR> }<BR>printf("gongyueshu:%d\n",a);<BR>printf("gongbeishu:%d\n",num1*num2/a);<BR>}<BR>==============================================================<BR>【程序17】<BR>题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。<BR>1.程序分析:利用while语句,条件为输入的字符不为'\n'.<BR>      <BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{char c;<BR> int letters=0,space=0,digit=0,others=0;<BR> printf("please input some characters\n");<BR> while((c=getchar())!='\n')<BR> {<BR> if(c&gt;='a'&amp;&amp;c&lt;='z'||c&gt;='A'&amp;&amp;c&lt;='Z')<BR>  letters++;<BR> else if(c==' ')<BR>  space++;<BR>   else if(c&gt;='0'&amp;&amp;c&lt;='9')<BR>       digit++;<BR>     else<BR>       others++;<BR>}<BR>printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,<BR>space,digit,others);<BR>}<BR>==============================================================<BR>【程序18】<BR>题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时<BR>   共有5个数相加),几个数相加有键盘控制。<BR>1.程序分析:关键是计算出每一项的值。<BR>2.程序源代码:<BR>main()<BR>{<BR> int a,n,count=1;<BR> long int sn=0,tn=0;<BR> printf("please input a and n\n");<BR> scanf("%d,%d",&amp;a,&amp;n);<BR> printf("a=%d,n=%d\n",a,n);<BR> while(count&lt;=n)<BR> {<BR>  tn=tn+a;<BR>  sn=sn+tn;<BR>  a=a*10;<BR>  ++count;<BR> }<BR>printf("a+aa+...=%ld\n",sn);<BR>}<BR>==============================================================<BR>【程序19】<BR>题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程<BR>   找出1000以内的所有完数。<BR>1. 程序分析:请参照程序&lt;--上页程序14. <BR>2.程序源代码:<BR>main()<BR>{<BR>static int k[10];<BR>int i,j,n,s;<BR>for(j=2;j&lt;1000;j++)<BR> {<BR> n=-1;<BR> s=j;<BR>  for(i=1;i&lt;J;I++)<BR>  {<BR>   if((j%i)==0)<BR>   { n++;<BR>    s=s-i;<BR>    k[n]=i;<BR>   }<BR>  }<BR> if(s==0)<BR> {<BR> printf("%d is a wanshu",j);<BR> for(i=0;i&lt;N;I++)<BR> printf("%d,",k);<BR> printf("%d\n",k[n]);<BR> }<BR>}<BR>}<BR>============================================================== <BR>【程序20】<BR>题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在<BR>   第10次落地时,共经过多少米?第10次反弹多高?<BR>1.程序分析:见下面注释<BR>2.程序源代码:<BR>main()<BR>{<BR>float sn=100.0,hn=sn/2;<BR>int n;<BR>for(n=2;n&lt;=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>}</DIV>
 楼主| 发表于 2005-11-21 01:38 | 显示全部楼层

回复:(风花雪月)[分享]c语言 ---- 经典百例[zz]

<DIV>【程序21】<BR>题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个<BR>   第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下<BR>   的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。<BR>1.程序分析:采取逆向思维的方法,从后往前推断。<BR>2.程序源代码:<BR>main()<BR>{<BR>int day,x1,x2;<BR>day=9;<BR>x2=1;<BR>while(day&gt;0)<BR> {x1=(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/<BR> x2=x1;<BR> day--;<BR> }<BR>printf("the total is %d\n",x1);<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>main()<BR>{<BR>char i,j,k;/*i是a的对手,j是b的对手,k是c的对手*/<BR>for(i='x';i&lt;='z';i++)<BR> for(j='x';j&lt;='z';j++)<BR> {<BR> if(i!=j)<BR>  for(k='x';k&lt;='z';k++)<BR>  { if(i!=k&amp;&amp;j!=k)<BR>   { if(i!='x'&amp;&amp;k!='x'&amp;&amp;k!='z')<BR>   printf("order is a--%c\tb--%c\tc--%c\n",i,j,k);<BR>   }<BR>  }<BR> }<BR>}<BR>==============================================================<BR>【程序23】 <BR>题目:打印出如下图案(菱形)</DIV>
<DIV>*<BR>***<BR>******<BR>********<BR>******<BR>***<BR>*<BR>1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重<BR>      for循环,第一层控制行,第二层控制列。 <BR>2.程序源代码:<BR>main()<BR>{<BR>int i,j,k;<BR>for(i=0;i&lt;=3;i++)<BR> {<BR> for(j=0;j&lt;=2-i;j++)<BR>  printf(" ");<BR> for(k=0;k&lt;=2*i;k++)<BR>  printf("*");<BR> printf("\n");<BR> }<BR>for(i=0;i&lt;=2;i++)<BR> {<BR> for(j=0;j&lt;=i;j++)<BR>  printf(" ");<BR> for(k=0;k&lt;=4-2*i;k++)<BR>  printf("*");<BR> printf("\n");<BR> }<BR>}<BR>==============================================================<BR>【程序24】 <BR>题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。<BR>1.程序分析:请抓住分子与分母的变化规律。 <BR>2.程序源代码:<BR>main()<BR>{<BR>int n,t,number=20;<BR>float a=2,b=1,s=0;<BR>for(n=1;n&lt;=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>}<BR>==============================================================<BR>【程序25】 <BR>题目:求1+2!+3!+...+20!的和<BR>1.程序分析:此程序只是把累加变成了累乘。 <BR>2.程序源代码:<BR>main()<BR>{<BR>float n,s=0,t=1;<BR>for(n=1;n&lt;=20;n++)<BR> {<BR> t*=n;<BR> s+=t;<BR> }<BR>printf("1+2!+3!...+20!=%e\n",s);<BR>}<BR>==============================================================<BR>【程序26】 <BR>题目:利用递归方法求5!。<BR>1.程序分析:递归公式:fn=fn_1*4!<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR>int i;<BR>int fact();<BR>for(i=0;i&lt;5;i++)<BR> printf("\40:%d!=%d\n",i,fact(i));<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>main()<BR>{<BR>int i=5;<BR>void palin(int n);<BR>printf("\40:");<BR>palin(i);<BR>printf("\n");<BR>}<BR>void palin(n)<BR>int n;<BR>{<BR>char next;<BR>if(n&lt;=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>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>{ printf("%d",age(5));<BR>}<BR>==============================================================<BR>【程序29】 <BR>题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。<BR>1. 程序分析:学会分解出每一位数,如下解释:(这里是一种简单的算法,师专数002班赵鑫提供) <BR>2.程序源代码:<BR>main( )<BR>{<BR>long a,b,c,d,e,x;<BR>scanf("%ld",&amp;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>}<BR>==============================================================<BR>【程序30】 <BR>题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。   <BR>1.程序分析:同29例<BR>2.程序源代码:<BR>main( )<BR>{<BR>long ge,shi,qian,wan,x;<BR>scanf("%ld",&amp;x);<BR>wan=x/10000;<BR>qian=x%10000/1000;<BR>shi=x%100/10;<BR>ge=x%10;<BR>if (ge==wan&amp;&amp;shi==qian)/*个位等于万位并且十位等于千位*/<BR> printf("this number is a huiwen\n");<BR>else<BR> printf("this number is not a huiwen\n");<BR>}</DIV>
 楼主| 发表于 2005-11-21 01:39 | 显示全部楼层

回复:(风花雪月)[分享]c语言 ---- 经典百例[zz]

<DIV>【程序31】<BR>题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续<BR>   判断第二个字母。<BR>1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。<BR>2.程序源代码:<BR>#include &lt;stdio.h&gt;<BR>void main()<BR>{<BR>char letter;<BR>printf("please input the first letter of someday\n");<BR>while ((letter=getch())!='Y')/*当所按字母为Y时才结束*/<BR>{ switch (letter)<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>}<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 &lt;conio.h&gt;<BR>void main(void)<BR>{<BR>int color;<BR>for (color = 0; color &lt; 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 &lt;conio.h&gt;<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>}<BR>==============================================================<BR>【程序34】<BR>题目:练习函数调用<BR>1. 程序分析: <BR>2.程序源代码:<BR>#include &lt;stdio.h&gt;<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 &lt;= 3; counter++)<BR>hello_world();/*调用此函数*/<BR>}<BR>void main(void)<BR>{<BR>three_hellos();/*调用此函数*/<BR>}<BR>==============================================================<BR>【程序35】<BR>题目:文本颜色设置<BR>1.程序分析:<BR>2.程序源代码:<BR>#include &lt;conio.h&gt;<BR>void main(void)<BR>{<BR>int color;<BR>for (color = 1; color &lt; 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>}<BR>==============================================================<BR>【程序36】<BR>题目:求100之内的素数   <BR>1.程序分析:<BR>2.程序源代码:<BR>#include &lt;stdio.h&gt;<BR>#include "math.h"<BR>#define N 101<BR>main()<BR>{<BR>int i,j,line,a[N];<BR>for(i=2;i&lt;N;i++) a=i;<BR>for(i=2;i&lt;sqrt(N);i++)<BR> for(j=i+1;j&lt;N;j++)<BR> {<BR>  if(a!=0&amp;&amp;a[j]!=0)<BR>  if(a[j]%a==0)<BR>  a[j]=0;}<BR>printf("\n");<BR>for(i=2,line=0;i&lt;N;i++)<BR>{<BR> if(a!=0)<BR> {printf("%5d",a);<BR> line++;}<BR> if(line==10)<BR> {printf("\n");<BR>line=0;}<BR>}<BR>}<BR>==============================================================<BR>【程序37】<BR>题目:对10个数进行排序<BR>1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,<BR>      下次类推,即用第二个元素与后8个进行比较,并进行交换。        <BR>2.程序源代码:<BR>#define N 10<BR>main()<BR>{int i,j,min,tem,a[N];<BR>/*input data*/<BR>printf("please input ten num:\n");<BR>for(i=0;i&lt;N;i++)<BR>{<BR>printf("a[%d]=",i);<BR>scanf("%d",&amp;a);}<BR>printf("\n");<BR>for(i=0;i&lt;N;i++)<BR>printf("%5d",a);<BR>printf("\n");<BR>/*sort ten num*/<BR>for(i=0;i&lt;N-1;i++)<BR>{min=i;<BR>for(j=i+1;j&lt;N;j++)<BR>if(a[min]&gt;a[j]) min=j;<BR>tem=a;<BR>a=a[min];<BR>a[min]=tem;<BR>}<BR>/*output data*/<BR>printf("After sorted \n");<BR>for(i=0;i&lt;N;i++)<BR>printf("%5d",a);<BR>}<BR>==============================================================<BR>【程序38】<BR>题目:求一个3*3矩阵对角线元素之和 <BR>1.程序分析:利用双重for循环控制输入二维数组,再将a累加后输出。<BR>2.程序源代码:<BR>main()<BR>{<BR>float a[3][3],sum=0;<BR>int i,j;<BR>printf("please input rectangle element:\n");<BR>for(i=0;i&lt;3;i++)<BR> for(j=0;j&lt;3;j++)<BR> scanf("%f",&amp;a[j]);<BR>for(i=0;i&lt;3;i++)<BR> sum=sum+a;<BR>printf("duijiaoxian he is %6.2f",sum);<BR>}<BR>==============================================================<BR>【程序39】<BR>题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。<BR>1. 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后<BR>     此元素之后的数,依次后移一个位置。 <BR>2.程序源代码:<BR>main()<BR>{<BR>int a[11]={1,4,6,9,13,16,19,28,40,100};<BR>int temp1,temp2,number,end,i,j;<BR>printf("original array is:\n");<BR>for(i=0;i&lt;10;i++)<BR> printf("%5d",a);<BR>printf("\n");<BR>printf("insert a new number:");<BR>scanf("%d",&amp;number);<BR>end=a[9];<BR>if(number&gt;end)<BR> a[10]=number;<BR>else<BR> {for(i=0;i&lt;10;i++)<BR>  { if(a&gt;number)<BR>   {temp1=a;<BR>    a=number;<BR>   for(j=i+1;j&lt;11;j++)<BR>   {temp2=a[j];<BR>    a[j]=temp1;<BR>    temp1=temp2;<BR>   }<BR>   break;<BR>   }<BR>  }<BR>}<BR>for(i=0;i&lt;11;i++)<BR> printf("%6d",a);<BR>}<BR>==============================================================<BR>【程序40】<BR>题目:将一个数组逆序输出。<BR>1.程序分析:用第一个与最后一个交换。<BR>2.程序源代码:<BR>#define N 5<BR>main()<BR>{ int a[N]={9,6,5,4,1},i,temp;<BR> printf("\n original array:\n");<BR> for(i=0;i&lt;N;i++)<BR> printf("%4d",a);<BR> for(i=0;i&lt;N/2;i++)<BR> {temp=a;<BR>  a=a[N-i-1];<BR>  a[N-i-1]=temp;<BR> }<BR>printf("\n sorted array:\n");<BR>for(i=0;i&lt;N;i++)<BR> printf("%4d",a);<BR>}</DIV>
 楼主| 发表于 2005-11-21 01:39 | 显示全部楼层

回复:(风花雪月)[分享]c语言 ---- 经典百例[zz]

<DIV>【程序41】<BR>题目:学习static定义静态变量的用法   <BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>varfunc()<BR>{<BR>int var=0;<BR>static int static_var=0;<BR>printf("\40:var equal %d \n",var);<BR>printf("\40:static var equal %d \n",static_var);<BR>printf("\n");<BR>var++;<BR>static_var++;<BR>}<BR>void main()<BR>{int i;<BR> for(i=0;i&lt;3;i++)<BR>  varfunc();<BR>}<BR>==============================================================<BR>【程序42】 <BR>题目:学习使用auto定义变量的用法<BR>1.程序分析:      <BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{int i,num;<BR>num=2;<BR> for (i=0;i&lt;3;i++)<BR> { printf("\40: The num equal %d \n",num);<BR>  num++;<BR>  {<BR>  auto int num=1;<BR>  printf("\40: The internal block num equal %d \n",num);<BR>  num++;<BR>  }<BR> }<BR>}<BR>==============================================================<BR>【程序43】<BR>题目:学习使用static的另一用法。   <BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR>int i,num;<BR>num=2;<BR>for(i=0;i&lt;3;i++)<BR>{<BR>printf("\40: The num equal %d \n",num);<BR>num++;<BR>{<BR>static int num=1;<BR>printf("\40:The internal block num equal %d\n",num);<BR>num++;<BR>}<BR>}<BR>}<BR>==============================================================<BR>【程序44】<BR>题目:学习使用external的用法。<BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>int a,b,c;<BR>void add()<BR>{ int a;<BR>a=3;<BR>c=a+b;<BR>}<BR>void main()<BR>{ a=b=4;<BR>add();<BR>printf("The value of c is equal to %d\n",c);<BR>}<BR>==============================================================<BR>【程序45】<BR>题目:学习使用register定义变量的方法。<BR>1.程序分析:<BR>2.程序源代码:<BR>void main()<BR>{<BR>register int i;<BR>int tmp=0;<BR>for(i=1;i&lt;=100;i++)<BR>tmp+=i;<BR>printf("The sum is %d\n",tmp);<BR>}<BR>==============================================================<BR>【程序46】<BR>题目:宏#define命令练习(1)   <BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#define TRUE 1<BR>#define FALSE 0<BR>#define SQ(x) (x)*(x)<BR>void main()<BR>{<BR>int num;<BR>int again=1;<BR>printf("\40: Program will stop if input value less than 50.\n");<BR>while(again)<BR>{<BR>printf("\40:Please input number==&gt;");<BR>scanf("%d",&amp;num);<BR>printf("\40:The square for this number is %d \n",SQ(num));<BR>if(num&gt;=50)<BR> again=TRUE;<BR>else<BR> again=FALSE;<BR>}<BR>}<BR>==============================================================<BR>【程序47】<BR>题目:宏#define命令练习(2)<BR>1.程序分析:            <BR>2.程序源代码:<BR>#include "stdio.h"<BR>#define exchange(a,b) { \ /*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上"\"*/<BR>            int t;\<BR>            t=a;\<BR>            a=b;\<BR>            b=t;\<BR>           }<BR>void main(void)<BR>{<BR>int x=10;<BR>int y=20;<BR>printf("x=%d; y=%d\n",x,y);<BR>exchange(x,y);<BR>printf("x=%d; y=%d\n",x,y);<BR>}<BR>==============================================================<BR>【程序48】<BR>题目:宏#define命令练习(3)   <BR>1.程序分析:<BR>2.程序源代码:<BR>#define LAG &gt;<BR>#define SMA &lt;<BR>#define EQ ==<BR>#include "stdio.h"<BR>void main()<BR>{ int i=10;<BR>int j=20;<BR>if(i LAG j)<BR>printf("\40: %d larger than %d \n",i,j);<BR>else if(i EQ j)<BR>printf("\40: %d equal to %d \n",i,j);<BR>else if(i SMA j)<BR>printf("\40:%d smaller than %d \n",i,j);<BR>else<BR>printf("\40: No such value.\n");<BR>}<BR>==============================================================<BR>【程序49】<BR>题目:#if #ifdef和#ifndef的综合应用。<BR>1. 程序分析: <BR>2.程序源代码:<BR>#include "stdio.h"<BR>#define MAX<BR>#define MAXIMUM(x,y) (x&gt;y)?x:y<BR>#define MINIMUM(x,y) (x&gt;y)?y:x<BR>void main()<BR>{ int a=10,b=20;<BR>#ifdef MAX<BR>printf("\40: The larger one is %d\n",MAXIMUM(a,b));<BR>#else<BR>printf("\40: The lower one is %d\n",MINIMUM(a,b));<BR>#endif<BR>#ifndef MIN<BR>printf("\40: The lower one is %d\n",MINIMUM(a,b));<BR>#else<BR>printf("\40: The larger one is %d\n",MAXIMUM(a,b));<BR>#endif<BR>#undef MAX<BR>#ifdef MAX<BR>printf("\40: The larger one is %d\n",MAXIMUM(a,b));<BR>#else<BR>printf("\40: The lower one is %d\n",MINIMUM(a,b));<BR>#endif<BR>#define MIN<BR>#ifndef MIN<BR>printf("\40: The lower one is %d\n",MINIMUM(a,b));<BR>#else<BR>printf("\40: The larger one is %d\n",MAXIMUM(a,b));<BR>#endif<BR>}<BR>==============================================================<BR>【程序50】<BR>题目:#include 的应用练习   <BR>1.程序分析:<BR>2.程序源代码:<BR>test.h 文件如下:<BR>#define LAG &gt;<BR>#define SMA &lt;<BR>#define EQ ==<BR>#include "test.h" /*一个新文件50.c,包含test.h*/<BR>#include "stdio.h"<BR>void main()<BR>{ int i=10;<BR>int j=20;<BR>if(i LAG j)<BR>printf("\40: %d larger than %d \n",i,j);<BR>else if(i EQ j)<BR>printf("\40: %d equal to %d \n",i,j);<BR>else if(i SMA j)<BR>printf("\40:%d smaller than %d \n",i,j);<BR>else<BR>printf("\40: No such value.\n");<BR>}</DIV>
 楼主| 发表于 2005-11-21 01:39 | 显示全部楼层

回复:(风花雪月)[分享]c语言 ---- 经典百例[zz]

<DIV>【程序51】<BR>题目:学习使用按位与 &amp; 。   <BR>1.程序分析:0&amp;0=0; 0&amp;1=0; 1&amp;0=0; 1&amp;1=1<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR>int a,b;<BR>a=077;<BR>b=a&amp;3;<BR>printf("\40: The a &amp; b(decimal) is %d \n",b);<BR>b&amp;=7;<BR>printf("\40: The a &amp; b(decimal) is %d \n",b);<BR>}<BR>==============================================================<BR>【程序52】<BR>题目:学习使用按位或 | 。<BR>1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1            <BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR>int a,b;<BR>a=077;<BR>b=a|3;<BR>printf("\40: The a &amp; b(decimal) is %d \n",b);<BR>b|=7;<BR>printf("\40: The a &amp; b(decimal) is %d \n",b);<BR>}<BR>==============================================================<BR>【程序53】<BR>题目:学习使用按位异或 ^ 。   <BR>1.程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR>int a,b;<BR>a=077;<BR>b=a^3;<BR>printf("\40: The a &amp; b(decimal) is %d \n",b);<BR>b^=7;<BR>printf("\40: The a &amp; b(decimal) is %d \n",b);<BR>}<BR>==============================================================<BR>【程序54】<BR>题目:取一个整数a从右端开始的4~7位。<BR>程序分析:可以这样考虑: <BR>(1)先使a右移4位。<BR>(2)设置一个低4位全为1,其余全为0的数。可用~(~0&lt;&lt;4)<BR>(3)将上面二者进行&amp;运算。<BR>2.程序源代码:<BR>main()<BR>{<BR>unsigned a,b,c,d;<BR>scanf("%o",&amp;a);<BR>b=a&gt;&gt;4;<BR>c=~(~0&lt;&lt;4);<BR>d=b&amp;c;<BR>printf("%o\n%o\n",a,d);<BR>}<BR>==============================================================<BR>【程序55】<BR>题目:学习使用按位取反~。   <BR>1.程序分析:~0=1; ~1=0;<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR>int a,b;<BR>a=234;<BR>b=~a;<BR>printf("\40: The a's 1 complement(decimal) is %d \n",b);<BR>a=~a;<BR>printf("\40: The a's 1 complement(hexidecimal) is %x \n",a);<BR>} <BR>==============================================================<BR>【程序56】<BR>题目:画图,学用circle画圆形。   <BR>1.程序分析:<BR>2.程序源代码:<BR>/*circle*/<BR>#include "graphics.h"<BR>main()<BR>{int driver,mode,i;<BR>float j=1,k=1;<BR>driver=VGA;mode=VGAHI;<BR>initgraph(&amp;driver,&amp;mode,"");<BR>setbkcolor(YELLOW);<BR>for(i=0;i&lt;=25;i++)<BR>{<BR>setcolor(8);<BR>circle(310,250,k);<BR>k=k+j;<BR>j=j+0.3;<BR>}<BR>} <BR>==============================================================<BR>【程序57】<BR>题目:画图,学用line画直线。<BR>1.程序分析:           <BR>2.程序源代码:<BR>#include "graphics.h"<BR>main()<BR>{int driver,mode,i;<BR>float x0,y0,y1,x1;<BR>float j=12,k;<BR>driver=VGA;mode=VGAHI;<BR>initgraph(&amp;driver,&amp;mode,"");<BR>setbkcolor(GREEN);<BR>x0=263;y0=263;y1=275;x1=275;<BR>for(i=0;i&lt;=18;i++)<BR>{<BR>setcolor(5);<BR>line(x0,y0,x0,y1);<BR>x0=x0-5;<BR>y0=y0-5;<BR>x1=x1+5;<BR>y1=y1+5;<BR>j=j+10;<BR>}<BR>x0=263;y1=275;y0=263;<BR>for(i=0;i&lt;=20;i++)<BR>{<BR>setcolor(5);<BR>line(x0,y0,x0,y1);<BR>x0=x0+5;<BR>y0=y0+5;<BR>y1=y1-5;<BR>}<BR>}<BR>==============================================================<BR>【程序58】<BR>题目:画图,学用rectangle画方形。   <BR>1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。<BR>2.程序源代码:<BR>#include "graphics.h"<BR>main()<BR>{int x0,y0,y1,x1,driver,mode,i;<BR>driver=VGA;mode=VGAHI;<BR>initgraph(&amp;driver,&amp;mode,"");<BR>setbkcolor(YELLOW);<BR>x0=263;y0=263;y1=275;x1=275;<BR>for(i=0;i&lt;=18;i++)<BR>{<BR>setcolor(1);<BR>rectangle(x0,y0,x1,y1);<BR>x0=x0-5;<BR>y0=y0-5;<BR>x1=x1+5;<BR>y1=y1+5;<BR>}<BR>settextstyle(DEFAULT_FONT,HORIZ_DIR,2);<BR>outtextxy(150,40,"How beautiful it is!");<BR>line(130,60,480,60);<BR>setcolor(2);<BR>circle(269,269,137);<BR>}<BR>==============================================================<BR>【程序59】<BR>题目:画图,综合例子。<BR>1.程序分析:<BR>2.程序源代码:<BR># define PAI 3.1415926<BR># define B 0.809<BR># include "graphics.h"<BR>#include "math.h"<BR>main()<BR>{<BR>int i,j,k,x0,y0,x,y,driver,mode;<BR>float a;<BR>driver=CGA;mode=CGAC0;<BR>initgraph(&amp;driver,&amp;mode,"");<BR>setcolor(3);<BR>setbkcolor(GREEN);<BR>x0=150;y0=100;<BR>circle(x0,y0,10);<BR>circle(x0,y0,20);<BR>circle(x0,y0,50);<BR>for(i=0;i&lt;16;i++)<BR>{<BR> a=(2*PAI/16)*i;<BR> x=ceil(x0+48*cos(a));<BR> y=ceil(y0+48*sin(a)*B);<BR> setcolor(2); line(x0,y0,x,y);}<BR>setcolor(3);circle(x0,y0,60);<BR>/* Make 0 time normal size letters */<BR>settextstyle(DEFAULT_FONT,HORIZ_DIR,0);<BR>outtextxy(10,170,"press a key");<BR>getch();<BR>setfillstyle(HATCH_FILL,YELLOW);<BR>floodfill(202,100,WHITE);<BR>getch();<BR>for(k=0;k&lt;=500;k++)<BR>{<BR> setcolor(3);<BR> for(i=0;i&lt;=16;i++)<BR> {<BR>  a=(2*PAI/16)*i+(2*PAI/180)*k;<BR>  x=ceil(x0+48*cos(a));<BR>  y=ceil(y0+48+sin(a)*B);<BR>  setcolor(2); line(x0,y0,x,y);<BR> }<BR> for(j=1;j&lt;=50;j++)<BR> {<BR>  a=(2*PAI/16)*i+(2*PAI/180)*k-1;<BR>  x=ceil(x0+48*cos(a));<BR>  y=ceil(y0+48*sin(a)*B);<BR>  line(x0,y0,x,y);<BR> }<BR>}<BR>restorecrtmode();<BR>}<BR>==============================================================<BR>【程序60】<BR>题目:画图,综合例子。   <BR>1.程序分析:<BR>2.程序源代码:<BR>#include "graphics.h"<BR>#define LEFT 0<BR>#define TOP 0<BR>#define RIGHT 639<BR>#define BOTTOM 479<BR>#define LINES 400<BR>#define MAXCOLOR 15<BR>main()<BR>{<BR>int driver,mode,error;<BR>int x1,y1;<BR>int x2,y2;<BR>int dx1,dy1,dx2,dy2,i=1;<BR>int count=0;<BR>int color=0;<BR>driver=VGA;<BR>mode=VGAHI;<BR>initgraph(&amp;driver,&amp;mode,"");<BR>x1=x2=y1=y2=10;<BR>dx1=dy1=2;<BR>dx2=dy2=3;<BR>while(!kbhit())<BR>{<BR> line(x1,y1,x2,y2);<BR> x1+=dx1;y1+=dy1;<BR> x2+=dx2;y2+dy2;<BR> if(x1&lt;=LEFT||x1&gt;=RIGHT)<BR> dx1=-dx1;<BR> if(y1&lt;=TOP||y1&gt;=BOTTOM)<BR>  dy1=-dy1;<BR> if(x2&lt;=LEFT||x2&gt;=RIGHT)<BR>  dx2=-dx2;<BR> if(y2&lt;=TOP||y2&gt;=BOTTOM)<BR>  dy2=-dy2;<BR> if(++count&gt;LINES)<BR> {<BR>  setcolor(color);<BR>  color=(color&gt;=MAXCOLOR)?0:++color;<BR> }<BR>}<BR>closegraph();<BR>}</DIV>
 楼主| 发表于 2005-11-21 01:40 | 显示全部楼层

回复:(风花雪月)[分享]c语言 ---- 经典百例[zz]

<DIV>【程序61】<BR>题目:打印出杨辉三角形(要求打印出10行如下图)   <BR>1.程序分析:<BR>       1<BR>      1  1<BR>      1  2  1<BR>      1  3  3  1<BR>      1  4  6  4  1<BR>      1  5  10 10 5  1  <BR>2.程序源代码:<BR>main()<BR>{int i,j;<BR>int a[10][10];<BR>printf("\n");<BR>for(i=0;i&lt;10;i++)<BR> {a[0]=1;<BR> a=1;}<BR>for(i=2;i&lt;10;i++)<BR> for(j=1;j&lt;i;j++)<BR> a[j]=a[i-1][j-1]+a[i-1][j];<BR>for(i=0;i&lt;10;i++)<BR> {for(j=0;j&lt;=i;j++)<BR> printf("%5d",a[j]);<BR> printf("\n");<BR> }<BR>}<BR>==============================================================<BR>【程序62】<BR>题目:学习putpixel画点。<BR>1.程序分析:            <BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include "graphics.h"<BR>main()<BR>{<BR>int i,j,driver=VGA,mode=VGAHI;<BR>initgraph(&amp;driver,&amp;mode,"");<BR>setbkcolor(YELLOW);<BR>for(i=50;i&lt;=230;i+=20)<BR> for(j=50;j&lt;=230;j++)<BR> putpixel(i,j,1);<BR>for(j=50;j&lt;=230;j+=20)<BR> for(i=50;i&lt;=230;i++)<BR> putpixel(i,j,1);<BR>}<BR>==============================================================<BR>【程序63】<BR>题目:画椭圆ellipse   <BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include "graphics.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int x=360,y=160,driver=VGA,mode=VGAHI;<BR>int num=20,i;<BR>int top,bottom;<BR>initgraph(&amp;driver,&amp;mode,"");<BR>top=y-30;<BR>bottom=y-30;<BR>for(i=0;i&lt;num;i++)<BR>{<BR>ellipse(250,250,0,360,top,bottom);<BR>top-=5;<BR>bottom+=5;<BR>}<BR>getch();<BR>}<BR>==============================================================<BR>【程序64】<BR>题目:利用ellipse and rectangle 画图。<BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include "graphics.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int driver=VGA,mode=VGAHI;<BR>int i,num=15,top=50;<BR>int left=20,right=50;<BR>initgraph(&amp;driver,&amp;mode,"");<BR>for(i=0;i&lt;num;i++)<BR>{<BR>ellipse(250,250,0,360,right,left);<BR>ellipse(250,250,0,360,20,top);<BR>rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));<BR>right+=5;<BR>left+=5;<BR>top+=10;<BR>}<BR>getch();<BR>}<BR>==============================================================<BR>【程序65】<BR>题目:一个最优美的图案。   <BR>1.程序分析:<BR>2.程序源代码:<BR>#include "graphics.h"<BR>#include "math.h"<BR>#include "dos.h"<BR>#include "conio.h"<BR>#include "stdlib.h"<BR>#include "stdio.h"<BR>#include "stdarg.h"<BR>#define MAXPTS 15<BR>#define PI 3.1415926<BR>struct PTS {<BR>int x,y;<BR>};<BR>double AspectRatio=0.85;<BR>void LineToDemo(void)<BR>{<BR>struct viewporttype vp;<BR>struct PTS points[MAXPTS];<BR>int i, j, h, w, xcenter, ycenter;<BR>int radius, angle, step;<BR>double rads;<BR>printf(" MoveTo / LineTo Demonstration" );<BR>getviewsettings( &amp;vp );<BR>h = vp.bottom - vp.top;<BR>w = vp.right - vp.left;<BR>xcenter = w / 2; /* Determine the center of circle */<BR>ycenter = h / 2;<BR>radius = (h - 30) / (AspectRatio * 2);<BR>step = 360 / MAXPTS; /* Determine # of increments */<BR>angle = 0; /* Begin at zero degrees */<BR>for( i=0 ; i&lt;MAXPTS ; ++i ){ /* Determine circle intercepts */<BR>rads = (double)angle * PI / 180.0; /* Convert angle to radians */<BR>points.x = xcenter + (int)( cos(rads) * radius );<BR>points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );<BR>angle += step; /* Move to next increment */<BR>}<BR>circle( xcenter, ycenter, radius ); /* Draw bounding circle */<BR>for( i=0 ; i&lt;MAXPTS ; ++i ){ /* Draw the cords to the circle */<BR>for( j=i ; j&lt;MAXPTS ; ++j ){ /* For each remaining intersect */<BR>moveto(points.x, points.y); /* Move to beginning of cord */<BR>lineto(points[j].x, points[j].y); /* Draw the cord */<BR>} } }<BR>main()<BR>{int driver,mode;<BR>driver=CGA;mode=CGAC0;<BR>initgraph(&amp;driver,&amp;mode,"");<BR>setcolor(3);<BR>setbkcolor(GREEN);<BR>LineToDemo();}<BR>==============================================================<BR>【程序66】<BR>题目:输入3个数a,b,c,按大小顺序输出。   <BR>1.程序分析:利用指针方法。<BR>2.程序源代码:<BR>/*pointer*/<BR>main()<BR>{<BR>int n1,n2,n3;<BR>int *pointer1,*pointer2,*pointer3;<BR>printf("please input 3 number:n1,n2,n3:");<BR>scanf("%d,%d,%d",&amp;n1,&amp;n2,&amp;n3);<BR>pointer1=&amp;n1;<BR>pointer2=&amp;n2;<BR>pointer3=&amp;n3;<BR>if(n1&gt;n2) swap(pointer1,pointer2);<BR>if(n1&gt;n3) swap(pointer1,pointer3);<BR>if(n2&gt;n3) swap(pointer2,pointer3);<BR>printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);<BR>}<BR>swap(p1,p2)<BR>int *p1,*p2;<BR>{int p;<BR>p=*p1;*p1=*p2;*p2=p;<BR>}<BR>==============================================================<BR>【程序67】<BR>题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。<BR>1.程序分析:谭浩强的书中答案有问题。      <BR>2.程序源代码:<BR>main()<BR>{<BR>int number[10];<BR>input(number);<BR>max_min(number);<BR>output(number);<BR>}<BR>input(number)<BR>int number[10];<BR>{int i;<BR>for(i=0;i&lt;9;i++)<BR> scanf("%d,",&amp;number);<BR> scanf("%d",&amp;number[9]);<BR>}<BR>max_min(array)<BR>int array[10];<BR>{int *max,*min,k,l;<BR>int *p,*arr_end;<BR>arr_end=array+10;<BR>max=min=array;<BR>for(p=array+1;p&lt;arr_end;p++)<BR> if(*p&gt;*max) max=p;<BR> else if(*p&lt;*min) min=p;<BR> k=*max;<BR> l=*min;<BR> *p=array[0];array[0]=l;l=*p;<BR> *p=array[9];array[9]=k;k=*p;<BR> return;<BR>}<BR>output(array)<BR>int array[10];<BR>{ int *p;<BR>for(p=array;p&lt;array+9;p++)<BR> printf("%d,",*p);<BR>printf("%d\n",array[9]);<BR>}<BR>==============================================================<BR>【程序68】<BR>题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数<BR>1.程序分析:<BR>2.程序源代码:<BR>main()<BR>{<BR>int number[20],n,m,i;<BR>printf("the total numbers is:");<BR>scanf("%d",&amp;n);<BR>printf("back m:");<BR>scanf("%d",&amp;m);<BR>for(i=0;i&lt;n-1;i++)<BR> scanf("%d,",&amp;number);<BR>scanf("%d",&amp;number[n-1]);<BR>move(number,n,m);<BR>for(i=0;i&lt;n-1;i++)<BR> printf("%d,",number);<BR>printf("%d",number[n-1]);<BR>}<BR>move(array,n,m)<BR>int n,m,array[20];<BR>{<BR>int *p,array_end;<BR>array_end=*(array+n-1);<BR>for(p=array+n-1;p&gt;array;p--)<BR> *p=*(p-1);<BR> *array=array_end;<BR> m--;<BR> if(m&gt;0) move(array,n,m);<BR>}<BR>==============================================================<BR>【程序69】<BR>题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出<BR>   圈子,问最后留下的是原来第几号的那位。<BR>1. 程序分析:<BR>2.程序源代码:<BR>#define nmax 50<BR>main()<BR>{<BR>int i,k,m,n,num[nmax],*p;<BR>printf("please input the total of numbers:");<BR>scanf("%d",&amp;n);<BR>p=num;<BR>for(i=0;i&lt;n;i++)<BR> *(p+i)=i+1;<BR> i=0;<BR> k=0;<BR> m=0;<BR> while(m&lt;n-1)<BR> {<BR> if(*(p+i)!=0) k++;<BR> if(k==3)<BR> { *(p+i)=0;<BR> k=0;<BR> m++;<BR> }<BR>i++;<BR>if(i==n) i=0;<BR>}<BR>while(*p==0) p++;<BR>printf("%d is left\n",*p);<BR>}<BR>==============================================================<BR>【程序70】<BR>题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。   <BR>1.程序分析:<BR>2.程序源代码:<BR>main()<BR>{<BR>int len;<BR>char *str[20];<BR>printf("please input a string:\n");<BR>scanf("%s",str);<BR>len=length(str);<BR>printf("the string has %d characters.",len);<BR>}<BR>length(p)<BR>char *p;<BR>{<BR>int n;<BR>n=0;<BR>while(*p!='\0')<BR>{<BR> n++;<BR> p++;<BR>}<BR>return n;<BR>}</DIV>
 楼主| 发表于 2005-11-21 01:40 | 显示全部楼层

回复:(风花雪月)[分享]c语言 ---- 经典百例[zz]

<DIV>程序71】<BR>题目:编写input()和output()函数输入,输出5个学生的数据记录。<BR>1.程序分析:<BR>2.程序源代码:<BR>#define N 5<BR>struct student<BR>{ char num[6];<BR> char name[8];<BR> int score[4];<BR>} stu[N];<BR>input(stu)<BR>struct student stu[];<BR>{ int i,j;<BR> for(i=0;i&lt;N;i++)<BR> { printf("\n please input %d of %d\n",i+1,N);<BR>  printf("num: ");<BR>  scanf("%s",stu.num);<BR>  printf("name: ");<BR>  scanf("%s",stu.name);<BR>   for(j=0;j&lt;3;j++)<BR>   { printf("score %d.",j+1);<BR>    scanf("%d",&amp;stu.score[j]);<BR>   }<BR>  printf("\n");<BR> }<BR>}<BR>print(stu)<BR>struct student stu[];<BR>{ int i,j;<BR>printf("\nNo. Name Sco1 Sco2 Sco3\n");<BR>for(i=0;i&lt;N;i++)<BR>{ printf("%-6s%-10s",stu.num,stu.name);<BR> for(j=0;j&lt;3;j++)<BR>  printf("%-8d",stu.score[j]);<BR> printf("\n");<BR>}<BR>}<BR>main()<BR>{<BR> input();<BR> print();<BR>}<BR>==============================================================<BR>【程序72】<BR>题目:创建一个链表。<BR>1.程序分析:           <BR>2.程序源代码:<BR>/*creat a list*/<BR>#include "stdlib.h"<BR>#include "stdio.h"<BR>struct list<BR>{ int data;<BR>struct list *next;<BR>};<BR>typedef struct list node;<BR>typedef node *link;<BR>void main()<BR>{ link ptr,head;<BR>int num,i;<BR>ptr=(link)malloc(sizeof(node));<BR>ptr=head;<BR>printf("please input 5 numbers==&gt;\n");<BR>for(i=0;i&lt;=4;i++)<BR>{<BR> scanf("%d",&amp;num);<BR> ptr-&gt;data=num;<BR> ptr-&gt;next=(link)malloc(sizeof(node));<BR> if(i==4) ptr-&gt;next=NULL;<BR> else ptr=ptr-&gt;next;<BR>}<BR>ptr=head;<BR>while(ptr!=NULL)<BR>{ printf("The value is ==&gt;%d\n",ptr-&gt;data);<BR> ptr=ptr-&gt;next;<BR>}<BR>}<BR>==============================================================<BR>【程序73】<BR>题目:反向输出一个链表。   <BR>1.程序分析:<BR>2.程序源代码:<BR>/*reverse output a list*/<BR>#include "stdlib.h"<BR>#include "stdio.h"<BR>struct list<BR>{ int data;<BR> struct list *next;<BR>};<BR>typedef struct list node;<BR>typedef node *link;<BR>void main()<BR>{ link ptr,head,tail; <BR> int num,i;<BR> tail=(link)malloc(sizeof(node));<BR> tail-&gt;next=NULL;<BR> ptr=tail;<BR> printf("\nplease input 5 data==&gt;\n");<BR> for(i=0;i&lt;=4;i++)<BR> {<BR>  scanf("%d",&amp;num);<BR>  ptr-&gt;data=num;<BR>  head=(link)malloc(sizeof(node));<BR>  head-&gt;next=ptr;<BR>  ptr=head;<BR> }<BR>ptr=ptr-&gt;next;<BR>while(ptr!=NULL)<BR>{ printf("The value is ==&gt;%d\n",ptr-&gt;data);<BR> ptr=ptr-&gt;next;<BR>}}<BR>==============================================================<BR>【程序74】<BR>题目:连接两个链表。<BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdlib.h"<BR>#include "stdio.h"<BR>struct list<BR>{ int data;<BR>struct list *next;<BR>};<BR>typedef struct list node;<BR>typedef node *link;<BR>link delete_node(link pointer,link tmp)<BR>{if (tmp==NULL) /*delete first node*/<BR> return pointer-&gt;next;<BR>else<BR>{ if(tmp-&gt;next-&gt;next==NULL)/*delete last node*/<BR>  tmp-&gt;next=NULL;<BR> else /*delete the other node*/<BR>  tmp-&gt;next=tmp-&gt;next-&gt;next;<BR> return pointer;<BR>}<BR>}<BR>void selection_sort(link pointer,int num)<BR>{ link tmp,btmp;<BR> int i,min;<BR> for(i=0;i&lt;num;i++)<BR> {<BR> tmp=pointer;<BR> min=tmp-&gt;data;<BR> btmp=NULL;<BR> while(tmp-&gt;next)<BR> { if(min&gt;tmp-&gt;next-&gt;data)<BR> {min=tmp-&gt;next-&gt;data;<BR>  btmp=tmp;<BR> }<BR> tmp=tmp-&gt;next;<BR> }<BR>printf("\40: %d\n",min);<BR>pointer=delete_node(pointer,btmp);<BR>}<BR>}<BR>link create_list(int array[],int num)<BR>{ link tmp1,tmp2,pointer;<BR>int i;<BR>pointer=(link)malloc(sizeof(node));<BR>pointer-&gt;data=array[0];<BR>tmp1=pointer;<BR>for(i=1;i&lt;num;i++)<BR>{ tmp2=(link)malloc(sizeof(node));<BR> tmp2-&gt;next=NULL;<BR> tmp2-&gt;data=array;<BR> tmp1-&gt;next=tmp2;<BR> tmp1=tmp1-&gt;next;<BR>}<BR>return pointer;<BR>}<BR>link concatenate(link pointer1,link pointer2)<BR>{ link tmp;<BR>tmp=pointer1;<BR>while(tmp-&gt;next)<BR> tmp=tmp-&gt;next;<BR>tmp-&gt;next=pointer2;<BR>return pointer1;<BR>}<BR>void main(void)<BR>{ int arr1[]={3,12,8,9,11};<BR> link ptr;<BR> ptr=create_list(arr1,5);<BR> selection_sort(ptr,5);<BR>}<BR>==============================================================<BR>【程序75】<BR>题目:放松一下,算一道简单的题目。<BR>1.程序分析:<BR>2.程序源代码:<BR>main()<BR>{<BR>int i,n;<BR>for(i=1;i&lt;5;i++)<BR>{ n=0;<BR> if(i!=1)<BR> n=n+1;<BR> if(i==3)<BR> n=n+1;<BR> if(i==4)<BR> n=n+1;<BR> if(i!=4)<BR> n=n+1;<BR> if(n==3)<BR>  printf("zhu hao shi de shi:%c",64+i);<BR> }<BR>}<BR>==============================================================<BR>【程序76】<BR>题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数<BR>   1/1+1/3+...+1/n(利用指针函数)<BR>1.程序分析:<BR>2.程序源代码:<BR>main()<BR>#include "stdio.h"<BR>main()<BR>{<BR>float peven(),podd(),dcall();<BR>float sum;<BR>int n;<BR>while (1)<BR>{<BR> scanf("%d",&amp;n);<BR> if(n&gt;1)<BR>  break;<BR>}<BR>if(n%2==0)<BR>{<BR> printf("Even=");<BR> sum=dcall(peven,n);<BR>}<BR>else<BR>{<BR> printf("Odd=");<BR> sum=dcall(podd,n);<BR>}<BR>printf("%f",sum);<BR>}<BR>float peven(int n)<BR>{<BR>float s;<BR>int i;<BR>s=1;<BR>for(i=2;i&lt;=n;i+=2)<BR> s+=1/(float)i;<BR>return(s);<BR>}<BR>float podd(n)<BR>int n;<BR>{<BR>float s;<BR>int i;<BR>s=0;<BR>for(i=1;i&lt;=n;i+=2)<BR> s+=1/(float)i;<BR>return(s);<BR>}<BR>float dcall(fp,n)<BR>float (*fp)();<BR>int n;<BR>{<BR>float s;<BR>s=(*fp)(n);<BR>return(s);<BR>}<BR>==============================================================<BR>【程序77】<BR>题目:填空练习(指向指针的指针)<BR>1.程序分析:     <BR>2.程序源代码:<BR>main()<BR>{ char *s[]={"man","woman","girl","boy","sister"};<BR>char **q;<BR>int k;<BR>for(k=0;k&lt;5;k++)<BR>{       ;/*这里填写什么语句*/<BR> printf("%s\n",*q);<BR>}<BR>}<BR>==============================================================<BR>【程序78】<BR>题目:找到年龄最大的人,并输出。请找出程序中有什么问题。<BR>1.程序分析:<BR>2.程序源代码:<BR>#define N 4<BR>#include "stdio.h"<BR>static struct man<BR>{ char name[20];<BR>int age;<BR>} person[N]={"li",18,"wang",19,"zhang",20,"sun",22};<BR>main()<BR>{struct man *q,*p;<BR>int i,m=0;<BR>p=person;<BR>for (i=0;i&lt;N;i++)<BR>{if(m&lt;p-&gt;age)<BR> q=p++;<BR> m=q-&gt;age;}<BR>printf("%s,%d",(*q).name,(*q).age);<BR>}<BR>==============================================================<BR>【程序79】<BR>题目:字符串排序。<BR>1.程序分析:<BR>2.程序源代码:<BR>main()<BR>{<BR>char *str1[20],*str2[20],*str3[20];<BR>char swap();<BR>printf("please input three strings\n");<BR>scanf("%s",str1);<BR>scanf("%s",str2);<BR>scanf("%s",str3);<BR>if(strcmp(str1,str2)&gt;0) swap(str1,str2);<BR>if(strcmp(str1,str3)&gt;0) swap(str1,str3);<BR>if(strcmp(str2,str3)&gt;0) swap(str2,str3);<BR>printf("after being sorted\n");<BR>printf("%s\n%s\n%s\n",str1,str2,str3);<BR>}<BR>char swap(p1,p2)<BR>char *p1,*p2;<BR>{<BR>char *p[20];<BR>strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);<BR>}<BR>==============================================================<BR>【程序80】<BR>题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只<BR>   猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了<BR>   一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,<BR>   问海滩上原来最少有多少个桃子?<BR>1.程序分析:<BR>2.程序源代码:<BR>main()<BR>{int i,m,j,k,count;<BR>for(i=4;i&lt;10000;i+=4)<BR>{ count=0;<BR>m=i;<BR>for(k=0;k&lt;5;k++)<BR>{<BR> j=i/4*5+1;<BR> i=j;<BR> if(j%4==0)<BR>  count++;<BR> else<BR>  break;<BR>}<BR> i=m;<BR> if(count==4)<BR> {printf("%d\n",count);<BR>  break;}<BR>}<BR>}</DIV>
 楼主| 发表于 2005-11-21 01:41 | 显示全部楼层

回复:(风花雪月)[分享]c语言 ---- 经典百例[zz]

<DIV>【程序81】<BR>题目:809*??=800*??+9*??+1 其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。<BR>1.程序分析:<BR>2.程序源代码:<BR>output(long b,long i)<BR>{ printf("\n%ld/%ld=809*%ld+%ld",b,i,i,b%i);<BR>}<BR>main()<BR>{long int a,b,i;<BR>a=809;<BR>for(i=10;i&lt;100;i++)<BR>{b=i*a+1;<BR>if(b&gt;=1000&amp;&amp;b&lt;=10000&amp;&amp;8*i&lt;100&amp;&amp;9*i&gt;=100)<BR>output(b,i); }<BR>}<BR>==============================================================<BR>【程序82】<BR>题目:八进制转换为十进制<BR>1.程序分析:           <BR>2.程序源代码:<BR>main()<BR>{ char *p,s[6];int n;<BR>p=s;<BR>gets(p);<BR>n=0;<BR>while(*(p)!='\0')<BR>{n=n*8+*p-'0';<BR>p++;}<BR>printf("%d",n);<BR>}<BR>==============================================================<BR>【程序83】<BR>题目:求0—7所能组成的奇数个数。<BR>1.程序分析:<BR>2.程序源代码:<BR>main()<BR>{<BR>long sum=4,s=4;<BR>int j;<BR>for(j=2;j&lt;=8;j++)/*j is place of number*/<BR>{ printf("\n%ld",sum);<BR>if(j&lt;=2)<BR>s*=7;<BR>else<BR>s*=8;<BR>sum+=s;}<BR>printf("\nsum=%ld",sum);<BR>}<BR>==============================================================<BR>【程序84】<BR>题目:一个偶数总能表示为两个素数之和。<BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include "math.h"<BR>main()<BR>{ int a,b,c,d;<BR>scanf("%d",&amp;a);<BR>for(b=3;b&lt;=a/2;b+=2)<BR>{ for(c=2;c&lt;=sqrt(b);c++)<BR>if(b%c==0) break;<BR>if(c&gt;sqrt(b))<BR>d=a-b;<BR>else<BR>break;<BR>for(c=2;c&lt;=sqrt(d);c++)<BR>if(d%c==0) break;<BR>if(c&gt;sqrt(d))<BR>printf("%d=%d+%d\n",a,b,d);<BR>}<BR>}<BR>==============================================================<BR>【程序85】<BR>题目:判断一个素数能被几个9整除<BR>1.程序分析:<BR>2.程序源代码:<BR>main()<BR>{ long int m9=9,sum=9;<BR>int zi,n1=1,c9=1;<BR>scanf("%d",&amp;zi);<BR>while(n1!=0)<BR>{ if(!(sum%zi))<BR>n1=0;<BR>else<BR>{m9=m9*10;<BR>sum=sum+m9;<BR>c9++;<BR>}<BR>}<BR>printf("%ld,can be divided by %d \"9\"",sum,c9);<BR>}<BR>==============================================================<BR>【程序86】<BR>题目:两个字符串连接程序<BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{char a[]="acegikm";<BR>char b[]="bdfhjlnpq";<BR>char c[80],*p;<BR>int i=0,j=0,k=0;<BR>while(a!='\0'&amp;&amp;b[j]!='\0')<BR>{if (a&lt;B[J])<BR>{ c[k]=a;i++;}<BR>else<BR>c[k]=b[j++];<BR>k++;<BR>}<BR>c[k]='\0';<BR>if(a=='\0')<BR>p=b+j;<BR>else<BR>p=a+i;<BR>strcat(c,p);<BR>puts(c);<BR>}<BR>==============================================================<BR>【程序87】<BR>题目:回答结果(结构体变量传递)<BR>1.程序分析:     <BR>2.程序源代码:<BR>#include "stdio.h"<BR>struct student<BR>{ int x;<BR>char c;<BR>} a;<BR>main()<BR>{a.x=3;<BR>a.c='a';<BR>f(a);<BR>printf("%d,%c",a.x,a.c);<BR>}<BR>f(struct student b)<BR>{<BR>b.x=20;<BR>b.c='y';<BR>}<BR>==============================================================<BR>【程序88】<BR>题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。<BR>1.程序分析:<BR>2.程序源代码:<BR>main()<BR>{int i,a,n=1;<BR>while(n&lt;=7)<BR>{ do {<BR>   scanf("%d",&amp;a);<BR>   }while(a&lt;1||a&gt;50);<BR>for(i=1;i&lt;=a;i++)<BR> printf("*");<BR>printf("\n");<BR>n++;}<BR>getch();<BR>}<BR>==============================================================<BR>【程序89】<BR>题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:<BR>   每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。<BR>1.程序分析:<BR>2.程序源代码:<BR>main()<BR>{int a,i,aa[4],t;<BR>scanf("%d",&amp;a);<BR>aa[0]=a%10;<BR>aa[1]=a%100/10;<BR>aa[2]=a%1000/100;<BR>aa[3]=a/1000;<BR>for(i=0;i&lt;=3;i++)<BR> {aa+=5;<BR> aa%=10;<BR> }<BR>for(i=0;i&lt;=3/2;i++)<BR> {t=aa;<BR> aa=aa[3-i];<BR> aa[3-i]=t;<BR> }<BR>for(i=3;i&gt;=0;i--)<BR>printf("%d",aa);<BR>}<BR>==============================================================<BR>【程序90】<BR>题目:专升本一题,读结果。<BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#define M 5<BR>main()<BR>{int a[M]={1,2,3,4,5};<BR>int i,j,t;<BR>i=0;j=M-1;<BR>while(i&lt;J)<BR>{t=*(a+i);<BR>*(a+i)=*(a+j);<BR>*(a+j)=t;<BR>i++;j--;<BR>}<BR>for(i=0;i&lt;M;I++)<BR>printf("%d",*(a+i));<BR>}</DIV>
 楼主| 发表于 2005-11-21 01:41 | 显示全部楼层

回复:(风花雪月)[分享]c语言 ---- 经典百例[zz]

【程序91】<BR>题目:时间函数举例1<BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include "time.h"<BR>void main()<BR>{ time_t lt; /*define a longint time varible*/<BR>lt=time(NULL);/*system time and date*/<BR>printf(ctime(&lt;)); /*english format output*/<BR>printf(asctime(localtime(&lt;)));/*tranfer to tm*/<BR>printf(asctime(gmtime(&lt;))); /*tranfer to Greenwich time*/<BR>}<BR>==============================================================<BR>【程序92】<BR>题目:时间函数举例2<BR>1.程序分析:           <BR>2.程序源代码:<BR>/*calculate time*/<BR>#include "time.h"<BR>#include "stdio.h"<BR>main()<BR>{ time_t start,end;<BR>int i;<BR>start=time(NULL);<BR>for(i=0;i&lt;3000;i++)<BR>{ printf("\1\1\1\1\1\1\1\1\1\1\n");}<BR>end=time(NULL);<BR>printf("\1: The different is %6.3f\n",difftime(end,start));<BR>}<BR>==============================================================<BR>【程序93】<BR>题目:时间函数举例3<BR>1.程序分析:<BR>2.程序源代码:<BR>/*calculate time*/<BR>#include "time.h"<BR>#include "stdio.h"<BR>main()<BR>{ clock_t start,end;<BR>int i;<BR>double var;<BR>start=clock();<BR>for(i=0;i&lt;10000;i++)<BR>{ printf("\1\1\1\1\1\1\1\1\1\1\n");}<BR>end=clock();<BR>printf("\1: The different is %6.3f\n",(double)(end-start));<BR>}<BR>==============================================================<BR>【程序94】<BR>题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)<BR>1.程序分析:<BR>2.程序源代码:<BR>#include "time.h"<BR>#include "stdlib.h"<BR>#include "stdio.h"<BR>main()<BR>{char c;<BR>clock_t start,end;<BR>time_t a,b;<BR>double var;<BR>int i,guess;<BR>srand(time(NULL));<BR>printf("do you want to play it.('y' or 'n') \n");<BR>loop:<BR>while((c=getchar())=='y')<BR>{<BR>i=rand()%100;<BR>printf("\nplease input number you guess:\n");<BR>start=clock();<BR>a=time(NULL);<BR>scanf("%d",&amp;guess);<BR>while(guess!=i)<BR>{if(guess&gt;i)<BR>{printf("please input a little smaller.\n");<BR>scanf("%d",&amp;guess);}<BR>else<BR>{printf("please input a little bigger.\n");<BR>scanf("%d",&amp;guess);}<BR>}<BR>end=clock();<BR>b=time(NULL);<BR>printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);<BR>printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));<BR>if(var&lt;15)<BR>printf("\1\1 You are very clever! \1\1\n\n");<BR>else if(var&lt;25)<BR>printf("\1\1 you are normal! \1\1\n\n");<BR>else<BR>printf("\1\1 you are stupid! \1\1\n\n");<BR>printf("\1\1 Congradulations \1\1\n\n");<BR>printf("The number you guess is %d",i);<BR>}<BR>printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");<BR>if((c=getch())=='y')<BR>goto loop;<BR>}<BR>==============================================================<BR>【程序95】<BR>题目:家庭财务管理小程序<BR>1.程序分析:<BR>2.程序源代码:<BR>/*money management system*/<BR>#include "stdio.h"<BR>#include "dos.h"<BR>main()<BR>{<BR>FILE *fp;<BR>struct date d;<BR>float sum,chm=0.0;<BR>int len,i,j=0;<BR>int c;<BR>char ch[4]="",ch1[16]="",chtime[12]="",chshop[16],chmoney[8];<BR>pp: clrscr();<BR>sum=0.0;<BR>gotoxy(1,1);printf("|---------------------------------------------------------------------------|");<BR>gotoxy(1,2);printf("| money management system(C1.0) 2000.03 |");<BR>gotoxy(1,3);printf("|---------------------------------------------------------------------------|");<BR>gotoxy(1,4);printf("| -- money records -- | -- today cost list -- |");<BR>gotoxy(1,5);printf("| ------------------------ |-------------------------------------|");<BR>gotoxy(1,6);printf("| date: -------------- | |");<BR>gotoxy(1,7);printf("| | | | |");<BR>gotoxy(1,8);printf("| -------------- | |");<BR>gotoxy(1,9);printf("| thgs: ------------------ | |");<BR>gotoxy(1,10);printf("| | | | |");<BR>gotoxy(1,11);printf("| ------------------ | |");<BR>gotoxy(1,12);printf("| cost: ---------- | |");<BR>gotoxy(1,13);printf("| | | | |");<BR>gotoxy(1,14);printf("| ---------- | |");<BR>gotoxy(1,15);printf("| | |");<BR>gotoxy(1,16);printf("| | |");<BR>gotoxy(1,17);printf("| | |");<BR>gotoxy(1,18);printf("| | |");<BR>gotoxy(1,19);printf("| | |");<BR>gotoxy(1,20);printf("| | |");<BR>gotoxy(1,21);printf("| | |");<BR>gotoxy(1,22);printf("| | |");<BR>gotoxy(1,23);printf("|---------------------------------------------------------------------------|");<BR>i=0;<BR>getdate(&amp;d);<BR>sprintf(chtime,"%4d.%02d.%02d",d.da_year,d.da_mon,d.da_day);<BR>for(;;)<BR>{<BR>gotoxy(3,24);printf(" Tab __browse cost list Esc __quit");<BR>gotoxy(13,10);printf(" ");<BR>gotoxy(13,13);printf(" ");<BR>gotoxy(13,7);printf("%s",chtime);<BR>j=18;<BR>ch[0]=getch();<BR>if(ch[0]==27)<BR>break;<BR>strcpy(chshop,"");<BR>strcpy(chmoney,"");<BR>if(ch[0]==9)<BR>{<BR>mm:i=0;<BR>fp=fopen("home.dat","r+");<BR>gotoxy(3,24);printf(" ");<BR>gotoxy(6,4);printf(" list records ");<BR>gotoxy(1,5);printf("|-------------------------------------|");<BR>gotoxy(41,4);printf(" ");<BR>gotoxy(41,5);printf(" |");<BR>while(fscanf(fp,"%10s%14s%f\n",chtime,chshop,&amp;chm)!=EOF)<BR>{ if(i==36)<BR>{ getch();<BR>i=0;}<BR>if ((i%36)&lt;17)<BR>{ gotoxy(4,6+i);<BR>printf(" ");<BR>gotoxy(4,6+i);}<BR>else<BR>if((i%36)&gt;16)<BR>{ gotoxy(41,4+i-17);<BR>printf(" ");<BR>gotoxy(42,4+i-17);}<BR>i++;<BR>sum=sum+chm;<BR>printf("%10s %-14s %6.1f\n",chtime,chshop,chm);}<BR>gotoxy(1,23);printf("|---------------------------------------------------------------------------|");<BR>gotoxy(1,24);printf("| |");<BR>gotoxy(1,25);printf("|---------------------------------------------------------------------------|");<BR>gotoxy(10,24);printf("total is %8.1f$",sum);<BR>fclose(fp);<BR>gotoxy(49,24);printf("press any key to.....");getch();goto pp;<BR>}<BR>else<BR>{<BR>while(ch[0]!='\r')<BR>{ if(j&lt;10)<BR>{ strncat(chtime,ch,1);<BR>j++;}<BR>if(ch[0]==8)<BR>{<BR>len=strlen(chtime)-1;<BR>if(j&gt;15)<BR>{ len=len+1; j=11;}<BR>strcpy(ch1,"");<BR>j=j-2;<BR>strncat(ch1,chtime,len);<BR>strcpy(chtime,"");<BR>strncat(chtime,ch1,len-1);<BR>gotoxy(13,7);printf(" ");}<BR>gotoxy(13,7);printf("%s",chtime);ch[0]=getch();<BR>if(ch[0]==9)<BR>goto mm;<BR>if(ch[0]==27)<BR>exit(1);<BR>}<BR>gotoxy(3,24);printf(" ");<BR>gotoxy(13,10);<BR>j=0;<BR>ch[0]=getch();<BR>while(ch[0]!='\r')<BR>{ if (j&lt;14)<BR>{ strncat(chshop,ch,1);<BR>j++;}<BR>if(ch[0]==8)<BR>{ len=strlen(chshop)-1;<BR>strcpy(ch1,"");<BR>j=j-2;<BR>strncat(ch1,chshop,len);<BR>strcpy(chshop,"");<BR>strncat(chshop,ch1,len-1);<BR>gotoxy(13,10);printf(" ");}<BR>gotoxy(13,10);printf("%s",chshop);ch[0]=getch();}<BR>gotoxy(13,13);<BR>j=0;<BR>ch[0]=getch();<BR>while(ch[0]!='\r')<BR>{ if (j&lt;6)<BR>{ strncat(chmoney,ch,1);<BR>j++;}<BR>if(ch[0]==8)<BR>{ len=strlen(chmoney)-1;<BR>strcpy(ch1,"");<BR>j=j-2;<BR>strncat(ch1,chmoney,len);<BR>strcpy(chmoney,"");<BR>strncat(chmoney,ch1,len-1);<BR>gotoxy(13,13);printf(" ");}<BR>gotoxy(13,13);printf("%s",chmoney);ch[0]=getch();}<BR>if((strlen(chshop)==0)||(strlen(chmoney)==0))<BR>continue;<BR>if((fp=fopen("home.dat","a+"))!=NULL);<BR>fprintf(fp,"%10s%14s%6s",chtime,chshop,chmoney);<BR>fputc('\n',fp);<BR>fclose(fp);<BR>i++;<BR>gotoxy(41,5+i);<BR>printf("%10s %-14s %-6s",chtime,chshop,chmoney);<BR>}}} <BR>==============================================================<BR>【程序96】<BR>题目:计算字符串中子串出现的次数<BR>1.程序分析:<BR>2.程序源代码:<BR>#include "string.h"<BR>#include "stdio.h"<BR>main()<BR>{ char str1[20],str2[20],*p1,*p2;<BR>int sum=0;<BR>printf("please input two strings\n");<BR>scanf("%s%s",str1,str2);<BR>p1=str1;p2=str2;<BR>while(*p1!='\0')<BR>{<BR>if(*p1==*p2)<BR>{while(*p1==*p2&amp;&amp;*p2!='\0')<BR>{p1++;<BR>p2++;}<BR>}<BR>else<BR>p1++;<BR>if(*p2=='\0')<BR>sum++;<BR>p2=str2;<BR>}<BR>printf("%d",sum);<BR>getch();} <BR>==============================================================<BR>【程序97】<BR>题目:从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个#为止。<BR>1.程序分析:     <BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{ FILE *fp;<BR>char ch,filename[10];<BR>scanf("%s",filename);<BR>if((fp=fopen(filename,"w"))==NULL)<BR>{printf("cannot open file\n");<BR>exit(0);}<BR>ch=getchar();<BR>ch=getchar();<BR>while(ch!='#')<BR>{fputc(ch,fp);putchar(ch);<BR>ch=getchar();<BR>}<BR>fclose(fp);<BR>}<BR>==============================================================<BR>【程序98】<BR>题目:从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存。<BR>   输入的字符串以!结束。 <BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{FILE *fp;<BR>char str[100],filename[10];<BR>int i=0;<BR>if((fp=fopen("test","w"))==NULL)<BR>{ printf("cannot open the file\n");<BR>exit(0);}<BR>printf("please input a string:\n");<BR>gets(str);<BR>while(str!='!')<BR>{ if(str&gt;='a'&amp;&amp;str&lt;='z')<BR>str=str-32;<BR>fputc(str,fp);<BR>i++;}<BR>fclose(fp);<BR>fp=fopen("test","r");<BR>fgets(str,strlen(str)+1,fp);<BR>printf("%s\n",str);<BR>fclose(fp);<BR>}<BR>==============================================================<BR>【程序99】<BR>题目:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), <BR>   输出到一个新文件C中。<BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{ FILE *fp;<BR>int i,j,n,ni;<BR>char c[160],t,ch;<BR>if((fp=fopen("A","r"))==NULL)<BR>{printf("file A cannot be opened\n");<BR>exit(0);}<BR>printf("\n A contents are :\n");<BR>for(i=0;(ch=fgetc(fp))!=EOF;i++)<BR>{c=ch;<BR>putchar(c);<BR>}<BR>fclose(fp);<BR>ni=i;<BR>if((fp=fopen("B","r"))==NULL)<BR>{printf("file B cannot be opened\n");<BR>exit(0);}<BR>printf("\n B contents are :\n");<BR>for(i=0;(ch=fgetc(fp))!=EOF;i++)<BR>{c=ch;<BR>putchar(c);<BR>}<BR>fclose(fp);<BR>n=i;<BR>for(i=0;i&lt;n;i++)<BR>for(j=i+1;j&lt;n;j++)<BR>if(c&gt;c[j])<BR>{t=c;c=c[j];c[j]=t;}<BR>printf("\n C file is:\n");<BR>fp=fopen("C","w");<BR>for(i=0;i&lt;n;i++)<BR>{ putc(c,fp);<BR>putchar(c);<BR>}<BR>fclose(fp);<BR>}<BR>==============================================================<BR>【程序100】<BR>题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出<BR>   平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件"stud"中。<BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>struct student<BR>{ char num[6];<BR>char name[8];<BR>int score[3];<BR>float avr;<BR>} stu[5];<BR>main()<BR>{int i,j,sum;<BR>FILE *fp;<BR>/*input*/<BR>for(i=0;i&lt;5;i++)<BR>{ printf("\n please input No. %d score:\n",i);<BR>printf("stuN");<BR>scanf("%s",stu.num);<BR>printf("name:");<BR>scanf("%s",stu.name);<BR>sum=0;<BR>for(j=0;j&lt;3;j++)<BR>{ printf("score %d.",j+1);<BR>scanf("%d",&amp;stu.score[j]);<BR>sum+=stu.score[j];<BR>}<BR>stu.avr=sum/3.0;<BR>}<BR>fp=fopen("stud","w");<BR>for(i=0;i&lt;5;i++)<BR>if(fwrite(&amp;stu,sizeof(struct student),1,fp)!=1)<BR>printf("file write error\n");<BR>fclose(fp);<BR>}<BR>
发表于 2005-11-25 18:50 | 显示全部楼层
<P>LZ辛苦了</P>
<P>虽然每个例子都不是很难,但是把c语言的基本用法都体现出来了</P>
<P>还是很值得初学者一看的</P>
发表于 2006-2-20 16:35 | 显示全部楼层
<P>在那里见过</P>
发表于 2006-2-27 15:59 | 显示全部楼层
<P>嗯  是好像在哪见过的!</P>[em01]
发表于 2006-3-1 18:43 | 显示全部楼层
赞美你
发表于 2006-3-7 15:12 | 显示全部楼层

我来补充几个~

<P>/*本程序能读取系统环境变量,查看有无鼠标.若有,便输出Mouse is OK,<BR>反之便输出No mouse.*/</P>
<P>/********************read mouse***********************/</P>
<P>#include&lt;stdio.h&gt;/*for function of printf()*/<BR>#include&lt;stdlib.h&gt;/*for function of exit()*/<BR>#include&lt;string.h&gt;/*for function of strcmp()*/</P>
<P><BR>void loadmous(void); /*查看有无鼠标子函数*/<BR>/*主函数开始*/<BR>main()<BR>{<BR>    loadmous();<BR>    return 0;<BR>}<BR>/*子函数*/<BR>void loadmous()<BR>{<BR>    char *p;<BR>    if((p=getenv("MOUSE"))!=NULL){/*调用getenv()函数,读取系统环境变量*/<BR>        if(!strcmp(p,"YES"))/*判断有无鼠标*/<BR>        printf("Mouse is OK\n");<BR>    }<BR>    else{<BR> printf("\n No mouse");<BR> getch();<BR> exit(1);/*正常退出程序*/<BR>    }<BR>}</P>
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-13 06:44 , Processed in 0.100298 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表