【程序说明】
验证哥德巴赫猜想:任何一个大于6的偶数均可表示为两个素数之和。例如6=3+3,8=3+5,…,18=7+11。将6~20之间的偶数表示成两个素数之和,打印时一行打印5组。要求定义和调用函数prime(m)判断m是否为素数,当m为素数时返回1,否则返回0。素数就是只能被1和自身整除的正整数,1不是素数,2是素数。
运行示例:
6=3+3,8=3+5,10=3+7,12=5+7,14=3+11 16=3+13 18=5+13 20=3+17 18=7+11 【程序】
#include
if(m == 1) return 0; n = sqrt(m);
for(i = 2; i <= n; i++) if(m % i == 0) return 0 (5)
}
main()
{ int count, i,number; count=0;
for(number=6;number<=20;number=number+2){ for(i=3;i<=number/2;i=i+2) if(__(6)_____){
printf(“%d=%d+%d”,number,i,number – i); count++;
if(__(7)_____) printf(“\\n”); ( (8) ) } } }
【供选择的答案】
(5) A、; B、return 1;
C、return 0; D、else return 1; (6) A、prime(i)!=0 || prime(number – i)!=0 B、prime(i)!=0 && prime(number – i)!=0
C、prime(i)==0 || prime(number – i)==0
D、prime(i)==0 && prime(number – i)==0 (7) A、count % 5 ==0 B、count % 5!=0 C、(count+1)%5==0 D、(count+1)%5!=0 (8) A、break; B、else break; C、continue; D、; 试题3(每小题3分,共12分)
阅读下列程序说明和程序,在每小题提供的若干可选答案中,挑选一个正确答案。 【程序说明】
输入一行字符,统计并输出其中数字字符、英文字母和其他字符的个数。要求定义并调用函数count(s, digit,letter,other)分类统计字符串s中数字字符、英文字母和其它字符的
17
个数,函数形参s的类型是字符指针,形参digit,letter,other的类型是整形指针,函数类型是void。
运行示例:
Enter characters:f(x,y)=5x+2y-6 Digit=3 letter=5 other=6 【程序】
#include
void count (char *s, int * digit, int * letter, int * other) { _____(9)___ while(_____(10)___){
if (*s>=?0? && *s<=?9?) (*digit)++;
else if ((*s>=?a? && *s<=?z?)||(*s>=?A? && *s<=?z?)) (*letter)++; else
(*other)++; s++; } }
main() { int i=0,digit,letter,other; char ch,str[80];
printf(“Enter characters:”); ch=getchar();
while (____(11)___){ str[i]=ch; i++;
ch=getchar(); }
str(i)=?\\0?; ___(12)__;
Printf(“digit=&d letter=%d other=%d\\n”, digit,letter,other); }
【供选择的答案】
(9) A、int dight=0,letter=0,other=0;
B、int *dight=0,*letter=0,*other=0; C、dight=letter=other=0;
D、*dight=*letter=*other=0; (10) A、*s++ !=?\\0 ? B、*s++ !=?\\n? C、*s!=?\\0? D、 *s !=?\\n? (11) A、ch !=?\\0? B、ch !=?\\n? C、ch ==?\\0? D、ch ==?\\n?
(12) A、count(str,&digit,&letter,&other)
B、count(&str,&digit,&letter,&other)
C、count(*str, digit, letter,other)
D、count(*str,*digit,*letter,*other)
试题4(每小题3分,共12分)
阅读下列程序并回答问题,在每小题提供的若干可选答案中,挑选一个正确答案。
【程序】
18
#include
{ int flag=0,i;
int a[7]={8,9,7,9,8,9,7};
for(i=0;i<7;i++) if(a[i]==7){ flag=i
break; }
printf(“%d\\n”,flag); flag=-1
for(i=6;i>=0;i--) if(a[i]==8){ break; flag=i; }
printf(“%d\\n”,flag); flag=0;
for(i=0;i<7;i++) if(a[i]==9){
printf(“%d”,i); }
printf(“\\n”); flag=0;
for(i=0;i<7;i++)
if(a[i]==7) flag=i; printf(“%d\\n, flag”);
}
(13) 程序运行时,第1行输出 (13) 。
A、2 B、0 C、3 D、6 (14) 程序运行时,第2行输出 (14) 。
A、4 B、-1 C、0 D、5 (15) 程序运行时,第3行输出 (15) 。
A、2 4 6 B、4 C、1 3 5 D、6 (16) 程序运行时,第4行输出 (16) 。
A、2 4 6 B、2 C、1 3 5 D、6 试题5(每小题3分,共12分)
阅读下列程序并回答问题,在每小题提供的若干可选答案中,挑选一个正确答案。
【程序】
int f1(int n)
{ if(n==1) return 1 ;
else return f1(n-1) + n; }
int f2(int n) { switch(n){ case 1:
case 2:return 1;
default: return f2(n-1) + f2(n-2); }
19
}
void f3(int n)
{ printf(“%d”,n);
if(n/10 !=0) f3(n/10); }
void f4(int n)
{ if (n/10 !=0) f4(n/10); printf(“%d”, n); }
#include
{ printf(“%d\\n”,f1(4)); printf(“%d\\n”,f2(4)); f3(123);
printf(“\\n”); f4(123);
printf(“\\n”); }
(17) 程序运行时,第1行输出 (17) 。 A、10 B、24 C、6 D、1 (18) 程序运行时,第2行输出 (18) 。
A、1 B、3 C、2 D、4 (19) 程序运行时,第3行输出 (19) 。
A、123 B、3 C、321 D、1 (20) 程序运行时,第4行输出 (20) 。
A、1 B、123 C、3 D、321 试题6(每小题3分,共12分)
#include
struct num{ int a,b;};
void f(struct num s[], int n) { int index, j, k; struct num temp;
for(k=0;k< n-1;k++){ index=k;
for(j=k+1;j if(s[j].b main() { int count, i, k, m, n, no; struct num s[100],*p; scanf(“%d%d%d”, &n, &m, &k); for(i=0;i 20
相关推荐: