C语言百道例题答案

上传人:平*** 文档编号:18583951 上传时间:2017-11-15 格式:DOC 页数:39 大小:189.47KB
返回 下载 相关 举报
C语言百道例题答案_第1页
第1页 / 共39页
C语言百道例题答案_第2页
第2页 / 共39页
C语言百道例题答案_第3页
第3页 / 共39页
C语言百道例题答案_第4页
第4页 / 共39页
C语言百道例题答案_第5页
第5页 / 共39页
点击查看更多>>
资源描述

《C语言百道例题答案》由会员分享,可在线阅读,更多相关《C语言百道例题答案(39页珍藏版)》请在金锄头文库上搜索。

1、【程序 9】 题目:要求输出国际象棋棋盘。 1.程序分析:用 i 控制行,j 来控制列,根据 i+j 的和的变化来控制输出黑方格,还是白方格。 2.程序源代码: #include stdio.h main() int i,j; for(i=0;ik,但 n 能被 k 整除,则应打印出 k 的值,并用 n 除以 k 的商,作为新的正整数你 n, 重复执行第一步。 (3)如果 n 不能被 k 整除,则用 k+1 作为 k 的值,重复执行第一步。 2.程序源代码: /* zheng int is divided yinshu*/ main() int n,i; printf(nplease inpu

2、t a number:n); scanf(%d,&n); printf(%d=,n); for(i=2;i void main(void) int color; for (color = 0; color void main(void) clrscr();/*清屏函数 */ textbackground(2); gotoxy(1, 5);/*定位函数 */ cprintf(Output at row 5 column 1n); textbackground(3); gotoxy(20, 10); cprintf(Output at row 10 column 20n); = 【程序 35】 题

3、目:文本颜色设置 1.程序分析: 2.程序源代码: #include void main(void) int color; for (color = 1; color 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(); 2005-1-22 11:31 回复 zhlei81 101 位粉丝 12 楼【

4、程序 61】 题目:打印出杨辉三角形(要求打印出 10 行如下图) 1.程序分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10105 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)

5、int *p1,*p2; int p; p=*p1;*p1=*p2;*p2=p; 【程序 68】 题目:有 n 个整数,使其前面各数顺序向后移 m 个位置,最后 m 个数变成最前面的 m 个数 1.程序分析: 2.程序源代码: main() int number20,n,m,i; printf(the total numbers is:); scanf(%d,&n); printf(back m:); scanf(%d,&m); for(i=0;iarray;p-) *p=*(p-1); *array=array_end; m-; if(m0) move(array,n,m); = 【程序 6

6、9】 题目:有 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,&n); p=num; for(i=0;in); for(i=0;idata=num; ptr-next=(link)malloc(sizeof(node); if(i=4) ptr-next=NULL; els

7、e ptr=ptr-next; ptr=head; while(ptr!=NULL) printf(The value is =%dn,ptr-data); ptr=ptr-next; = 【程序 73】 题目:反向输出一个链表。 1.程序分析: 2.程序源代码: /*reverse 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

8、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; 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.程序分析

9、: 2.程序源代码: #include stdlib.h #include stdio.h struct list int data; struct list *next; ; typedef struct list node; typedef node *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

10、/*delete the other node*/ tmp-next=tmp-next-next; return pointer; void selection_sort(link pointer,int num) link tmp,btmp; int i,min; for(i=0;idata; 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

11、 create_list(int array,int num) link tmp1,tmp2,pointer; int i; pointer=(link)malloc(sizeof(node); pointer-data=array0; tmp1=pointer; for(i=1;inext=NULL; tmp2-data=arrayi; tmp1-next=tmp2; tmp1=tmp1-next; return pointer; link concatenate(link pointer1,link pointer2) link tmp; tmp=pointer1; while(tmp-n

12、ext) tmp=tmp-next; tmp-next=pointer2; return pointer1; void main(void) 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=d

13、call(podd,n); printf(%f,sum); float peven(int n) float s; int i; s=1; for(i=2;iage) q=p+; m=q-age; 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,st

14、r3); if(strcmp(str1,str2)0) swap(str1,str2); if(strcmp(str1,str3)0) 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】 题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 试题/考题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号