资源描述
(完整版)C语言 程序阅读题
五、程序阅读题
要求:阅读下列程序,将程序运行时的输出结果填写在答案栏内。
1。 #include 〈stdio.h>
void main()
{ char ch; int k=0;
for(ch='A’;ch<’H’;ch++,k++)
if(k%2==0) printf(”%c ",ch);
printf(”\n");
}
输出结果:A C E G
2. #include 〈stdio.h>
void main( )
{ int k=0;
char c='A’;
do {
switch (c++)
{ case ’A’: k++; break;
case ’B': k—-;
case ’C’: k+=2; break;
case ’D’: k=k%2; break;
case ’E': k=k*10; break;
default: k=k/3;
}
k++;
} while(c〈’G’);
printf("k=%d\n”, k);
}
输出结果: K=8
3。 #include 〈stdio。h〉
void main()
{ int a[6]={12,4,17,25,27,16},b[6]={27,13,4,25,23,16},i,j;
for(i=0;i<6;i++) {
for(j=0;j<6;j++) if(a[i]==b[j])break;
if(j<6) printf(”%d ”,a[i]);
}
printf(”\n”);
}
输出结果: 4 25 27 16
4。 #include 〈stdio.h>
void main()
{ int a[8]={1,0,1,0,1,0,1,0},i;
for(i=2;i〈8;i++) a[i]+=a[i-1]+a[i-2];
for(i=0;i〈8;i++) printf("%d ",a[i]);
printf(”\n”);
}
输出结果: 1 0 2 2 5 7 13 20
5. #include 〈stdio.h〉
void main()
{ int s=1,t=1,a,n;
scanf(”%d%d”,&a,&n);
for(int i=1;i〈n;i++)
{
t=t*10+1; s=s+t;
}
s*=a; printf(”SUM=%d\n”,s);
}
输入数据:2,4
输出结果: SUM=2468
6。 #include 〈stdio。h〉
#define PR(x,y) printf("%s=\t%d\n”,x,y)
void main( )
{ int x=03,y=02,z=01;
PR("x|y&z",x|y&z); PR("x^y&~z”,x^y&~z);
PR("x&y&z”,x&y&z); x=1; y=-1;
PR("! x|x”,! x|x); PR(”~x|x”,~x|x);
}
输出结果:
x|y&z=3
x^y&~z=1
x&y&z=0
!x|x=1、~x|x=-1
7。 #include <stdio。h〉
void main()
{ int a[5]={2,4,6,9,12},b[6]={2,3,4,6,8,9};
for(int i=0;i〈5;i++)
for(int j=0;j<6;j++)
if(*(a+i)==*(b+j)) printf("%d ",*(a+i));
printf("\n");
}
输出结果: 2 4 6 9
8. #include <stdio。h>
#include <string。h〉
void main()
{ char str1[]=”*******”;
for(int i=0;i〈4;i++) {
printf("%s\n",str1);
str1[i]=' ';
str1[strlen(str1)-1]=’\0’;
}
}
输出结果:
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
9. #include <stdio。h>
void main()
{ char a[5][8];
for(int i=0;i<5;i++)
for(int j=0;j<8;j++)
if(i==0||i==4) a[i][j]='—';
else a[i][j]=' ';
for(i=1;i<4;i++) { a[i][0]='|'; a[i][5]=’|’; }
for(i=0;i<5;i++) a[i][6]=’\0';
for(i=0;i<5;i++) printf("%s\n",a[i]);
}
输出结果:
—
-
—
—
-
-
|
|
|
|
|
|
—
-
—
—
—
-
10。 #include 〈stdio.h>
void main()
{ char a[8],temp; int i,j;
for(i=0;i〈7;i++) a[i]=’a'+i;
for(i=0;i〈3;i++) {
temp=a[0]; for(j=1;j<7;j++) a[j-1]=a[j];
a[6]=temp;a[7]='\0';
printf("%s\n”,a);
}
}
输出结果:
b
c
d
e
f
g
a
c
d
e
f
g
a
b
d
e
f
g
a
b
c
11。 #include <stdio。h>
void main()
{ int i,j,n;
for(i=0;i〈4;i++) {
for(j=1;j〈=i;j++) printf(" ”);
n=7—2*i;
for(j=1;j〈=n;j++) printf("%1d”,n);
printf(”\n");
}
}
输出结果:
7
7
7
7
7
7
7
5
5
5
5
5
3
3
3
1
12. #include 〈stdio.h〉
void main()
{ char a[8],temp; int j,k;
for(j=0;j〈7;j++) a[j]=’a’+j;
a[7]='\0’;
for(j=0;j〈3;j++) {
temp=a[6];
for(k=6;k〉0;k-—) a[k]=a[k—1];
a[0]=temp;
printf(”%s\n”,a);
}
}
输出结果:
g
a
b
c
d
e
f
f
g
a
b
c
d
e
e
f
g
a
b
c
d
13。 #include 〈stdio。h>
void print(int,char,int);
void main()
{ print(3,'*’,1); print(2,’*',3);
print(1,'*’,5); print(2,'#',3);
print(2,’#',3);
}
void print(int begin,char pzf,int n)
{ char a[50];
for(int i=0;i<begin;i++) a[i]=' ';
for(i=0;i<n;i++) a[begin+i]=pzf;
a[begin+i]='\0';
printf("%s\n”,a);
}
输出结果:
*
*
*
*
*
*
*
*
*
#
#
#
#
#
#
14. #include <stdio.h>
void main()
{ int a,b,c,find(int,int,int);
scanf("%d%d%d”,&a,&b,&c);
printf("%d\n%d,%d,%d\n”,find(a,b,c),a,b,c);
}
int find(int a,int b,int c)
{ int u,s,t;
u=((u=(a〉b)?a:b)〉c)? u:c;
t=((t=(a〈b)?a:b)〈c)? t:c;
s=a+b+c—u-t; a=u;
b=s; c=t; return s;
}
运行时输入: 5 –7 3,输出结果: 3
5,—7,3
15。 #include <stdio。h>
void fun1(int n,int a[][3])
{ for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j]=a[i][j]/a[i][i];
}
void main()
{ int a[3][3]={{6,4,2},{8,6,4},{9,6,3}};
fun1(3,a);
for(int i=0;i<3;i++) {
for(int j=0;j〈3;j++) printf("%d ”,a[i][j]);
printf("\n");
}
}
输出结果:
1
4
2
1
1
4
3
2
1
16。 #include <stdio.h〉
int fun3(int m)
{ int i;
if(m==2||m==3) return 1;
if(m〈2||m%2==0) return 0;
for(i=3;i〈m;i=i+2) if(m%i==0)return 0;
return 1;
}
void main()
{ int n;
for(n=1;n<10;n++)
if(fun3(n)==1) printf("%d ",n);
}
输出结果: 2 3 5 7
17。 #include 〈stdio.h>
void sub(int *a,int *b,int *c,m,n)
{ int i,j;
for(i=0;i〈m;i++) *(c+i)=*(a+i);
for(j=0;j<n;j++,i++) *(c+i)=*(b+j);
}
void main()
{ int i,x[5]={1,5,3,8,4},y[3]={9,-4,6},z[8];
sub(x,y,z,5,3);
for(i=0;i<8;i++) printf(”%d ",z[i]);
printf("\n");
}
输出结果: 1 5 3 8 4 9 –4 6
18. #include <stdio。h>
void sort(char *a[],int n)
{ int i,j,l; char *temp;
for(i=0;i<n-1;i++) {
l=i;
for(j=i+1;j<n;j++)
if(strcmp(a[j],a[l])〈0) l=j;
if(l!=i) {
temp=a[i]; a[i]=a[l]; a[l]=temp;
}
}
}
void main()
{ char *name[4]; int k;
char ch[4][15]={"morning","afternoon”,"night”,”evening” };
for(k=0;k<4;k++) name[k]=ch[k];
sort(name,4);
for(k=0;k〈4;k++)
printf(”%s\n”,name[k]);
}
输出结果: afternoon
evening
morning
night
19. #include "stdio。h”
void pline(char *a,char c,int l,int n)
{ int i;
for(i=1;i<l;i++) { *a=’ ’; a++; }
for(i=1;i〈=n;i++,*a=c,a++); *a='\0’;
}
void main()
{ char a[80];
int i; void (*pf)(char*,char,int,int);
pf=pline;
for(i=1;i<5;i++) {
(*pf)(a,'$’,5-i,2*i—1);
puts(a);
}
}
输出结果:
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
20。 #include ”stdio.h"
int binary(int x,int a[],int n)
{ int low=0,high=n—1,mid;
while(low<=high) {
mid=(low+high)/2;
if(x〉a[mid]) high=mid—1;
else if(x〈a[mid]) low=mid+1;
else return(mid);
}
return(—1);
}
void main( )
{ static int a[]={4,0,2,3,1}; int i,t,j;
for(i=1;i<5;i++) {
t=a[i]; j=i-1;
while(j〉=0 && t>a[j]) { a[j+1]=a[j]; j-—; }
a[j+1]=t;
}
printf ("%d \n",binary(3,a,5));
}
输出结果: 1
21. #include<math。h〉
#include <stdio。h>
void main( )
{ double f(double,int);
printf(”%1f\n",f(2.0,14));
}
double f(double x,int n)
{ double t;
if(n==1) t=x;
else {
if(n/2*2==n)t=x*f(x,n/2);
else t=x*pow(f(x,n/2),2.0);
}
return t;
}
输出结果: 256。000000
22。 #include <stdio.h〉
double x,u,v
double t(double a,double(*f)(double))
{ return(*f)(a*a); }
double f(double x)
{ return 2.0*x; }
double g(double x)
{ return 2.0+x; }
void main()
{ x=4.0;u=t(x,f);v=t(x,g);
printf(”u=%5.3fv=%5。3f\n”,u,v);
}
输出结果: U=32.000v=18.000
23. #include <stdio。h>
void main()
{ static struct s1{ char c[4],*s; };
s1={"abc","def"};
static struct s2{char *cp; struct s1 ss1;}
s2={”ghi",{"jkl”,”mno"}};
printf("%c,%c\n",s1。c[0],*s1。s);
printf("%s,%s\n",s1.c,s1.s);
printf("%s,%s\n",s2.cp,s2.ss1。s);
printf ("%s,%s\n",++s2。cp,++s2。ss1。s);
}
输出结果: a,d
abc,def
ghi,mno
hi,no
24。 #include 〈stdio。h〉
struct s1{ char *s; int i; struct s1 *s1p; };
void main()
{ static s1 a[]={{"abcd",1,a+1},{”efgh",2,a+2},{"ijkl”,3,a}};
int i;
for(i=0;i<2;i++) {
printf(”%d\n”,-—a[i].i);
printf("%c\n",++a[i]。s[3]);
}
}
输出结果: 0
e
1
i
25. #include<stdio.h〉
void main()
{ FILE *fp; int i;
char s1[80],s[]="abcdefghijklmnop”;
fp=fopen("alf。dat”,”wb+”); i=sizeof(s);
fwrite(s,i,1,fp); rewind(fp); fread(s1,i,l,fp);
printf("all=%s\n”,s1); fseek(fp,0,0);
printf(”seek1 ch=%c\n",fgetc(fp));
fseek(fp,10,1);
printf(”seek2 ch=%c\n”,fgetc(fp));
fseek(fp,1,1);
printf(”seek3 ch=%c\n",fgetc(fp));
fclose(fp);
}
输出结果: All=abcdefghijklmnop
seek1 ch=a
seek2 ch=l
seek3 ch=n
26.#include 〈stdio.h>
void main()
{ struct T1{ char c[4],*s; }
s1={"abc","def"};
struct T2{ char *cp; T1 ss1;}
s2={”ghi”,{"jkl","mno"}};
printf("%c,%c\n”,s1.c[0],*s1。s);
printf(”%s,%s\n”,s1。c,s1.s);
printf(”%s,%s\n”,s2.cp,s2。ss1。s);
printf(”%s,%s\n”,++s2。cp,++s2。ss1。s);
}
输出结果为
a,d
abc,def
ghi,mno
hi,no
27. #include 〈stdio。h〉
void main()
{ struct info { int data; info *pn; };
info *base,*p;
base=NULL;
for(int i=0;i<10;i++) {
p=new info; p-〉data=i+1;
p-〉pn=base; base=p;
}
p=base;
while(p!=NULL) { printf(”%2d”,p—〉data); p=p-〉pn; }
printf (”\n”);
}
输出结果为:
10 9 8 7 6 5 4 3 2 1
28.#include <stdio.h>
#include <string。h〉
void del_bk(char *p)
{ char *p1; p1=p;
while(*p1!=’\0’)
if(*p1==' '&&*(p1+1)==’ ') strcpy(p1,p1+1);
else p1++;
}
void main()
{ char *aa=”aa bb cccc ddd efg h";
printf(”%s\n”,aa); del_bk(aa); printf(”%s\n",aa);
}
输出结果: aa bb cccc ddd efg h
aa bb cccc ddd efg h
29.#include <stdio。h>
void main()
{ int m[]={1,2,3,4,5,6,7,8,9},i,j,k;
for(i=0;i〈4;i++) {
k=m[i]; m[i]=m[8-i]; m[8—i]=k;
for(j=0;j〈9;j++)
printf(”%d ”,m[j]);
putchar('\n’);
}
}
输出结果为:
923456781
983456721
987456321
987654321
30.#include <stdio.h〉
void main()
{ int x[4][4]={{1,2,3,4},{3,4,5,6},{5,6,7,8},{7,8,9,10}};
int i,j;
for(i=0;i<4;i++)
for(j=0;j〈4;j++)
*(*(x+i)+j)/=*(*(x+i)+i);
for(i=0;i<4;i++) {
for(j=0;j〈4;j++) printf("%3d”,*(*(x+i)+j));
putchar('\n');
}
}
输出结果为:1 2 3 4
0 1 5 6
0 0 1 8
0 0 0 1
31.#include 〈stdio。h>
#include 〈string。h>
void main()
{ char line[]=”123456789”;
int i,k=strlen(line);
for(i=0;i〈4;i++) {
line[k-i]=’\0';
puts(line+i);
}
}
输出结果为: 123456789
2345678
34567
456
32. #include 〈stdio。h>
#include 〈string。h〉
void main()
{ char a[5][20]={”abc","def”,"123”,”456","#$%”};
int i,j;
for(i=0;i<4;i+
展开阅读全文