【打基础】高级语言程序设计·厦大出版社 课后习题个人记录3
2012-09-24 17:05
471 查看
【前记】
大一时,贪玩
,没有认真地跟着忠孝教授好好学习C语言。后来虽然又花了些时间查阅,但总归没有花一段连续的时间来研读这本教材《高级语言程序设计》(厦门大学出版社·黄翠兰主编),趁着实习前的这段空闲时间,重新拿起学习,并认真做好课后的习题,做到每题都弄懂,打好基础!有闲暇了,数据结构和C++也每题必做!发到自己的博客上监督自己完成!
【第四章】
大一时,贪玩
,没有认真地跟着忠孝教授好好学习C语言。后来虽然又花了些时间查阅,但总归没有花一段连续的时间来研读这本教材《高级语言程序设计》(厦门大学出版社·黄翠兰主编),趁着实习前的这段空闲时间,重新拿起学习,并认真做好课后的习题,做到每题都弄懂,打好基础!有闲暇了,数据结构和C++也每题必做!发到自己的博客上监督自己完成!
【第四章】
1.有一个3*4的矩阵,要求编写程序求出其中值最大的那个元素,以及其所在的行号和列号。 #include <stdio.h> void main() { int a[3][4] = {{9,2,5,15},{17,3,5,8},{55,2,9,19}}; int i,j,max,maxi,maxj; max = 0; for (i=0;i<3;i++) { for (j=0;j<4;j++) { if (a[i][j] > max) { max = a[i][j]; maxi = i; maxj = j; } } } printf("最大的数a[%d][%d]:%d",maxi,maxj,max); getch(); } 2.现有一个已排好序的数组,要求输入一个数后,按原来排序的规律将它插入数组中。 #include <stdio.h> void main() { int a[10] = {1,2,3,5,6,7,8,9,10}; int input,flag; int i,j; scanf("%d",&input); if (a[0]>a[1]) { flag = 1; //逆序 } else { flag = 0;//正序 } for (i=0;i<10;i++) { if (flag==0&&input<a[i]) { for (j = 9;j>=i;j--) { a[j]=a[j-1]; } a[i]=input; return; } if (flag==1&&input>a[i]) { for (j = 9;j>=i;j--) { a[j]=a[j-1]; } a[i]=input; return; } } getch(); } 4.15个数按从大到小顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不存在数组中,则输出“无此数”。 #include <stdio.h> int search1(int a[],int x, int low,int high); int search2(int a[],int x, int low,int high); void main() { int a[16]={0,1,2,3,4,6,5,7,8,9,10,11,12,13,14,15}; int t; t=search2(a,5,1,15); if (t==-1) printf("没找到!\n"); else printf("第%d个元素!\n",t); getch(); } int search1(int a[],int x, int low,int high) { int i=low+(high-low)/2; if(low>high) return -1;/* 没找到,返回-1 */ if(a[i]==x) return i; else if(a[i]>x) return search1(a,x,i+1,high); else return search1(a,x,low,i-1); } int search2(int a[],int x,int low,int high) { int mid; while (low<=high) { mid = (low+high)/2; if (x==a[mid]) return mid; if (x>a[mid]) low = mid+1; else high = mid-1; } return -1;/* 没找到,返回-1 */ } 5.有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的哪位。 #include <stdio.h> int fun(int n) { int i,k,m,num[50],*p; p=num; for (i=0;i<n;i++) *(p+i)=i+1; // 以1至n为序给每个人编号 i=0; // i为每次循环时计数变量 k=0; // k为按1,2,3报数时的计数变量 m=0; // m为退出人数 while (m<n-1) // 当退出人数比n-1少时(即未退出人数大于1时)执行循环体 { if (*(p+i)!=0) k++; if (k==3) // 将退出的人的编号置为0 {*(p+i)=0; k=0; m++; } i++; if (i==n) i=0; // 报数到尾后,i恢复为0 } while(*p==0) p++; printf("The last one is NO:%d\n",*p); return 0; } int main() { int n; linklist *l; scanf("%d",&n); fun(n); getch(); } 7.输出以下的杨辉三角形(要求输出10行)。 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 #include <stdio.h> #define M 10 void main() { int a[M][M],i,j; for (i=0;i<M;i++) for (j=0;j<=i;j++) { if (i==j||j==0) { a[i][j]=1; } else a[i][j]=a[i-1][j]+a[i-1][j-1]; printf("%5d",a[i][j]); if (i==j) printf("\n"); } getchar(); } 8.有一篇文章,共有3行文字,每行有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其字符的个数。 #include <stdio.h> int main() { int i,j,upp,low,dig,spa,oth; char text[3][80]; upp=low=dig=spa=oth=0; for (i=0;i<3;i++) { printf("please input line %d:\n",i+1); gets(text[i]); for (j=0;j<80 && text[i][j]!='\0';j++) { if (text[i][j]>='A'&& text[i][j]<='Z') upp++; else if (text[i][j]>='a' && text[i][j]<='z') low++; else if (text[i][j]>='0' && text[i][j]<='9') dig++; else if (text[i][j]==' ') spa++; else oth++; } } printf("\nupper case: %d\n",upp); printf("lower case: %d\n",low); printf("digit : %d\n",dig); printf("space : %d\n",spa); printf("other : %d\n",oth); return 0; } 9.实现strcmp。 #include <stdio.h> int mystrcmp(char *str1,char *str2) { while(*str1 && *str2) { if (*str1 > *str2) return 1; else if (*str1 < *str2) return -1; str1++, str2++; } if (*str1) return 1; if (*str2) return -1; return 0; } void main() { int i,j; char *str1 = "chinacyr"; char *str2 = ""; j = mystrcmp(str1,str2); printf("%d\n",j); getch(); } 10.实现strcpy。 11.实现strcat。 #include <stdio.h> #include <assert.h> char *mystrcpy(char *strDestination, const char *strSource) { char *strD = strDestination; assert(strDestination!=NULL && strSource!=NULL); while (1) { char temp; temp = *strSource; *strDestination = temp; strDestination++; strSource++; if (temp == '\0') break; } return strD; } void mystrcat(char* str1,char* str2) { while(*str1!='\0') str1++; while(*str2!='\0') { *str1=*str2; str1++; str2++; } *str1='\0'; } void mystrcat2(char* str1,char* str2) { int i=0,j=0; while(str1[i]!='\0')i++; while(str2[j]!='\0') { str1[i] = str2[j]; i++; j++; } str1[i]='\0'; } void main() { char *p1; char *str1 = "ah"; char *str2 = "chinacyr"; char source1[50]="ah"; char source2[20]="chinacyr"; p1 = (char*)malloc(20); //mystrcpy(str1,str2); //mystrcat(str1,str2); //mystrcat2(str1,str2); //mystrcat2(source1,source2); //mystrcpy(source1,source2); mystrcpy(p1,str1); //mystrcat(source1,source2); puts(source1); system("pause"); }
相关文章推荐
- 【打基础】高级语言程序设计·厦大出版社 课后习题个人记录2
- 【打基础】高级语言程序设计·厦大出版社 课后习题个人记录4
- 【打基础】高级语言程序设计·厦大出版社 课后习题个人记录1
- 个人学习C程序设计语言(第2版·新版)笔记
- C程序设计语言(第2版·新版)- 练习2-6
- java语言程序设计第十版(Introduce to java) 课后习题 chapter6-22
- java语言程序设计第十版(Introduce to java 10th) 课后习题 chapter12-11
- java语言程序设计第十版(Introduce to java 10th) 课后习题 chapter6-30
- JAVA语言程序设计-基础篇课后习题2.1--2.17
- java语言程序设计第十版(Introduce to java 10th) 课后习题 chapter7-31
- JAVA语言程序设计基础课后习题第四章
- C++程序设计语言课后习题10章15题
- java语言程序设计第十版(Introduce to java) 课后习题 chapter6-25
- 新版汇编语言程序设计【课后习题答案】
- JAVA语言程序设计基础课后习题第五章
- JAVA语言程序设计基础课后习题第六章
- java 语言程序设计-李尊朝 第9章 异常处理 课后习题
- C程序设计语言(第2版·新版)- 练习2-7
- java语言程序设计第十版(Introduce to java) 课后习题 chapter6-6
- java语言程序设计第十版(Introduce to java 10th) 课后习题 chapter6-31