C語言程序設(shè)計復(fù)習(xí)題
C語言程序設(shè)計
一、單項選擇題
若以下各題所有庫函數(shù)都可以合法使用,且變量定義如下:
int ?m=5, i=3,j=2,*p, a=8,b=0;
int arr[10]={0,1,2,3,4,5,6,7,8,9},td[ ][3]={1,2,3,4,5,6};
float x=2.5,y=3.0;
double ?z=0; long n=1;
char ?c=’A’,ch[20]={’a’,’b’,’c’,’\0’,’d’,’\0’};
struct student
?{ ?int num;
? ? char name[20];
float score;
struct student *next;
}st1,st2,*ps;
FILE *fp;
?1.若將3和10從鍵盤輸入給變量a、x,輸入語句為scanf(“a=%d,x=%f”,&a,&x);正確的鍵盤輸入是(↙表回車):
?A a=3,x=10↙ ? B 3,10↙ ? C 3 ?10↙ ? ?D 3,10.0↙
2. 以下不是死循環(huán)的語句是:
A ?for ( ?; ;y+=i++); ? ? ?
B ?while(a) y+=i++ ;
C ?do{ y+=i++}while (a>0);
D ?for (b=0;(c=getchar()!=’\n’);)b++;
3.若p=arr;正確的語句是: ?
A ?*p=arr[10]; ?B ?*p=arr[2*a];
C ?p=arr[5]; ? D ?p[i]=arr[0];
4.若ps=&st1;則下面描述正確的是:
?A ?ps->num與st1->num等價。 B ?ps.num與st1.num等價。
?C ?ps->num與num等價。 ? ? ?D ?ps->num與st1.num等價。
5.語句printf(“%d",a<i>j);的輸出結(jié)果是:
A ?1 ? ? ? ? ? B ?0 ? ? ? ? ?C 8 ? ? ? ? D都不對
?6.表達(dá)式 j/i+a%i+x ?的值是:
A ?6 ? ? ? ? ? B ?4.5 ? ? ? C ?6.0 ? ? ? D 5.5
?7.表達(dá)式 b||a+i&&i<j ?的值是:
A ?不確定 ? ? ?B ?0.0 ? ? ? C 1 ? ? ? ? ? D 0
8.若執(zhí)行語句 a=b+=3*j+(int)x;則b的值為:
A ?1 ? ? ? ?B ?8 ? ? ? ? ?C ?0 ? ? ? ? D 8.5
9. 若執(zhí)行語句 a=b>j ?++i : i++;則a與i的值分別為:
A ?4 ,4 ? ? ?B ?3 ,4 ? ? ? C ?4 ,3 ? ? D ?3 ,3

編輯切換為居中
11.將字符串“Wan ? ?Ming”輸入數(shù)組ch中,正確的是:
? A、scanf(“%s”,ch) ? ? ? ? ? B、scanf(“%s”,&ch) ? ?
C、ch=“Wan ? ?Ming” ? ? ? D、gets(ch)
12. 執(zhí)行語句printf(“%c”,td[1][0]+’A’)的結(jié)果是:
? A、A ? ? ? ? ? ? ? ?B、B
C、D ? ? ? ? ? ? ? ? ? ? ? D、E
13.下列關(guān)于main()函數(shù)說法正確的是
A、 C語言程序總是從main()開始執(zhí)行
B、 C語言程序可以沒有main()函數(shù)
C、C語言程序可以包含多個main()函數(shù),但必須在不同的模塊中
D、C語言程序中main()函數(shù)不能有形式參數(shù)
14.下列關(guān)于函數(shù)說法正確的是
A、 C語言程序的函數(shù)內(nèi)還可定義其他的函數(shù)
B、 C語言程序的函數(shù)內(nèi)不可再定義其他的函數(shù)
C、 C語言程序的函數(shù)不能自己調(diào)用自己
D、 C語言程序中只有同一源文件中的函數(shù)才能有調(diào)用與被調(diào)用關(guān)系
15.若#define squ(x) x*x 執(zhí)行語句printf(“result=%d”,squ(i+j));結(jié)果為:
A、result=36 ? ? ?B、36 ? ? ? ?C、result=11 ? ? ?D、11
16. 若#define squ(x) (x)*(x) 執(zhí)行語句printf(“result=%d”,squ(i+j));結(jié)果為:
A、result=36 ? ? ?B、result=17 ? ? C、result=11 ? ? ?D、result=25
17.C語言對宏命令敘述正確的是
A、 宏替換占用運行時間
B、 編譯程序?qū)昝畹奶幚硎窃趯υ闯绦蛑衅渌煞志幾g之前進(jìn)行的
C、 宏命令的處理是在程序執(zhí)行時進(jìn)行的
D、 宏命令的處理是在程序連接時進(jìn)行的
18. 若執(zhí)行語句a- =a+=a*=10;a的值為:
A、0 ? ? ? ? ? ? ?B、-10 ? ? ? ? ? C、-100 ? ? ? ? ? D、100
19.若執(zhí)行語句p=arr; printf(“%d”,*(p+i));輸出結(jié)果為
A、0 ? ? ? ? ? ? ?B、2 ? ? ? ? ?C、3 ? ? ? ? ? ? ?D、1
20.若執(zhí)行printf(“%s”,ch);結(jié)果為:
A、’abc’ ? ? ? ? ?B、abc ? ? ? ?C、abcd ? ? ? D、abc\0d
21.若對數(shù)組進(jìn)行初始化:char carr[ ]={’s’,’t’,’u’,’d’,’e’,’n’,’t’},barr[ ]=”student”;則正確的是
A、carr與barr長度相同 B、carr比barr長
C、carr與barr相同 D、carr比barr短
22.若有說明語句int (*p)[N];則p是
A、N個指向整型變量的指針 B、可指向有N個整型元素的一維數(shù)組的指針
C、指向N個整型變量的函數(shù)指針 D、有N個指針元素的一維指針數(shù)組
23.若有說明語句int *f( );則函數(shù)f是
A、f是一個指向整型變量的數(shù)組指針 B、f返回一個整型數(shù)據(jù)
C、f是一個指向整型變量的函數(shù)指針 D、f 返回一個整型指針,指向函數(shù)本身
24.若函數(shù)f定義如下:void f(char *d, char *s)
{ while ( *d++=*s++);
}
函數(shù)f的功能是:
A、串比較 B、串復(fù)制 C、求串長 D、串反向
25.表達(dá)式100+a+’a’+x*z的值的數(shù)據(jù)類型為
A、int ? ?B、float ? ?C、double D、char
26.若要動態(tài)申請一個double型內(nèi)存單元,正確的語句是
A、pd=double ?malloc(sizeof(double)) B、pd=( * double)malloc(sizeof(double))
C、pd=double * malloc(sizeof(double)) D、pd=(double *)malloc(sizeof(double))
27.若要從鍵盤上輸入67892到變量n中,正確的語句是
A、scanf(“%lf”,&n); ? ? ? ? ? B、scanf(“%ld ”,&n); ?
C、scanf(“%lf ”,n); ? ? ? ? ? ? ? D、scanf(“%d ”,&n); ? ?
28.判斷字符變量c是否不為小寫英文字母的正確表達(dá)式是
A、’a’<=c <=’z’ ? ? ? ? ? ? ?B、c>= ‘a(chǎn)’ && c<=’z’
C、’a’≤c ≤’z’ ? ? ? ? ? ? ? ? D、c<’a’ || c>’z’ ?
29.下列每組表達(dá)式被執(zhí)行后,結(jié)果完全等價的是
A、 ?。╞= =0)與 ?。?!b ) ? ?B、b=(a=4,4*5) 與 b=a=4,4*5
C、float(i/j) 與 float i/j ? ? ?D、0<x<100 ?與 x>0 && x<100
30.表達(dá)式(m&1)==(m%2)的值是
A、 0 ?B、無法確定
C、 1 ? ? ? D、以上都不對
31.?dāng)?shù)學(xué)式ln2x+asin(60°)的C語言表達(dá)式是
A、log(2*x)+a*sin(60) ? ? ? ? ?B、ln(2*x) +a*sin(60/180*3.14)
C、ln(2*x) +a*sin(60) ? ? ?D、log(2*x)+a*sin(3.14*60/180) ?
32. 要打開一個已經(jīng)存在的非空文件file用于修改,則正確的語句是
A、fopen(“file”,”r+”) ? ? ? ? ?B、fopen(“file”,”w”) ?
C、fopen(“file”,”a+”) ? ? ? ? ? D、fopen(“file”,”r”) ?
33. 若打開文件時發(fā)生錯誤,則fopen()函數(shù)的返回值是
A、1 ?B、0 ? C、不確定 ? ? ?D、沒有返回值
34. 調(diào)用函數(shù)fseek(fp,-25L,2),其操作結(jié)果是
A、 文件位置指針從文件開始處前移25字節(jié)
B、文件位置指針從文件結(jié)尾處后移25字節(jié)
C、文件位置指針從文件當(dāng)前位置處前移25字節(jié)
D、文件位置指針從文件當(dāng)前位置處后移25字節(jié)
35. 能正確表示a和b同時為正或同時為負(fù)的邏輯表達(dá)式是
A、 (a>=0||b>=0)&&(a<0||b<0)
B、 (a>=0&&b>=0)&&(a<0&&b<0)
C、 (a*b>0&&(a*b<=0)
D、 a*b>0
36. 若x=y=z=1;表達(dá)式(x--||++y)&&(z++)執(zhí)行后,x,y,z的值分別為:
A. 0,1,1 ? ? ?B. 0,1,2 C. 0,2,1 ? ? D. 0,2,2
37.下列變量的初始化,不正確的是:
A. int x,y=5; ? ? ? ? ?B. float x=y=3.5;
? ?C. char s[]=”a”; ? ? ? ? ?D. int a[][3]={{1},{},{0,9}};
38. 可以代替while(x!=0)中的條件x!=0的是:
A. x ? B. !x ? ? ? ?C. x==1 ? ? ? ?D. x!=1
39.有關(guān)函數(shù)參數(shù),下列說法不正確的是:
A. 實參和形參是不同的變量; ? B. 實參可以將數(shù)據(jù)傳給形參;
C. 形參可以將數(shù)據(jù)傳給實參; ? D. 實參和形參必須個數(shù)相等,類型一致。
40.為避免嵌套的條件語句if-else的二義性,C語言規(guī)定else與if的配對原則是:
A. 同一行上的未配對if ? ? ? ?B. 縮排位置相同的未配對if ? ? ?
?C. 它之前最近的未配對的if ? D. 它之后最近的未配對if
41.對于一個二維數(shù)組,以下幾種方法中,不能正確表示i行j列元素的是:
A.td[i][j] ? ?B. *(td[i]+j) ? ?C. *(*(td+i)+j) ? ?D.*(td+i+j)
42.下面語句段不是死循環(huán)的是:
A. for(i=1;;i++) ? ? ? ? ? ? ? ? B. k=0;
? ? ? ? s=s+i; ? ? ? ? ? ? ? ? ? ? ? ? do{ k++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}while(k>0);
? C. i=0; ? ? ? ? ? ? ? ? ? ? ? ? ? D. a=1;
? ? ?while(1) ? ? ? ? ? ? ? ? ? ? ? ? ?while(a<10)
? ? ?{ i++; ? ? ? ? ? ? ? ? ? ? ? ? ? ?{ if(a>5) continue;
? ? ? ?if(i==20) break; } ? ? ? ? ? ? ? ?a++; }
43.下列字符串中,可以用作變量名的是:
A. _out2 ? ? ?B. 12abc ? ? C. while ? ? ? ?D. stu&3
44. 表達(dá)式0<a<5和0<a&&a<5的值分別為:
A. 0和0 ? ? ? B. 0和1 ? ? ? C. 1和0 ? ? D. 1和1
45.下列常數(shù)中,符合C語法規(guī)則的是:
A. ‘\’ ? ? ? ?B. .5e2 ? ? ? ?C. 018 ? ? ? ?D. e3
46.以下為合法的整型常量的是:
A. 027a ? ?B. 0.0 ? ?C. ?0x4e3 ? ?D. ?3×102 ?
47.以下為合法的整型常量的是:
A. ‘3’ ? ? ? B. 281 ? ?C. 5af D. ‘\101’
48.以下為合法的實型常量的是:
A. 1.5×102 ? ?B. 1e-5 ? ?C. ?1e1.5 ? D. ?E+5 ?
49.以下為合法的實型常量的是:
A. π ? ? ? ?B. ?.5 ? ? C. ?025.3 ? ? D. ? 0
50.以下為合法的字符型常量的是:
?A. "A" ? ?B. 64 ? ? ?C. '啊 ' ? ?D. '\n'
51. 以下為非法的字符型常量的是:
A. ‘ok’ ? ? ? B. ‘*’ ? C. ‘A’ ? ? ?D.‘\111’
52. 以下為合法的變量定義的是:
A. #define N 10 ?B. int a=12,b,c; ?float x,y,a=1.5;
int ?a[3*N]; ? ? ? ? ? ? ? ? ? ? ? ? ? ?
C. int m,f(x); ? ? ? ?D. char c,*pc=&c,n; ?
53.以下為非法的變量定義的是:
A. char 21cn; ? ? ? ? ? B. int ?k=0,*p=&k;
C. int ?a=b=c=21; ? ? ? D. static char[4]=”Good”;
54.以下是合法的表達(dá)式的是:
A. y=mn ? ?B. (x+y)++ ? ? ?C. x+y=8 ? ? D. ?putchar('A ')
55. 以下是非法的表達(dá)式的是:
A. a+=b*c ? ? ?B. i=0,k=k++ ? ?C. k==0 ? ? ? D. ?!a||m≤n &&7
56.下是非法賦值語句的是:
A.a(chǎn)+b=0 ? ? ? ? ? ? ? ? B. pc=&x ? ? ? ?
C. ps->name=”Tan Jing” ? D. static char[4]=’G’,’o’,’o’,’d’
二、程序理解題(指出程序執(zhí)行的結(jié)果)
1. ? #include <stdio.h>
main( )
{ int i,j,x=0;
for (i=0;i<2;i++)
{ ?x++;
for (j=0;j<3;j++) { if ?(j%2 ) continue;
? ? x++;
}
?x++;
?}
? printf(“x=%d\n”,x);
} ?
2. ? ? #include <stdio.h>
main( )
{ int k=0,j,x=0;
while (k<2)
{ ?++x;
for (j=0;j<3;j++)
{ if ?(j%2 ) ?break;
? x++;
}
? ? ? ? ? ?k++; ++x;
? ? ? ? ? }
? printf(“x=%d\n”,x);
} ?
3. #include <stdio.h>
main()
{int ?a=2,b=7,c=5;
switch (a>0)
{case 1:switch (b<0)
? ? ? { case 1: printf(“$”);break;
? ? ? ? case 2: printf(“!”); break;
? ? ? ?}
case 0: switch (c= =5)
? ? ? { case 0: printf(“*”);break;
? ? ? ?case 1:printf(“#”);break;
? ? ? ?default: printf(“%”);
? ? ? ?}
default: printf(“&”);
} printf(“\n”);
} ?
?
4. ? ? ?main()
{ int ?i,sum=0;
?for (i=1;i<=50;i++)
? { sum+=i;
? ? if (sum>10) ?break;
? ? printf(“sum=%-5d\n”,sum);
}
} ?
5. ? ? ?若輸入字符串program時,下列程序輸出為:
#include <stdio.h>
main()
{ char str[80];
void prochar(char *str,char ch);
?scanf(“%s”,str);
?prochar(str,’r’);
?puts(str);
}
void prochar(char *str,char ch)
{ char *p;
?for (p=str;*p!='\0';p++)
? ? if (*p==ch){*str=*p;(*str)++;str++;}
?*str='\0';
}
6. ?#include <stdio.h> #include <string.h>
main()
{ int ?i,j,temp, d[4][4]={{1,2,3,4},{5,6,7,8},{4,3,2,1},{1,2,3,4}};
for (i=0;i<4;i++)
? for (j=0;j<i;j++)
? ? ? if (d[i][j]>d[j][i]) d[j][i]=d[i][j];
for (i=0;i<4;i++)
?{ printf("\n");
? ?for (j=0;j<4;j++)
? ?if (j>=i) printf("%6d",d[i][j] ) ;
? ?else ? ? ?printf("%6c",' ') ;
? }
}
7. ? ? ? #include<stdio.h>
main()
{ int k;
?printf("\n");
?for (k=1;k<10;k++)
switch(k%2)
? ?{case 0: printf("#");break;
? ? case 1: k+=2;printf("*");
? ? defalt: printf("\n");
}
} ?
8. ? ? ?int d=1;
?fun(int p)
?{ static int d=5;
? ?d+=p;
? ?printf("(f)%-4d",d);
? ?return (d);
? ?}
? main()
? ?{int ?a=3;
? ? printf("\n(m)%d",fun(a*fun(d)));
? ?}
9. #include <stdio.h> ?main()
{char a[2][6]={"sun","moon"};
int j,i,len[2];
for(i=0;i<2;i++)
? { for (j=0;j<6;j++)
? ? ? if (a[i][j]=='\0')
? ? ? ? ?{ ?len[i]=j;
break;
}
? ? printf("%8s:%d\n",a[i],len[i]);
? ?}
10. int x=2,y=4;
f1(int x)
{ x++;y++;
?printf(“f1:x=%d,y=%d\n”,x,y);
}
f2(int *y)
{ x++;(*y)++;
?printf(“f2:x=%d,y=%d\n”,x,*y);
}
main()
{ int y=6;
?f1(x);f2(&y);
?printf(“main:x=%d,y=%d”,x,y);
}
三、程序填空題
1.從鍵盤輸入兩個整數(shù),按由大到小輸出,程序如下:
?main( )
?{ ? int ?a,b;
;
? scanf(“%d,%d”,&a,&b);
?if ( ?) swap( );
?printf(“%d,%d”,a,b);
}
void swap(int * x,int * y)
?{ ? ;
?t= ?*x; ? ?; ?*y=t;
?}
2.輸入任意多個十進(jìn)制正整數(shù),將其對應(yīng)的二進(jìn)制數(shù)的各位反序,形成新的十進(jìn)制數(shù)輸出。當(dāng)輸入0時程序結(jié)束。例如:輸入13,13→1101→1011→11,輸出11。
#include <stdio.h>
main()
{ int ?x,y,m,t;
?printf(“Enter a integer:”);
scanf(“%d”,&x);m=x;
while ( )
{ y=0;
do
{ t=x%2;
? ? ? ?y=
? ? ? x=x/2;
} while ( ? ?);
printf(“\nOld integer is:%d,New integer is: ?%d”,m,y);
printf(“Enter a integer:”);
scanf(“%d”,&x);
}
}
3. ?以下函數(shù)creatlist用來建立一個帶頭節(jié)點的單向鏈表,新的結(jié)點總是插入在鏈表的末尾。鏈表的頭指針作為函數(shù)值返回。讀入時字符以#表示輸入結(jié)束(#不存入鏈表)。
struct node
{char data;
struct node *next;
};
? creatlist()
{struct node *head,*s,*r;char ch;
head=(struct ?node *)malloc(sizeof(struct ?node));
r=head;
ch=getchar();
?while (ch!=’# ’)
{s=(struct node *)malloc(sizeof(struct node));
? s->data= ?;
r->next=s;
?
? ch=getchar();
}
? r->next= ?;
?;
}
4. 程序功能:輸出10~100之間的全部素數(shù),每行顯示5個。
#include <stdio.h>
main()
? { int i, j, ?
? ? for(i=11; i<=100; i+=2) ?
? ? ? { for(j=2; ? ? ? ?; j++) ?
? ? ? ? if(i%j= =0) ? ; ?
? ? ? ? if( j >= i ) { ? ;
counter++;
if( ? ? ? ? ) ?printf("\n");
?}
? ? ? }
}
5. 求菲波那契數(shù)列第n項的值。n由鍵盤輸入。菲波那契數(shù)列的第一、二項為1,以后各項為前兩項之和。即:f1=f2=1,fn=fn-1+fn-2
#include <stdio.h>
main()
{ int k,n,f1,f2,f;
? scanf( ) ;
? f1=f2=1;
? if ?( ) ?f=1 ;
? else ? for ( ?;k<=n;k++)
?{ f=f1+f2 ;
? f1=f2;
? ?;
? }
? ?printf("fibonacci number=%d\n",f);
?}
6.從鍵盤上輸入一個數(shù),將其插入到一個升序數(shù)組中,保持?jǐn)?shù)組仍然按升序排列。
#include <stdio.h>
? ? ? ?main()
{int data,temp,k;
static int a[9]={-10,2,4,8,10,15,25,50};
printf("\nEnter a data:");
scanf("%d",&data);
;
for (k=8; ;k- -)
?if (a[k]<a[k-1]) { ;
? ? ? a[k]=a[k-1];
? ? ? a[k-1]=temp;
? ? ? }
?else ?;
?for (k=0; ;k++)
? ?printf("%7d",a[k]);
}
7.求1!+2!+…+9!+20!,程序如下:
? #include <stdio.h>
? main()
? {float ?sum=0; ? int k;
?
for (k=1; ? ? ? ? ? ?;k++)
?sum+=jiecheng(k);
printf( ,sum);
}
float ? jiecheng(int ?n)
{ int j;
for (j=1; ; j++) ? ? ?t=t*j;
?
}
8. 從鍵盤上輸入一個整數(shù)字符串,將其轉(zhuǎn)換為一個整數(shù),如輸入“-1234”轉(zhuǎn)換為-1234。
#include<stdio.h>
#include<string.h>
main()
{char s[6];
int n;
gets(s);
if (*s=='-') n= ;
else n=chnum(s);
printf("%d\n",n);
}
int ?chnum(char *p)
{ int num=0,k,len,j;
? len=strlen(p);
? for (; ;p++)
? ? {k= ? ? ? ? ? ?;
? ? ?j=(--len);
? ? ?while (j-->0) ?;
? ? ?num=num+k;
}
? ;
}
9.以下程序從鍵盤輸入10個不相同的數(shù)到數(shù)組a中,再輸入一個數(shù)到x中,在a中查找與x值相同的元素所在的位置,填空完善該程序。
main()
{int a[11],x,i;
printf("Input 10 integers:\n");
for(i=1; ;i++)
? ? scanf("%d", );
printf("Input x:");
scanf("%d",&x);
*a=x;i=10;
while (x!=*(a+i))
?
if ( ) printf("%6d's position is :%3d\n",x,i);
else printf("%6d Not bee found !\n",x);
}
10. 從鍵盤上輸入一串英文字符(不含空格與其他字符),統(tǒng)計每個字符的數(shù)目,并輸出字母及相應(yīng)的數(shù)目。 ? ? ? ? ? #include <string.h> ? ? ? ? ? ?main()
{int i=0,c[52]={0},m=0;
char str[80];
printf("Input a string:");
scanf( ,str);
while (str[i])
{ if (str[i]>='A' && ?) c[str[i]-'A']++;
? if (str[i]>='a' && str[i] <='z') ?;
? i++;
}
for (i=0;i<26;i++)
? ?if (c[i]){ if (m%8==0) putchar('\n');m++;
? ? printf("%c:%-d ",i+'A',c[i]);
? ?}
for (i=0;i<26;i++)
? ?if (c[i+26]){if (m%8==0) putchar('\n');m++;
? ?printf("%c:%-d ", ,c[i+26]);
? }
}
四、用波浪線標(biāo)出錯誤,并在空白處改正(不要重寫程序)
1. 從鍵盤上輸入一個數(shù),判斷其是否為素數(shù)
#include<stdio.h>
main()
{ int a,flag,k;
?scanf(“%d”,a);flag=1;
?for (k=2;k<i;k++)
if (a/k=0) {break;flag=0;}
? ? ? ?if (flag=0);
? ? ? ?printf(“%d” is a prime number”,a);
}

編輯切換為居中
#include <stdio.h>
main()
{ float x,y;
?printf("Input ?a data:");
?scanf("%d",%x);
?if (x<0 && x>=10 ) ?printf("Input error!\n");
else { if (0≤x<1) ?y=2-x;
else if (x<3) y=1+1/2x;
? ?else if (x<6) ?y=2-0.5*(x-3.0);
? if (x<10) ? y=x2;
? printf("x=%.2f,y=%.1f\n",x,y);
? }
}

編輯切換為居中
4. 將“computer”按逆序顯示,程序如下:
#include <stdio.h>
main() ? ? ? ? ? ? ? ? ? ?
{char ?a[]=‘computer’; ?
void print(char *str) ? ?
print(&a); ? ? ? ? ? ? ? ?
}
void print(char *str)
{char *str ;
?int count ;
?if (*str!='\0')
{ while (*str){ str++;count++;}
? str - -;
? while(count- -){printf("%c", str);str++ ;} ? ?
?}
}
5.求滿足如下條件的最大自然數(shù)n.:12+22+32+…+n2≤1000
#include <stdio.h>;
main()
{int n=s=0;
while (s<=1000);
{ n++;
? s+=s+n*n;
}
print("n=%d",n );
}_
五、編程題
1.從鍵盤上輸入不超過50個非0數(shù),計算這些數(shù)的平均值并輸出,輸出高于平均值的數(shù)。輸入0時結(jié)束。
2.計算

編輯
3. 回文是從前向后讀與從后向前讀都是一樣的句子。編寫一函數(shù)判斷字符串是否為回文。字符串(少于80字符)從主函數(shù)中輸入。
4. 公雞5元一只,母雞3元一只,小雞一元3只,100元買100雞,求有多少中買法并輸出所有可能的買法,。
5.求1000以內(nèi)的完全數(shù)。一個整數(shù)如果正好等于它的因子之和(自身除外),則稱該數(shù)是完全數(shù)。如6=1+2+3,所以6是完全數(shù)。
6.用下面的公式求π的近似值,要求最后一項的絕對值小于10-4為止。
公式為:π/4≈1-1/3+1/5-1/7+1/9-1/11+…
題型題量及分值
一、選擇題:在每小題給出的四個選項中,只有一項是符合題目要求的,請把所選項前的字母填在題后的括號內(nèi)。
二、閱讀程序,寫出執(zhí)行結(jié)果。
三、程序填空題。
四、程序改錯題。用波浪線標(biāo)出錯誤,在空白處改正(不得重寫程序)。
五、編寫程序。
參 考 答 案
一、單項選擇題
1. A ? ?2. D ?3. D ? 4. D ?5. B ? ?6. B ? ?7. D ? ?8. B ? 9. B ? 10. A
? 11.D ? 12.D ?13. A ?14.B ?15.C ? ?16 .D ? 17.B ? 18. A ? 19.C ? 20.B
? 21. D ?22. B ?23 .C ? ?24 B ?25.C ? 26.D ? 27.B ? 28 D ? 29 A ? 30.C
31. D ?32.A ? 33 . B ? 34. B ? 35. D ? 36.B ? 37.B ? ?38.A ? ?39.C ?40.C
41.D ? 42. C ?43. A ? ?44.C ? 45.D ? ?46.C ? ?47.B ? ?48.B ? 49.B ?50.D
51. A ?52. A ? 53.B ? 54. D ? 55. D ? 56.C
二、程序理解題(指出程序執(zhí)行的結(jié)果)
1. ?x=8
2. ?程序輸出為:x=6
3. 程序輸出為:#&
4. ?程序輸出為:
?sum=1 ?
sum=3 ?
sum=6
sum=10
5.當(dāng)鍵盤輸入program.時,下列程序的輸出: ?pogam
6. ?程序輸出為: ?1 ? ?5 ? 4 ? 4
6 ? 7 ? 8
2 ?3
4
7. 程序輸出為:
? ? ? *
? ? ? ? ? ? #*
? ? ? ? ? ? #*
?
8. 程序輸出為:
? ? ? ? (f)6 ? (f)24
? ? ? ? ? ? ?(m)24
9 . ? sun:3
moon:4
10. ? ? f1:x=3 ,y=5 ? ? ? ? ? ? ? ?
f2:x=3 ,y=7 ? ? ? ? ? ? ? ? ? ? ?
main:x=3 ,y=7
三、程序填空題
1.void swap(int * x,int * y); a<b; &a,&b; int ?t; ? *x= *y;
2.x!=0 、 y+2*t ? ? 、 x!=0 ? ? 、 ?m=x;
3. 以下函數(shù)creatlist用來建立一個帶頭節(jié)點的單向鏈表,新的結(jié)點總是插入在鏈表的末尾。鏈表的頭指針作為函數(shù)值返回。讀入時字符以#表示輸入結(jié)束(#不存入鏈表)。
struct node
{char data;
struct node *next;
};
struct node ?creatlist()
{struct node *head,*s,*r;char ch;
head=(struct ?node *)malloc(sizeof(struct ?node));
r=head;
ch=getchar();
?while (ch!=’# ’)
{s=(struct node *)malloc(sizeof(struct node));
? s->data= ?ch ? ? ?;
r->next=s;
r=s;
? ch=getchar();
}
? r->next= ?NULL ? ? ?;
?return head;
}
4. 輸出10~100之間的全部素數(shù),每行顯示5個。
#include <stdio.h>
main()
? ? ? { int i, j, counter=0;
? for(i=11; i<=100; i+=2) ?
? { for(j=2; j< i; j++) ?
? ? ? ? if(i%j= =0) ? break; ?
? ? ? ? if( j >= i ) { printf("%8d",i);
counter++;
if(counter%5= =0) ?printf("\n");
?}
? ? ? }
}
5. 求菲波那契數(shù)列第n項的值。n由鍵盤輸入。菲波那契數(shù)列的第一、二項為1,以后各項為前兩項之和。即:f1=f2=1,fn=fn-1+fn-2
#include <stdio.h>
main()
{ int k,n,f1,f2,f;
scanf("%d",&n) ;
? f1=f2=1;
? if ?(n<=2) ?f=1 ;
? else ? for ( k=3;k<=n;k++)
?{ f=f1+f2 ;
? f1=f2;
? f2=f;
? }
? ?printf("fibonacci number=%d\n",f);
?}
6.從鍵盤上輸入一個數(shù),將其插入到一個升序數(shù)組中,保持?jǐn)?shù)組仍然按升序排列。
#include <stdio.h>
? ? ? ? main()
{int data,temp,k;
static int a[9]={-10,2,4,8,10,15,25,50};
printf("\nEnter a data:");
scanf("%d",&data);
a[8]=data;
for (k=8;k>0;k- -)
?if (a[k]<a[k-1]) { temp=a[k];
? ? ? a[k]=a[k-1];
? ? ? a[k-1]=temp;
? ? ? }
?else break;
?for (k=0;k<=8;k++)
? ?printf("%7d",a[k]);
}
7.求1!+2!+…+9!+20!,程序如下:
? #include <stdio.h>
? main()
? {float ?sum; ? int k;
float ?jiecheng(int ?n);
sum=0;
for (k=1;k<=20;k++) ?sum+=jiecheng(k);
printf(“sum=%.1f”,sum);
}
float ? jiecheng(int ?n)
{ int j; float ?t=1;
for (j=1;j<=n; j++) ? ? ?t=t*j;
? return ? t;
}
8. 從鍵盤上輸入一個整數(shù)字符串,將其轉(zhuǎn)換為一個整數(shù),如輸入“-1234”轉(zhuǎn)換為-1234。
#include<stdio.h>
#include<string.h>
main()
{char s[6];
int n;
gets(s);
if (*s=='-') n=-chnum(s+1);
else n=chnum(s);
printf("%d\n",n);
}
int ?chnum(char *p)
{ int num=0,k,len,j;
? len=strlen(p);
? for (;*p!='\0';p++)
? ? {k=*p-'0';
? ? ?j=(--len);
? ? ?while (j-->0) k=k*10;
? ? ?num=num+k;
}
return num;
}
9. ?以下程序從鍵盤輸入10個不相同的數(shù)到數(shù)組a中,再輸入一個數(shù)到x中,在a中查找與x值相同的元素所在的位置,填空完善該程序。
main()
{int a[11],x,i;
printf("Input 10 integers:\n");
for(i=1;i<=10;i++)
? ? scanf("%d",a+i);
printf("Input x:");
scanf("%d",&x);
*a=x; ? ?i=10;
while (x!=*(a+i))
i--;
if (i) printf("%6d's position is :%3d\n",x,i);
else printf("%6d Not bee found !\n",x);
}
10. 從鍵盤上輸入一串英文字符(不含空格與其他字符),統(tǒng)計每個字符的數(shù)目,并輸出字母及相應(yīng)的數(shù)目。
#include <string.h> ? ? ? ? ? ?main()
{int i=0,c[52]={0},m=0;
char str[80];
printf("Input a string:");
scanf("%s",str);
while (str[i])
{ if (str[i]>='A' && str[i] <='Z') c[str[i]-'A']++;
? if (str[i]>='a' && str[i] <='z') c[str[i]-'a'+26]++;
? i++;
}
for (i=0;i<26;i++)
? ?if (c[i]){ if (m%8==0) putchar('\n');m++;
? ? printf("%c:%-d ",i+'A',c[i]);
? ?}
for (i=0;i<26;i++)
? ?if (c[i+26]){if (m%8==0) putchar('\n');m++;
? ?printf("%c:%-d ",i+'a',c[i+26]);
? }
}
四、用波浪線標(biāo)出錯誤,并在空白處改正(不要重寫程序)
1.從鍵盤上輸入一個數(shù),判斷其是否為素數(shù)
#include<stdio.h>
main()
{ int a,flag,k; ? ? ? ? ? ? ? ? ? ? ?改為:
?scanf(“%d”,a);flag=1; ? ? ? ? ? ? ? ? ?&a
?for (k=2;k<i;k++) ? ? ? ? ? ? ? ? ? ? ?k<a
if (a/k=0) {break;flag=0;} ? ? ? ?a%d==0 {flag=0;break;}
? ? ? ?if (flag=0); ? ? ? ? ? ? ? ? ? ? ? ? ?(flag==1) ?去掉;
? ? ? ?printf(“%d” is a prime number”,a);
? ? }
2.計算分段函數(shù)的值
? ? ? ? ? ? 2-x ? ? ? ? ? 0≤x<1
? ? ? ? ? ? 1+1/2x ? ? ? ?1≤x<3
? ? ?y= ? ? 2-0.5(x-3) ? ? 3≤x<6
? ? ? ? ? ? x2 ? ? ? ? ? ? ? ? ? ? ? ?6≤x<10
#include <stdio.h>
main() 改為:
{ float x,y;
?printf("Input ?a data:");
?scanf("%d",%x); “%f”,&x
?if (x<0 && x>=10 ) ?printf("Input error!\n"); ?||
else { if (0≤x<1) ?y=2-x; ?x>=0 && x<1
else if (x<3) y=1+1/2x; y=1+1.0/2*x;
? ?else if (x<6) ?y=2-0.5*(x-3.0);
? if (x<10) ? y=x2; else if (x<10)y=x*x;
? printf("x=%.2f,y=%.1f\n",x,y);
? }
}

編輯切換為居中
4. 將“computer”按逆序顯示,程序如下:
#include <stdio.h>
main() ? ? ? ? ? ? ? ? ? ?改為:
{char ?a[]=‘computer’; ? “computer”
void print(char *str);
print(&a); ? ? ? ? ? ? ? ? ? ? ? a
}
void print(char *str)
{char *str ; ? ?char *str ?
?int count ?; ? count=0;
?if (*str!=\0) ? ? ? ? ? ? ? ? ? ? ? ?‘\0’
{ while (str){ str++;count++;} ? ? ?*str
? str - -;
? while(count- -)
{printf("%s", *str);str++ ;} ? ? ?%c ? str--
?}
}
5.求滿足如下條件的最大自然數(shù)n.:12+22+32+…+n2≤1000
#include <stdio.h>; ?去掉;
main()
{int n=s=0; ?n=0,s=0;
while (s<=1000);
{ n++;
s+=s+n*n; ? ? ? ? ? ? ? ? ? ?s=s+n*n;
}
print("n=%d",n ); ? ? ? ? ? ? printf
}
五、編程題
1. 從鍵盤上輸入不超過50個非0數(shù),計算這些數(shù)的平均值并輸出,輸出高于平均值的數(shù)。輸入0時結(jié)束。
#include <stdio.h>
main()
{ float data[50],ave,sum=0,x;
?int i,n=0,count;
?printf("Input ?data:");
?scanf("%f",&x);
? while (x!=0&&n<=50)
? { sum+=x;
? ? data[n++]=x;
? ? scanf("%f",&x);
? }
?ave=sum/n;
?printf("average= %f\n",ave);
?for (count=0,i=0;i<n;i++)
? ?if (data[i]>=ave)
? ? ?{ printf("%f\n",data[i]);
?count++;
? ? ? if (count%8= =0) printf(“\n”);
? ? ?}
}

編輯切換為居中
#include <stdio.h>
main()
{ int i,n,s; ? ? ? ?
? float sum;
? scanf("%d",&n);
? s=0; ? sum=0.0;
? for (i=1;i<=n;i++)
? { s+= i;
sum+=1.0/s ; ? ?
}
? printf("sum=%f\n",sum);
?}
3. 回文是從前向后讀與從后向前讀都是一樣的句子。編寫一函數(shù)判斷字符串是否為回文。字符串(少于80字符)從主函數(shù)中輸入。
#include <stdio.h>
#include <string.h>
int huiwen(char str[])
{ int i=0,j, flag=0;
j=strlen(str)-1;
?while (i<j&&str[i]= =str[j]) { i++; j--;}
?if (i>=j) flag=1;
?return flag;
}
main()
{ char str[80];
?printf("Enter string:"); ?gets(str);
?if (huiwen(str)) ?printf("%s is a palindroma\n",str);
?else ? ? ?printf("%s is not palindroma\n",str);
}
4. 公雞5元一只,母雞3元一只,小雞一元3只,100元買100雞,列出所有可能的買法。
main()
{ int i,j,k,m=0;
for (i=1;i<=20;i++)
? ?for (j=1;j<=33;j++)
? ?{k=100-i-j;
? ?if (i*5+j*3+k/3==100) && (k%3==0) /* ((i*15+j*9+k==300)) &&(k%3==0))*/
? ? ? {printf("i=%5dj=%5dk=%5d\n",i,j,k);m++;}
? ?}
printf("m=%d\n",m);
}_
5.求1000以內(nèi)的完全數(shù)。一個整數(shù)如果正好等于它的因子之和(自身除外),則稱該數(shù)是完全數(shù)。如6=1+2+3,所以6是完全數(shù)。
#include <stdio.h>
? main()
? {int ?k, s=0,a;
? ? for (a=1;a<=1000;a++)
? ? ? { for(k=1;k<=a/2;k++)
? ? ? ? ?if (a%k= =0) s+=k;
? ? ? ? if (s= =a) printf(“%-5d”,a);
? ? ? }
? ?}
6.用下面的公式求π的近似值,要求最后一項的絕對值小于10-4為止。
公式為:π/4≈1-1/3+1/5-1/7+1/9-1/11+…
#include <stdio.h>
#include <math.h>
main()
{ float pi,temp,n;
?int s=1;
?pi=0; n=1; temp=1;
?while (fabs(temp) >= 1e-4)
?{ ?temp=s/n;
? ? pi+=temp;
? ? n+=2;
? ? s=-s;
?}
?pi=pi*4;
?printf("pi = %.4f\n",pi);
}