1单选(1分)
以下程序有语法错误,有关错误原因的正确说法是
#include <stdio.h>
void prt_char(float x);
int main()
{
int G=5,k;
......
k=prt_char(G);
......
return 0;
}
int prt_char(int x)
{ ...... }
-
A.函数prt_char(int x)定义的位置不对,不能放在主函数main()的后面。
-
B.变量名不能使用大写字母
-
C.函数原型和函数定义不匹配
-
D.函数名不能使用下划线
-
答案:C
2单选(1分)
设计一个函数MinCommonMultiple(),计算两个正整数的最小公倍数。
代码如下,按要求在空白处填写适当的表达式或语句,使程序完整并符合题目要求。
#include <stdio.h>
int MinCommonMultiple(int a, int b);
int main()
{
int a, b, x;
printf("Input a,b:");
scanf("%d,%d", &a, &b);
x = _________________;
if (__________) printf("MinCommonMultiple = %d\n", x); else printf("Input error!\n");
return 0; } //函数功能:计算两个正整数的最小公倍数,-1表示没有最小公倍数 int MinCommonMultiple(int a, int b)
{
int i;
if (_________________) return -1; // 保证输入的参数为正整数 for (i=1; i<b; i++)
{ if (________________) return i * a; } return b * a; }
-
A.第8行: MinCommonMultiple
第10行: x == -1
第22行: a<=0 && b<=0
第26行: (i * a) % b == 0
-
B.第8行: MinCommonMultiple(a, b)
第10行: x != -1
第22行: a<=0 || b<=0
第26行: (i * a) % b == 0
-
C.第8行: MinCommonMultiple(int a, int b)
第10行: x = -1
第22行: a<=0 || b<=0
第26行: (i * a) % b == 0
-
D.第8行: MinCommonMultiple(b, a)
第10行: x != -1
第22行: a<=0 || b<=0
第26行: (i * a) / b == 0
-
答案:B
3单选(1分)
设计一个函数MaxCommonFactor(),利用欧几里德算法(也称辗转相除法)计算两个正整数的最大公约数。
代码如下,按要求在空白处填写适当的表达式或语句,使程序完整并符合题目要求。
#include <stdio.h>
int MaxCommonFactor(int a, int b);
int main()
{ int a, b, x;
printf("Input a,b:");
scanf("%d,%d", &a, &b);
x =_______________ ;
if (x != -1)
{ printf("MaxCommonFactor = %d\n", x);}
else { printf("Input error!\n");} return 0; } //函数功能: 计算两个正整数的最大公约数,-1表示没有最大公约数
int MaxCommonFactor(int a, int b)
{ int r;
if (a<=0 || b<=0) return -1; // 保证输入的参数为正整数 do{ ____________;
a = b;
_____________;
}while (__________);
return a; }
-
A.第8行: MaxCommonFactor(x, b)
第29行: r = a / b
第31行: a = r
第32行: r != 0
-
B.第8行: MaxCommonFactor(a, x)
第29行: r = b % a
第31行: b = r
第32行: r = 0
-
C.第8行: MaxCommonFactor(a, b)
第29行: r = a % b
第31行: b = r
第32行: r != 0
-
D.第8行: MaxCommonFactor(a, b, x)
第29行: r = a % b
第31行: a = r
第32行: r == 0
-
答案:C
4单选(1分)
完全数,又称完美数或完数(Perfect Number),它是指这样的一些特殊的自然数,它所有的真因子(即除了自身以外的约数)的和,恰好等于它本身。例如,6就是一个完全数,是因为6 = 1 + 2 + 3。请编写一个判断完全数的函数IsPerfect(),然后判断从键盘输入的整数是否是完全数。注意:1没有真因子,所以不是完全数。
代码如下,按要求在空白处填写适当的表达式或语句,使程序完整并符合题目要求。
#include <stdio.h>
#include <math.h>
int IsPerfect(int x);
int main()
{ int m;
printf("Input m:");
scanf("%d", &m);
if (_________________) /* 完全数判定 */ printf("%d is a perfect number\n", m);
else printf("%d is not a perfect number\n", m); return 0;
} /* 函数功能:判断完全数,若函数返回0,则代表不是完全数,若返回1,则代表是完全数 */ int IsPerfect(int x)
{ int i;
int total = 0; /* 1没有真因子,不是完全数 */ for (__________________)
{ if (___________) total = total + i; } return total==x ? 1 : 0;
}
-
A.第10行: IsPerfect(m)!=1
第24行: i=0; i<=x; i++
第26行: x / i == 0
-
B.第10行: IsPerfect(m)==0
第24行: i=0; i<x; i++
第26行: x % i != 0
-
C.第10行: m
第24行: i=1; i<=x; i++
第26行: x % i != 0
-
D.第10行: IsPerfect(m)
第24行: i=1; i<x; i++
第26行: x % i == 0
-
答案:D
5单选(1分)
以下程序执行后输出结果是
#include <stdio.h>
int MaxValue(int x, int y)
{ return x>y? x:y;
}
int MinValue(int x,int y)
{ return x>y? y:x;
}
int main()
{ int a=4,b=3,c=5,d,e,f;
d=MaxValue(a,b);
d=MaxValue(d,c);
、 e=MinValue(a,b);
e=MinValue(e,c);
f=a+b+c-d-e;
printf("%d,%d,%d\n",d,f,e);
return 0; }
-
A. 5,3,4
-
B.5,4,3
-
C.3,4,5
-
D.3,5,4
-
答案:B