C语言经典算法100例.pdf

上传人:tbuqq 文档编号:4765243 上传时间:2019-12-10 格式:PDF 页数:53 大小:418.67KB
返回 下载 相关 举报
C语言经典算法100例.pdf_第1页
第1页 / 共53页
亲,该文档总共53页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《C语言经典算法100例.pdf》由会员分享,可在线阅读,更多相关《C语言经典算法100例.pdf(53页珍藏版)》请在三一文库上搜索。

1、核心出品 必属精品 【程序 1】 题目:有 1、2、3、4 个数字,能组成多少个互不相同且无重复数字的三位数?都是多 少? 1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。 2.程序源代码: main() int i,j,k; printf(“n“); for(i=1;i2)/*如果是闰年且月份大于2,总天数应该加一天*/ sum+; printf(“It is the %dth day.“,sum); = 【程序 5】 题目:输入三个整数x,y,z,请把这三个数由小到大输出。 1.程序分析: 我们想办法把最小的数放到x 上,先将 x 与

2、 y 进行比较, 如果 xy 则将 x 与 y 的值进行交换, 然后再用 x 与 z 进行比较,如果xz 则将 x 与 z 的值进行交换,这样能使x 最小。 2.程序源代码: main() int x,y,z,t; scanf(“%d%d%d“, if (xy) t=x;x=y;y=t; /*交换 x,y 的值 */ if(xz) t=z;z=x;x=t;/*交换 x,z 的值 */ if(yz) t=y;y=z;z=t;/*交换 z,y 的值 */ printf(“small to big: %d %d %dn“,x,y,z); = 【程序 6】 题目:用 *号输出字母C 的图案。 1.程序

3、分析:可先用*号在纸上写出字母C,再分行输出。 2.程序源代码: #include “stdio.h“ main() printf(“Hello C-world!n“); printf(“ *n“); printf(“ *n“); printf(“ * n“); printf(“ *n“); = 【程序 7】 题目:输出特殊图案,请在c 环境中运行,看一看,Very Beautiful! 1.程序分析:字符共有256个。不同字符,图形不一样。 2.程序源代码: #include “stdio.h“ main() char a=176,b=219; printf(“%c%c%c%c%cn“,b,

4、a,a,a,b); printf(“%c%c%c%c%cn“,a,b,a,b,a); printf(“%c%c%c%c%cn“,a,a,b,a,a); printf(“%c%c%c%c%cn“,a,b,a,b,a); printf(“%c%c%c%c%cn“,b,a,a,a,b); = 【程序 8】 题目:输出9*9 口诀。 1.程序分析:分行与列考虑,共9 行 9 列, i 控制行, j 控制列。 2.程序源代码: #include “stdio.h“ main() int i,j,result; printf(“n“); for (i=1;ik,但 n 能被 k 整除,则应打印出k 的值,

5、并用n 除以 k 的商 ,作为新的正整数你n, 重复执行第一步。 (3)如果 n 不能被 k 整除,则用 k+1 作为 k 的值 ,重复执行第一步。 2.程序源代码: /* zheng int is divided yinshu*/ main() int n,i; printf(“nplease input a number:n“); scanf(“%d“, printf(“%d=“,n); for(i=2;i=90 分的同学用A 表示, 60-89 分之间的用B 表示, 60 分以下的用C 表示。 1.程序分析: (ab)?a:b 这是条件运算符的基本例子。 2.程序源代码: main()

6、int score; char grade; printf(“please input a scoren“); scanf(“%d“, grade=score=90?A:(score=60?B:C); printf(“%d belongs to %c“,score,grade); = 【程序 16】 题目:输入两个正整数m 和 n,求其最大公约数和最小公倍数。 作者:zhlei81 2005-1-22 11:30 回复此发言 - 4 回复:经典C 源程序 100 例 1.程序分析:利用辗除法。 2.程序源代码: main() int a,b,num1,num2,temp; printf(“pl

7、ease input two numbers:n“); scanf(“%d,%d“, if(num1 temp=num1; num1=num2; num2=temp; a=num1;b=num2; while(b!=0)/* 利用辗除法,直到b 为 0 为止 */ temp=a%b; a=b; b=temp; printf(“gongyueshu:%dn“,a); printf(“gongbeishu:%dn“,num1*num2/a); = 【程序 17】 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 1.程序分析:利用while 语句 ,条件为输入的字符不为n.

8、 2.程序源代码: #include “stdio.h“ main() char c; int letters=0,space=0,digit=0,others=0; printf(“please input some charactersn“); while(c=getchar()!=n) if(c=a/*第一天的桃子数是第2 天桃子数加1 后的 2 倍*/ x2=x1; day-; printf(“the total is %dn“,x1); = 【程序 22】 题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c 三人,乙队为x,y,z 三人。已抽签决定 比赛名单。有人向队员打听比赛的

9、名单。a 说他不和 x 比, c说他不和 x,z 比,请编程序找出 三队赛手的名单。 1.程序分析:判断素数的方法:用一个数分别去除2 到 sqrt(这个数 ),如果能被整除, 则表明此数不是素数,反之是素数。 2.程序源代码: main() char i,j,k;/*i 是 a 的对手, j 是 b 的对手, k 是 c的对手 */ for(i=x;iaj) min=j; tem=ai; ai=amin; amin=tem; /*output data*/ printf(“After sorted n“); for(i=0;iprintf(“%5d“,ai); = 【程序 38】 题目:求一

10、个3*3 矩阵对角线元素之和 1.程序分析:利用双重for 循环控制输入二维数组,再将aii 累加后输出。 2.程序源代码: main() float a33,sum=0; int i,j; printf(“please input rectangle element:n“); for(i=0;iend) a10=number; else for(i=0;inumber) temp1=ai; ai=number; for(j=i+1;j“); scanf(“%d“, printf(“40:The square for this number is %d n“,SQ(num); if(num=5

11、0) again=TRUE; else again=FALSE; = 【程序 47】 题目:宏 #define 命令练习 (2) 1.程序分析: 2.程序源代码: #include “stdio.h“ #define exchange(a,b) /* 宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上“*/ int t; t=a; a=b; b=t; void main(void) int x=10; int y=20; printf(“x=%d; y=%dn“,x,y); exchange(x,y); printf(“x=%d; y=%dn“,x,y); = 【程序 48】 题目:宏

12、#define 命令练习 (3) 1.程序分析: 2.程序源代码: #define LAG #define SMA y)?x:y #define MINIMUM(x,y) (xy)?y:x void main() int a=10,b=20; #ifdef MAX printf(“40: The larger one is %dn“,MAXIMUM(a,b); #else printf(“40: The lower one is %dn“,MINIMUM(a,b); #endif #ifndef MIN printf(“40: The lower one is %dn“,MINIMUM(a,b

13、); #else printf(“40: The larger one is %dn“,MAXIMUM(a,b); #endif #undef MAX #ifdef MAX printf(“40: The larger one is %dn“,MAXIMUM(a,b); #else printf(“40: The lower one is %dn“,MINIMUM(a,b); #endif #define MIN #ifndef MIN printf(“40: The lower one is %dn“,MINIMUM(a,b); #else printf(“40: The larger on

14、e is %dn“,MAXIMUM(a,b); #endif = 【程序 50】 题目: #include 的应用练习 1.程序分析: 2.程序源代码: test.h 文件如下: #define LAG #define SMA 4; c=(0=RIGHT) dx1=-dx1; if(y1=BOTTOM) dy1=-dy1; if(x2=RIGHT) dx2=-dx2; if(y2=BOTTOM) dy2=-dy2; if(+countLINES) setcolor(color); color=(color=MAXCOLOR)?0:+color; closegraph(); 【程序 61】 题目

15、:打印出杨辉三角形(要求打印出10 行如下图) 1.程序分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 2.程序源代码: main() int i,j; int a1010; printf(“n“); for(i=0;in2) swap(pointer1,pointer2); if(n1n3) swap(pointer1,pointer3); if(n2n3) swap(pointer2,pointer3); printf(“the sorted numbers are:%d,%d,%dn“,n1,n2,n3); swap(p1,p2) int

16、*p1,*p2; int p; p=*p1;*p1=*p2;*p2=p; = 【程序 67】 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。 1.程序分析:谭浩强的书中答案有问题。 2.程序源代码: main() int number10; input(number); max_min(number); output(number); input(number) int number10; int i; for(i=0;i*max) max=p; else if(*parray;p-) *p=*(p-1); *array=array_end; m-; if(m0)

17、 move(array,n,m); = 【程序 69】 题目:有 n 个人围成一圈,顺序排号。从第一个人开始报数(从1 到 3 报数) ,凡报到 3 的人退出 圈子,问最后留下的是原来第几号的那位。 1. 程序分析: 2.程序源代码: #define nmax 50 main() int i,k,m,n,numnmax,*p; printf(“please input the total of numbers:“); scanf(“%d“, p=num; for(i=0;i *(p+i)=i+1; i=0; k=0; m=0; while(m if(*(p+i)!=0) k+; if(k=3)

18、 *(p+i)=0; k=0; m+; i+; if(i=n) i=0; while(*p=0) p+; printf(“%d is leftn“,*p); = 【程序 70】 题目:写一个函数,求一个字符串的长度,在main 函数中输入字符串,并输出其长度。 1.程序分析: 2.程序源代码: main() int len; char *str20; printf(“please input a string:n“); scanf(“%s“,str); len=length(str); printf(“the string has %d characters.“,len); length(p)

19、 char *p; int n; n=0; while(*p!=0) n+; p+; return n; 【程序 71】 题目:编写 input() 和 output() 函数输入,输出5 个学生的数据记录。 1.程序分析: 2.程序源代码: #define N 5 struct student char num6; char name8; int score4; stuN; input(stu) struct student stu; int i,j; for(i=0;i printf(“n please input %d of %dn“,i+1,N); printf(“num: “); s

20、canf(“%s“,stui.num); printf(“name: “); scanf(“%s“,stui.name); for(j=0;jn“); for(i=0;idata=num; ptr-next=(link)malloc(sizeof(node); if(i=4) ptr-next=NULL; else ptr=ptr-next; ptr=head; while(ptr!=NULL) printf(“The value is =%dn“,ptr-data); ptr=ptr-next; = 【程序 73】 题目:反向输出一个链表。 1.程序分析: 2.程序源代码: /*revers

21、e output a list*/ #include “stdlib.h“ #include “stdio.h“ struct list int data; struct list *next; ; typedef struct list node; typedef node *link; void main() link ptr,head,tail; int num,i; tail=(link)malloc(sizeof(node); tail-next=NULL; ptr=tail; printf(“nplease input 5 data=n“); for(i=0;idata=num;

22、head=(link)malloc(sizeof(node); head-next=ptr; ptr=head; ptr=ptr-next; while(ptr!=NULL) printf(“The value is =%dn“,ptr-data); ptr=ptr-next; = 【程序 74】 题目:连接两个链表。 1.程序分析: 2.程序源代码: #include “stdlib.h“ #include “stdio.h“ struct list int data; struct list *next; ; typedef struct list node; typedef node *

23、link; link delete_node(link pointer,link tmp) if (tmp=NULL) /*delete first node*/ return pointer-next; else if(tmp-next-next=NULL)/*delete last node*/ tmp-next=NULL; else /*delete the other node*/ tmp-next=tmp-next-next; return pointer; void selection_sort(link pointer,int num) link tmp,btmp; int i,

24、min; for(i=0;i tmp=pointer; min=tmp-data; btmp=NULL; while(tmp-next) if(mintmp-next-data) min=tmp-next-data; btmp=tmp; tmp=tmp-next; printf(“40: %dn“,min); pointer=delete_node(pointer,btmp); link create_list(int array,int num) link tmp1,tmp2,pointer; int i; pointer=(link)malloc(sizeof(node); pointer

25、-data=array0; tmp1=pointer; for(i=1;i tmp2=(link)malloc(sizeof(node); tmp2-next=NULL; tmp2-data=arrayi; tmp1-next=tmp2; tmp1=tmp1-next; return pointer; link concatenate(link pointer1,link pointer2) link tmp; tmp=pointer1; while(tmp-next) tmp=tmp-next; tmp-next=pointer2; return pointer1; void main(vo

26、id) int arr1=3,12,8,9,11; link ptr; ptr=create_list(arr1,5); selection_sort(ptr,5); = 【程序 75】 题目:放松一下,算一道简单的题目。 1.程序分析: 2.程序源代码: main() int i,n; for(i=1;i1) break; if(n%2=0) printf(“Even=“); sum=dcall(peven,n); else printf(“Odd=“); sum=dcall(podd,n); printf(“%f“,sum); float peven(int n) float s; int

27、 i; s=1; for(i=2;iage; printf(“%s,%d“,(*q).name,(*q).age); = 【程序 79】 题目:字符串排序。 1.程序分析: 2.程序源代码: main() char *str120,*str220,*str320; char swap(); printf(“please input three stringsn“); scanf(“%s“,str1); scanf(“%s“,str2); scanf(“%s“,str3); if(strcmp(str1,str2)0) swap(str1,str2); if(strcmp(str1,str3)0

28、) swap(str1,str3); if(strcmp(str2,str3)0) swap(str2,str3); printf(“after being sortedn“); printf(“%sn%sn%sn“,str1,str2,str3); char swap(p1,p2) char *p1,*p2; char *p20; strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p); = 【程序 80】 题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只 猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,

29、又多了 一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的, 问海滩上原来最少有多少个桃子? 1.程序分析: 2.程序源代码: main() int i,m,j,k,count; for(i=4;i=1000 = 【程序 82】 题目:八进制转换为十进制 1.程序分析: 2.程序源代码: main() char *p,s6;int n; p=s; gets(p); n=0; while(*(p)!=0) n=n*8+*p-0; p+; printf(“%d“,n); = 【程序 83】 题目:求 07 所能组成的奇数个数。 1.程序分析: 2.程序源代码: main

30、() long sum=4,s=4; int j; for(j=2;jsqrt(b) d=a-b; else break; for(c=2;csqrt(d) printf(“%d=%d+%dn“,a,b,d); = 【程序 85】 题目:判断一个素数能被几个9 整除 1.程序分析: 2.程序源代码: main() long int m9=9,sum=9; int zi,n1=1,c9=1; scanf(“%d“, while(n1!=0) if(!(sum%zi) n1=0; else m9=m9*10; sum=sum+m9; c9+; printf(“%ld,can be divided

31、by %d “9“,sum,c9); = 【程序 86】 题目:两个字符串连接程序 1.程序分析: 2.程序源代码: #include “stdio.h“ main() char a=“acegikm“; char b=“bdfhjlnpq“; char c80,*p; int i=0,j=0,k=0; while(ai!=0i+; else ck=bj+; k+; ck=0; if(ai=0) p=b+j; else p=a+i; strcat(c,p); puts(c); = 【程序 87】 题目:回答结果(结构体变量传递) 1.程序分析: 2.程序源代码: #include “stdio

32、.h“ struct student int x; char c; a; main() a.x=3; a.c=a; f(a); printf(“%d,%c“,a.x,a.c); f(struct student b) b.x=20; b.c=y; = 【程序 88】 题目:读取 7 个数( 150)的整数值,每读取一个值,程序打印出该值个数的。 1.程序分析: 2.程序源代码: main() int i,a,n=1; while(n50); for(i=1;i=0;i-) printf(“%d“,aai); = 【程序 90】 题目:专升本一题,读结果。 1.程序分析: 2.程序源代码: #i

33、nclude “stdio.h“ #define M 5 main() int aM=1,2,3,4,5; int i,j,t; i=0;j=M-1; while(i t=*(a+i); *(a+i)=*(a+j); *(a+j)=t; i+;j-; for(i=0;i printf(“%d“,*(a+i); 【程序 91】 题目:时间函数举例1 1.程序分析: 2.程序源代码: #include “stdio.h“ #include “time.h“ void main() time_t lt; /*define a longint time varible*/ lt=time(NULL);

34、/*system time and date*/ printf(ctime(i) printf(“please input a little smaller.n“); scanf(“%d“, else printf(“please input a little bigger.n“); scanf(“%d“, end=clock(); b=time(NULL); printf(“1: It took you %6.3f secondsn“,var=(double)(end-start)/18.2); printf(“1: it took you %6.3f secondsnn“,difftime

35、(b,a); if(var15) printf(“11 You are very clever! 11nn“); else if(var25) printf(“11 you are normal! 11nn“); else printf(“11 you are stupid! 11nn“); printf(“11 Congradulations 11nn“); printf(“The number you guess is %d“,i); printf(“ndo you want to try it again?(“yy“.or.“n“)n“); if(c=getch()=y) goto loop;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1