一、單項選擇題(第192頁)
1-4.BBCC 5-8.AABA
二、填空題(第194頁)
1.1
2
4
8
16
32
64
128
256
512
2. ①a[age]++ ②i=18;i<26
3. ①break ②i==8
4. ①a[i]>b[j] ②i<3 ③j<5
5. ①b[j]=a[j][0] ②b[j]<a[j][k] 6.a[k++]=a[j]
三、編程題(第196頁)
1.源程序:
main()
{int a[4][4],i,j,s=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(i==j||i+j==3)s+=a[i][j];
printf("%d
",s);} / * 注:5×5矩陣不能照此計算! */
執(zhí)行結果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
68
2. 源程序:
main()
{int i,a[36];
a[0]=2;
for(i=1;i<=29;i++)a[i]=a[i-1]+2;
for(;i<=35;i++)a[i]=a[(i-30)*5+2];
for(i=0;i<=35;i++)printf("%d ",a[i]);}
執(zhí)行結果:
2 4 6 8 10 12 14 16 18 20
22 24 26 28 30 32 34 36 38 40
42 44 46 48 50 52 54 56 58 60
6 16 26 36 46 56
3. 源程序:
#include "stdlib.h"
#include "time.h"
main()
{int a[30],i,m=0;
randomize();
for(i=0;i<=29;i++)
{a[i]=rand();
if(m<a[i])m=a[i];
printf("%d ",a[i]);}
for(i=0;i<=29;i++)if(a[i]==m)a[i]=-1;
printf("
-----------------
");
for(i=0;i<=29;i++)
if(~a[i])printf("%d ",a[i]);
printf("
");}
執(zhí)行結果:
20679 29377 18589 9034 27083 4959 3438 5241 32278 23344
32499 29305 22340 5927 13031 2161 2583 31855 22977 14283
4851 22038 6992 11394 20887 27381 6293 18347 16414 10210
-----------------
20679 29377 18589 9034 27083 4959 3438 5241 32278 23344
29305 22340 5927 13031 2161 2583 31855 22977 14283 4851
22038 6992 11394 20887 27381 6293 18347 16414 10210
4.源程序:
main()
{int i,n=0,b[16];
scanf("%d",&i);
for(;i;i>>=1)b[n++]=i&1;
for(;n;)printf("%d",b[--n]);}
執(zhí)行結果:
9876
10011010010100
本題也可以不用數(shù)組。源程序如下:
#include "stdio.h"
main()
{int i,n;
scanf("%d",&i);
for(n=16;n;n--)
{asm ROL i,1
putchar(i&1|48);}
} / * ROL是循環(huán)左移的匯編指令 */
5. 源程序:
#include "stdlib.h"
#include "time.h"
#define M 5
#define N 6
main()
{int a[M][N],i,j,t[M];
randomize();
/ *生成M行N列隨機數(shù)*/
for(i=0;i<M;printf("
"),t[i++]=0)
for(j=0;j<N;j++)
printf("%4d",a[i][j]=random(50));
/ *找出每行的最小數(shù),t[M]是第M行的最小數(shù)所在的列數(shù)*/
for(i=0;i<M;i++)
for(j=0;j<N;j++)
if(a[i][t[i]]>a[i][j])t[i]=j;
/ *比較每個最小數(shù)在其所在的列上是否也是最小*/
for(j=0;j<M;j++)
for(i=0;i<M;i++)
{if(i==j)continue;
if(a[j][t[j]]>a[i][t[j]])
{t[j]=-1;break;}
}
printf("-------------------
");
/ *輸出在行和列上均為最小的數(shù)*/
for(i=0;i<M;i++)
if(t[i]!=-1)
printf("a[%d,%d]=%d
",i,t[i],a[i][t[i]]);
}
執(zhí)行結果:
13 19 13 20 0 1
20 41 6 16 35 30
3 5 37 8 23 15
6 36 24 29 18 1
1 5 28 21 46 34
-------------------
a[0,4]=0
a[1,2]=6
a[3,5]=1
a[4,0]=1
6. 源程序:
#include "stdlib.h"
#include "time.h"
#define M 5
#define N 7
main()
{int a[M][N],i,j,t=0;
randomize();
for(i=0;i<M;i++)
{a[i][N-1]=0;
for(j=0;j<N-1;j++)
{printf("%4d",a[i][j]=random(91)+10);
a[i][N-1]+=a[i][j];}
printf("%4d
",a[i][N-1]);}
for(i=1;i<M;i++)
if(a[i][N-1]>a[t][N-1])t=i;
if(t)for(j=0;j<N;j++)
{i=a[0][j];a[0][j]=a[t][j];a[t][j]=i;}
printf("-----------------
");
for(i=0;i<M;printf("
"),i++)
printf("%4d",a[i][j]);
}
執(zhí)行結果:
89 17 32 95 35 20 288
39 48 22 27 73 22 231
51 87 39 71 84 46 378
84 94 97 77 27 26 405
69 50 56 89 37 46 347
-----------------
84 94 97 77 27 26 405
39 48 22 27 73 22 231
51 87 39 71 84 46 378
89 17 32 95 35 20 288
69 50 56 89 37 46 347
7. 源程序:
#include "stdlib.h"
#include "time.h"
#define M 5
#define N 6
main()
{int a[M][N],i,j;
struct data{int value,x,y;}max,min;
max.value=0;min.value=100;
randomize();
for(i=0;i<M;printf("
"),i++)
for(j=0;j<N;j++)
{printf("%4d",a[i][j]=random(100)+1);
if(max.value<a[i][j])
{max.value=a[i][j];max.x=i;max.y=j;}
if(min.value>a[i][j])
{min.value=a[i][j];min.x=i;min.y=j;}
}
printf("-----------------
");
i=a[0][N-1];a[0][N-1]=max.value;a[max.x][max.y]=i;
i=a[M-1][0];a[M-1][0]=min.value;a[min.x][min.y]=i;
for(i=0;i<M;printf("
"),i++)
for(j=0;j<N;j++)
printf("%4d",a[i][j]);
}
執(zhí)行結果:
51 53 74 65 30 40
30 26 50 6 61 27
47 16 54 58 76 19
57 74 44 92 71 48
73 57 60 32 73 67
-----------------
51 53 74 65 30 92
30 26 50 73 61 27
47 16 54 58 76 19
57 74 44 40 71 48
6 57 60 32 73 67
9. 源程序:
main()
{char s[255];int i,j,b=1;
printf("Input a string:");
scanf("%s",s);
i=strlen(s);
for(j=1;j<=i/2;j++)
b=b&&(s[j-1]==s[i-j]);
printf(b?"Yes
":"No
");}
執(zhí)行結果:
Input a string:level
Yes
10. 源程序:
main()
{char s[255],t,max=0,min=0,l,i;
printf("Input a string(length>4):");
gets(s);
l=strlen(s);
for(i=0;i<l;i++)
{if(s[max]<s[i])max=i;if(s[min]>s[i])min=i;}
t=s[1];s[1]=s[max];s[max]=t;if(min==1)min=max;
t=s[l-2];s[l-2]=s[min];s[min]=t;
printf("%s
",s);}
執(zhí)行結果:
Input a string(length>4):C++Builder
Cu+Beild+r
11. 源程序:
main()
{char m[13][10]={"****","January","February","March",
"April","May","June","July","August","September",
"October","November","December"};
int i,j,k,a,s,n;
printf("Please input an integer(100..999):");
scanf("%d",&n);
printf("%d:%d+%d+%d=%d, %d%%13=%d, %s
", n,i,j,k,s,s,a,m[a=((s=(i=n/100)+(j=n/10%10)+(k=n%10))%13)]);}
執(zhí)行結果:
Please input an integer(100..999):539
539:5+3+9=17, 17%13=4, April