第7章 數(shù)組
1:??(1)??D
?????(2)??A
?????(3)??D
?????(4)??B
?????(5)??D
?
2:??(1)?1?????5????4????4
?????????????????6????7????8
??????????????????????2????3??
???????????????????????????4
?
?????(2)?6333
?
3:???(1)a[8]=data;???k>=0????a[k]=temp????break????k<=8
??
??????(2)?"%s"????str[i]<='Z'????c[str[i]+26-'a']++?????i+'a'
?
4、/*題目:求一組成績的平均分數(shù)以及高于平均分的成績。*/
#include<stdio.h>
#define?max?100
void?main()
{
float?a[max],s=0,average=0;
int?i,n;
printf("Please?input?N:???");???/*輸入要處理元素的個數(shù)*/
????????scanf("%d",&n);
printf("\nPlease?input?%d?ge?shu:???",n);??/*輸入90,85,92,77,80,62*/
for(i=0;i<n;i++)
{
?????scanf("%f",&a[i]);
?????s+=a[i];?
}
average?=?s?/?n;
printf("\naverage?=?%.2f\n",average);??/*輸出81.00*/
for(i=0;i<n;i++)
?????if(a[i]>average)
???printf("%.2f??",a[i]);??/*輸出90.00?,85.00?,92.00*/
}
5、/*題目:編寫程序,輸入一組整數(shù),將他們排序后由小到大輸出。*/
#include<stdio.h>
#define?max?100
void?main()
{
int?a[max],j,i,k=0,t=0,n=0;
printf("Please?input?N:???");???/*輸入要處理元素的個數(shù)*/
????????scanf("%d",&n);
for(i=0;i<n;i++)
?????scanf("%d",&a[i]);
for(i=0;i<n-1;i++)???/*比較n-1遍*/
{?k=i;
for(j=i+1;j<n;j++)???/*每遍比較n-(i+1)次*/
???if(a[k]>a[j])
k=j;
????????????????if(k!=i)???/*元素排序前下標與排序后下標不符,則交換其值*/
{??t=a[i];
???a[i]=a[k];
???a[k]=t;
}
}
printf("\n\n");
for(i=0;i<n;i++)
???printf("%d??",a[i]);
}
6、/*題目:從鍵盤輸入一個4x4的整數(shù)矩陣,以主對角線(\)為對稱軸*/
/*將左下角元素中較大者替換右上角元素,并將右上角含對稱軸輸出。*/
#include<stdio.h>
#include<string.h>
void?main()
{
int?d[4][4]={{0,0,0,0}},i,j;
clrscr();
printf("\nPlease?input?16?num:???");
for(i=0;i<4;i++)
???for(j=0;j<4;j++)
??????scanf("%d",&d[i][j]);
for(i=0;i<4;i++)
???for(j=0;j<4;j++)
??????if(d[i][j]>d[j][i])??/*左下角元素大于右上角元素則交換*/
?d[j][i]=d[i][j];
for(i=0;i<4;i++)
{?printf("\n\n\n\n\n");/*為了保持每行的間隔與每列的相同*/
for(j=0;j<4;j++)
???if(j>=i)
??????printf("\t%d",d[i][j]);/*為了使位數(shù)不同的數(shù)輸出依然保持三角型*/
???else
??????printf("\t");
}
?
}
7、*題目:輸入一個3x4的二維數(shù)組,然后分別按行和按列輸出。*/
#include<stdio.h>
#include<string.h>
void?main()
{
int?d[3][4]={{0,0,0,0}},i,j;
printf("Please?input?12?num:???");
for(i=0;i<3;i++)
???for(j=0;j<4;j++)
??????scanf("%d",&d[i][j]);
printf("\n\nAn?hang:?");
for(i=0;i<3;i++)
???for(j=0;j<4;j++)
???printf("%d??",d[i][j]);
printf("\n\nAn???lie:?");
for(i=0;i<4;i++)
???for(j=0;j<3;j++)
???printf("%d??",d[j][i]);
}
8、/*題目:編寫程序,將兩個字符串連接起來,不用strcat函數(shù)。*/
#include<stdio.h>
#include<string.h>
void?main()
{??char?s1[50],s2[50];
?int?i=0,j=0,t=0;
?printf("Input?one:??");
?gets(s1);
?while(s1[i]!='\0')???i++;???/*統(tǒng)計s1中字符個數(shù)*/
?printf("\n\nInput?second:???");
?gets(s2);??
?while(s2[j]!='\0')???j++;???/*統(tǒng)計s2中字符個數(shù)*/
?for(t=0;t<=j;t++)???/*把s2中字符連接到s1后面*/
?{??s1[i]=s2[t];??i++;}
?printf("\n\none?+?second:??");
?puts(s1);
}
9、/*輸入一行字符串,統(tǒng)計字符對ab的個數(shù)。*/
#include<stdio.h>
#include<string.h>
#define?MAX?100
void?main()
{
char?str[MAX];
int?i=0,flage=0;
int?count=0;
printf("\nPlease?input?a?string:???");
gets(str);
while(str[i])
{?if(str[i]=='a')
?????flage=1;
else?if(str[i]=='b')
{
???if(flage==1)
???{?count++;flage=0;}
}
else?flage=0;
i++;
}
?????printf("the?double?char?\"ab\"?count?is?%d\n",count);
}
10、
#include<stdio.h>??/*相對于第一種方法,這種方法能把同為最大的字符串全部打印出來。*/
#include<string.h>
int?paixu(int?a[],int?n)???/*構(gòu)造對長度為n的數(shù)組排序的函數(shù)*/
{
int?i,j,t,k,temp;
????????for(i=0;i<n-1;i++)
{?k=i;
for(j=i+1;j<n;j++)
???if(a[k]>a[j])
k=j;
????????????????if(k!=i)
{??t=a[i];
???a[i]=a[k];
???a[k]=t;
}
}
temp=a[n-1];
return?temp;???/*返回最大字符串的長度的值*/
}
void?main()
{
char?str1[50],str2[50],str3[50],str4[50],str5[50],str6[50];
char?str7[50],str8[50],str9[50],str10[50];
int?count[10]={0},temp[10]={0},i,j;
int?paixu(int?a[],int?n);
for(i=1;i<=10;i++)???/*輸入10個字符串*/
{
????printf("\nPlease?input?%d?string:???",i);
????if(i==1)
????{???gets(str1);while(str1[count[0]]){count[0]++;temp[0]++;?}}
????if(i==2)
????{???gets(str2);while(str2[count[1]]){count[1]++;temp[1]++;}}
????if(i==3)
????{???gets(str3);while(str3[count[2]]){count[2]++;temp[2]++;?}}
????if(i==4)
????{???gets(str4);while(str4[count[3]]){count[3]++;?temp[3]++;}}
????if(i==5)
????{???gets(str5);while(str5[count[4]]){count[4]++;?temp[4]++;}}
????if(i==6)
????{???gets(str6);while(str6[count[5]]){count[5]++;?temp[5]++;}}
????if(i==7)
????{???gets(str7);while(str7[count[6]]){count[6]++;?temp[6]++;}}
????if(i==8)
????{???gets(str8);while(str8[count[7]]){count[7]++;?temp[7]++;}}
????if(i==9)
????{???gets(str9);while(str9[count[8]]){count[8]++;?temp[8]++;}}
????if(i==10)
????{???gets(str10);while(str10[count[9]]){count[9]++;temp[9]++;}}
}
j=paixu(temp,10);??
for(i=1;i<=10;i++)
{
???if(count[i-1]==j)
???{????????????????????????/*只要字符串長度與最大長度相等就打印出來*/
switch(i)
{?case?1:puts(str1);?printf("\n");?break;
case?2:puts(str2);?printf("\n");?break;
case?3:puts(str3);?printf("\n");?break;
case?4:puts(str4);?printf("\n");?break;
case?5:puts(str5);?printf("\n");?break;
case?6:puts(str6);?printf("\n");?break;
case?7:puts(str7);?printf("\n");?break;
case?8:puts(str8);?printf("\n");?break;
case?9:puts(str9);?printf("\n");?break;
case?10:puts(str10);?printf("\n");?break;
?}
???}
}
}
11、
/*給數(shù)組a輸入m個按升序排列的數(shù),給數(shù)組b輸入n個按降序排列的數(shù)*/
/*將a與b中的元素按降序排列存在數(shù)組c中。*/
#include<stdio.h>
void?main()
{
????int?a[100],b[100],c[100],i,m,n,j,k,temp=0,s;
????printf("\nm?=??");scanf("%d",&m);???????????/*確定M的個數(shù)*/
????printf("\nn?=??");scanf("%d",&n);????????????/*確定N的個數(shù)*/
????printf("\nPlease?input?m?ge?shu:?????");
????for(s=0;s<m;s++)
????????scanf("%d",&a[s]);
????printf("\nPlease?input?n?ge?shu:?????");
????for(s=0;s<n;s++)
????????scanf("%d",&b[s]);
????i=m-1;j=0;k=0;???????/*i,j,分別標記a[]與b[]且都是從最大元素開始做標記*/
????for(;;)
????{???if(i==0)???????????????/*a[]中只有一個數(shù)或者a[]處理到了最后一個元素*/
????????{???if(a[i]<=b[j])?????/*如果a[]的最后一個元素<=b[]中當前元素*/??????????????
????????????{??c[k]=b[j];??k++;?j++;???}
????????????else
????????????{??c[k]=a[i];
???????????????k++;
???????????????for(;;)?????????/*把b[]數(shù)組接到c[]后面*/
???????????????{????c[k]=b[j];
?????????????j++;
?????????????k++;
?????????????????if(j==n)??/*如果b[]處理完,結(jié)束。并做標記temp=1*/
?????????????????????{???temp=1;break;??}
???????????????}
?????????}
????????}if(temp==1)break;??/*標記temp=1,全部處理結(jié)束*/
????????if((a[i]<=b[j])&&(i!=0)&&(j!=n))??
????????{???c[k]=b[j];??j++;??k++;?????}
????????if((a[i]>b[j])&&(i!=0)&&(j!=n))
????????{???c[k]=a[i];??i--;??k++;?????}
????????if(j==n)???????????/*b[]處理到完*/
????????{???for(;;)????????/*把b[]數(shù)組接到c[]后面*/
????????????{???if(i==0)???/*如果a[]處理完,結(jié)束。并做標記temp=1*/
????????????????{??????c[k]=a[i];temp=1;break;??}??
????????????????c[k]=a[i];
????????????????i--;
?????????k++;
????}
????????}if(temp==1)?break;/*標記temp=1,全部處理結(jié)束*/
????}
????for(s=0;s<m+n;s++)
????printf("%d???",c[s]);
????getch();
}
第8章 指針