C语言经典编程编写100例.doc
文本预览下载声明
C语言经典编程100例
程序1】3个月起每个月都生一对兔子,小兔子长到第三个月1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....2.程序源代码:main(){long f1,f2;int i;f1=f2=1;for(i=1;i=20;i++){ printf(%12ld %12ld,f1,f2); if(i%2==0) printf(\n);/*控制输出,每行四个*/f1=f1+f2; /*前两个月加起来赋值给第三个月*/f2=f1+f2; /*前两个月加起来赋值给第三个月*/}}==============================================================【程序2】101-200之间有多少个素数,并输出所有素数。1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,2.程序源代码:#include math.hmain(){int m,i,k,h=0,leap=1; printf(\n); for(m=101;m=200;m++) { k=sqrt(m+1); for(i=2;i=k;i++) if(m%i==0) {leap=0;break;} if(leap) {printf(%-4d,m);h++; if(h%10==0) printf(\n); } leap=1; } printf(\nThe total is %d,h);}【程序4】90,打印出90=2*3*3*5。
【程序81】809*??=800*??+9*??+1 其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。1.程序分析:2.程序源代码:output(long b,long i){ printf(\n%ld/%ld=809*%ld+%ld,b,i,i,b%i);}main(){long int a,b,i;a=809;for(i=10;i100;i++){b=i*a+1;if(b=1000b=100008*i1009*i=100)output(b,i); }}==============================================================【程序82】1.程序分析: 2.程序源代码:main(){ char *p,s[6];int n;p=s;gets(p);n=0;while(*(p)!=\0){n=n*8+*p-0;p++;}printf(%d,n);}==============================================================83】0—7所能组成的奇数个数。1.程序分析:2.程序源代码:main(){long sum=4,s=4;int j;for(j=2;j=8;j++)/*j is place of number*/{ printf(\n%ld,sum);if(j=2)s*=7;elses*=8;sum+=s;}printf(\nsum=%ld,sum);}==============================================================84】1.程序分析:2.程序源代码:#include stdio.h#include math.hmain(){ int a,b,c,d;scanf(%d,a);for(b=3;b=a/2;b+=2){ for(c=2;c=sqrt(b);c++)if(b%c==0) break;if(csqrt(b))d=a-b;elsebreak;for(c=2;c=sqrt(d);c++)if(d%c==0) break;if(csqrt(d))printf(%d=%d+%d\n,a,b,d);}}==============================================================【程序85】9整除1.程序分析:2.程序源代码:main(){ long int m9=9,sum=9;int zi,n1=1,c9=1;scanf(%d,zi);while(n1!=0){ if(!(sum%zi))n1=0;else{m9=m9*10
显示全部