【前記】
大一時,貪玩,沒有認真地跟着忠孝教授好好學習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");
}