c语言经典案例.doc
【精品文档】如有侵权,请联系网站删除,仅供学习与交流c语言经典案例.精品文档.1、用筛选法求100之内的素数。#include <stdio.h>void main() int num100,i,j; for (i=0;i<100;i+) numi=1; for (i=2;i<=10;i+) for (j=2;i*j<=100;j+) numi*j-1=0; printf("0至100内素数有:n"); for (i=j=0;i<100;i+) if (numi=1) printf("%-4d",i+1);if (+j%4=0) printf("n");2、用选择法对10个整数排序。#include <stdio.h>void main() int num10,n,i,j,t,k; printf("请输入十个整数:"); for (n=0;n<10;n+) scanf("%d",&numn); for (i=0;i<9;i+) k=i; for (j=i+1;j<10;j+) if (numk<numj) k=j; if (k!=i) t=numi;numi=numk;numk=t; printf("从大到小排序为:"); for (n=0;n<10;n+) printf("%d ",numn);3、求一个3*3的整型二维数组对角线元素之和。#include <stdio.h>void main() int num33,i,j; printf("输入二维数组:n"); for (i=0;i<3;i+) for (j=0;j<3;j+) scanf("%d",&numij); printf("两对角线和分别为%d和%dn",num00+num11+num22,num02+num11+num20);5、将一个数组中的值按逆序重新存放。#include <stdio.h>#include <stdlib.h>#include <time.h>void main() int num100,n,i,temp; srand(time(0); n=rand()%100+1; printf("生成数组长度为%dn生成的数组:n",n); for (i=0;i<n;i+) numi=rand()%100;printf("%-3d",numi); printf("n数组逆序存放后排列为:n"); for (i=0;i<n/2;i+) temp=numi;numi=numn-1-i;numn-1-i=temp; for (i=0;i<n;i+) printf("%-3d",numi); printf("n");6、输出以下的杨辉三角形(要求输出10行)。11 11 2 11 3 3 1#include <stdio.h>void main() int num1010,i,j; for (i=0;i<10;i+) for (j=0;j<10;j+) if (j-1<0|j=i) numij=1; else numij=numi-1j-1+numi-1j; printf("%-4d",numij); if (j=i) break; printf("n");7、输出“魔方阵”。所谓魔方阵是指这样的方阵,它的每一行、每一列和对角线之和均相等。例如,三阶魔方阵为8 1 6 3 5 7 4 9 2#include <stdio.h>void main() int n,i,j,k; printf("输入奇数阶魔方阵的阶数:"); scanf("%d",&n); k=(5*n-1)/2; for(i=0;i<n;+i) for (j=0;j<n;j+) printf("%-4d",(k-i+j)%n*n+(k-i-j-1)%n+1); printf("n");9、有15个数按由大到小顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则输出“无此数”。#include <stdio.h>#include <stdlib.h>#include <time.h>void main() int num15,n,t=0,left,right,middle,key; srand(time(0); printf("数组为n"); for (n=14;n>=0;n-) t=numn=rand()%10+(t+1); for (n=0;n<15;n+) printf("%-4d",numn); printf("n请输入要查找的数:"); scanf("%d",&key); left=0; right=14; while (left<=right) middle=(left+right)/2; if (key=nummiddle) t=-1;break; if (key<nummiddle) left=middle+1; else right=middle-1; if (t=-1) printf("该数是数组中第%d个元素的值n",middle+1); else printf("无此数n");11、输出以下图案:#include <stdio.h>void main() char asterisk="* * * * *",space=" " int a,b; for (a=0;a<5;a+) for (b=0;b<a;b+) printf("%s",space); printf("%sn",asterisk);12、有一行电文,已按下面规律译成密码:第1个字母变成第26个字母,第i个字母变成第(26-i+1)个字母。非字母字符不变。要求编程序将密码译回原文,并输出密码和原文。#include <stdio.h>#include <ctype.h>void main() char ch,code100; int n=0; printf("请输入密码:"); while(ch=getchar()!='n') if (n=0) printf("密码n"); printf("%c",ch); if (isupper(ch) coden+=(26-ch+65+64); if (islower(ch) coden+=(26-ch+97+96); if (!isalpha(ch) coden+=ch; coden='0' printf("n原文n%sn",code);13、编一个程序,将两个字符串连接超来,不要用strcat函数。#include <stdio.h>#include <string.h>void main() char a2100; puts("输入字符串:"); gets(a0); gets(a1); int i=0; while(a0i!='0') i+; strcpy(&a0i,a1); puts("连接后字符串:"); puts(a0);14、编一个程序,将两个字符串s1和s2比较,若s1>s2,输出一个正数;若s1=s2,输出0;若s1<s2,输出一个负数。不要用strcpy函数。两个字符串用gets函数读入。输出的正数或负数的绝对值应是相比较的两个字符串相应字符的ASCII码的差值。#include <stdio.h>void main() char a2100; puts("输入字符串:"); gets(a0); gets(a1); for (int i=0,n=0;a0i!='0'&&a1i!='0'i+) if (a0i!=a1i) n=a0i-a1i;break; printf("%dn",n);15、编写一个程序,将字符数组s2中的全部字符复制到字符数组s1中。不用strcpy函数。复制时,0后面的字符不复制。#include <stdio.h>#include <string.h>void main() char a2100; puts("输入字符串:"); gets(a0); gets(a1); a00='0' strcat(a0,a1); puts("复制后字符串:"); puts(a0);从1累加到100 #include <stdio.h>main() int i,sum=0; for(i=1;i<=100;i+) sum=sum+i; printf("sum=%dn",sum);求1!+2!+n!:#include<stdio.h>Void main() double fac, sum=0;Int i, n;fac=1;i=1;scanf(“%d”,&n);while(i<=n)fac=fac*I;i+;sum=sum+fac;printf(“%lf”,sum);求1-3+5-7+。+101的值#include<stdio.h>#include<math.h>Void main()int a,I,j,sum=0;i=1;j=2;while(1<=101)sum=sum+i*power(-1,j);i=i+2;j+;printf(“%dn”,sum);求1*2*3*100#include <stdio.h>main()int i ,sum;i=1, sum=1;while(i<101)i+;sum=sum*i;printf("sum=%dn",sum);水仙花数:#include <stdio.h>main() int a,b,c,num; for(a=1;a<10;a+) for(b=0;b<10;b+) for(c=0;c<10;c+) if(a*100+b*10+c=(num=a*a*a+b*b*b+c*c*c) printf("%d",num); 从1到1000的素数:#include "stdio.h"void main()int f(int n);int i,k=0;for(i=1;i<=1000;i+)if(!f(i)printf("%5d",i);k+;if(k%15=0)printf("n");getch();int f(int n)int j;for(j=2;j<=n-1;j+)if(n%j=0)break;if(j>=n)return 0;elsereturn 1;最大公约数和最小公倍数:#include<stdio.h>Void main() int m,n,I,j;Scanf(“m=%d,n=%d”,&m,&n); For(i=m;i>=1;i-)If(m%i=0&&n%i=0)Break;for(j=m;j<=m*n;j+)if(j%m=0&&j%n=0)break;printf(“最大公约数%dn,最小公倍数%dn”,i,j);一球从100米得高度落下,每次反弹至原高度的一半;再落下,求它第十次落地时,公经过多少米?第十次反弹多高?#include<stdio.h>main()int n;Float s=100,h=s/2;For(n=2;n<=10;n+)S=s+2*h;H=h/2;Printf(“s=%f,h=%f”,s,h)