程序填空
1
给定程序中,函数fun的功能是:找出100 ~ 999之间(含100和999) 所有整数中各位上数字之和为x (x为- -正整数) 的整数,然后输出;符合条件的整数个数
作为函数值返回。
例如,当x值为5时,100 ~ 999之间各位上数字之和为5的整数有: 104、113、 122、131、 140、 203、 212、 221、 230、 302、 311、 320、 401、 410、
500。共有15个。当x值为27时,各位数字之和为27的整数是: 999。 只有1个。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在考生文件夹下的BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
原始程序
#include <stdio.h>
int fun(int x)
{
int n, s1, s2, s3, t;
n=0;
t=100;
/**********found**********/
while(t<=__1__){
/**********found**********/
s1=t%10; s2=(__2__)%10; s3=t/100;
/**********found**********/
if(s1+s2+s3==__3__)
{
printf("%d ",t);
n++;
}
t++;
}
return n;
}
main()
{
int x=-1;
while(x<0)
{
printf("Please input(x>0): "); scanf("%d",&x); }
printf("\nThe result is: %d\n",fun(x));
}
正确代码
#include <stdio.h>
int fun(int x)
{
int n = 0, s1, s2, s3, t = 100;
while (t <= 999)
{
s1 = t % 10;
s2 = (t / 10) % 10;
s3 = t / 100;
if (s1 + s2 + s3 == x)
{
printf("%d ", t);
n++;
}
t++;
}
return n;
}
int main()
{
int x = -1;
while (x < 0)
{
printf("Please input(x>0): ");
scanf("%d", &x);
}
printf("\nThe result is: %d\n", fun(x));
return 0;
}
答案
1.999
2.t/10
3.x
2
程序通过定义学生结构体数组,存储了若干名学生的学号、姓名和3门]课的成绩。函数fun的功能是将存放学生数据的结构体数组,按照姓名的字典序(从小到
大)排序。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在考生文件夹下的BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
原始程序
#include <stdio.h>
#include <string.h>
struct student {
long sno;
char name[10];
float score[3];
};
void fun(struct student a[], int n)
{
/**********found**********/
__1__ t;
int i, j;
/**********found**********/
for (i=0; i<__2__; i++)
for (j=i+1; j<n; j++)
/**********found**********/
if (strcmp(__3__) > 0)
{
t = a[i]; a[i] = a[j]; a[j] = t; }
}
main()
{
struct student s[4]={
{
10001,"ZhangSan", 95, 80, 88},{
10002,"LiSi", 85, 70, 78},
{
10003,"CaoKai", 75, 60, 88}, {
10004,"FangFang", 90, 82, 87}};
int i, j;
printf("\n\nThe original data :\n\n");
for (j=0; j<4; j++)
{
printf("\nNo: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);
for (i=0; i<3; i++) printf("%6.2f ", s[j].score[i]);
printf("\n");
}
fun(s, 4);
printf("\n\nThe data after sorting :\n\n");
for (j=0; j<4; j++)
{
printf("\nNo: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);
for (i=0; i<3; i++) printf("%6.2f ", s[j].score[i]);
printf("\n");
}
}
正确代码
#include <stdio.h>
#include <string.h>
struct student {
long sno;
char name[10];
float score[3];
};
void fun(struct student a[], int n)
{
struct student t;
int i, j;
for (i=0; i<n-1; i++)
for (j=i+1; j<n; j++)
if (strcmp(a[i].name, a[j].name) > 0)
{
t = a[i]; a[i] = a[j]; a[j] = t; }
}
main()
{
struct student s[4]={
{
10001,"ZhangSan", 95, 80, 88},{
10002,"LiSi", 85, 70, 78},
{
10003,"CaoKai", 75, 60, 88}, {
10004,"FangFang", 90, 82, 87}};
int i, j;
printf("\n\nThe original data :\n\n");
for (j=0; j<4; j++)
{
printf("\nNo: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);
for (i=0; i<3; i++) printf("%6.2f ", s[j].score[i]);
printf("\n");
}
fun(s, 4);
printf("\n\nThe data after sorting :\n\n");
for (j=0; j<4; j++)
{
printf("\nNo: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);
for (i=0; i<3; i++) printf("%6.2f ", s[j].score[i]);
printf("\n");
}
}
答案
1.struct student
2.n-1
3.a[i].name,a[j].name
程序修改
1
给定程序MODI1.C中,读入一个整数k(2≤k<10000), 打印它的所有质因子(即所有为素数的因子)。例如,若输入整数: 2310, 则应输出:2、3、5、7、11.
请改正程序中的语法错误,使程序能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
原始程序
#include "conio.h"
#include "stdio.h"
/**************FOUND**************/
IsPrime (int n);
{
int i, m;
m=1;
for ( i=2; i<n; i++ )
/**************FOUND**************/
if !( n%i )
{
m=0; break; }
return(m);
}
main()
{
int j, k;
printf("\nPlease enter an integer number between 2 and 10000:" );
scanf ("%d",&k);
printf ( "\n\nThe prime factor(s) of %d is (are) :", k);
for( j=2; j<=k; j++)
if( ( !(k%j) )&& (IsPrime ( j ) ) )
printf ("\n %4d ", j );
printf("\n" );
}
正确代码
#include "conio.h"
#include "stdio.h"
/**************FOUND**************/
int IsPrime (int n)
{
int i, m;
m=1;
for ( i=2; i<n; i++ )
/**************FOUND**************/
if ( !( n%i ))
{
m=0; break; }
return(m);
}
main()
{
int j, k;
printf("\nPlease enter an integer number between 2 and 10000:" );
scanf ("%d",&k);
printf ( "\n\nThe prime factor(s) of %d is (are) :", k);
for( j=2; j<=k; j++)
if( ( !(k%j) )&& (IsPrime ( j ) ) )
printf ("\n %4d ", j );
printf("\n" );
}
2
给定程序MODI1.C中函数fun的功能是:应用递归算法求形参a的平方根。求平方根的迭代公式如下:
x1 = 1/2(x0+a/x0)
例如,a为2时,平方根值为: 1.414214。
请改正程序中的错误,使它能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
原始程序
#include <stdio.h>
#include <math.h>
/**********found**********/
double fun(double a, dounle x0)
{
double x1, y;
x1=(x0+ a/x0)/2.0;
/**********found**********/
if( fabs(x1-xo)>0.00001 )
y=fun(a,x1);
else y=x1;
return y;
}
main( )
{
double x;
printf("Enter x: "); scanf("%lf",&x);
printf("The square root of %lf is %lf\n",x,fun(x,1.0));
}
正确代码
#include <stdio.h>
#include <math.h>
double fun(double a, double x0)
{
double x1, y;
x1 = (x0 + a / x0) / 2.0;
if (fabs(x1 - x0) > 0.00001)
y = fun(a, x1);
else
y = x1;
return y;
}
int main()
{
double x;
printf("Enter x: ");
scanf("%lf", &x);
printf("The square root of %lf is %lf\n", x, fun(x, 1.0));
return 0;
}
修改说明
第一处是函数定义中的参数类型拼写错误,应该是“double”,而不是“dounle”。
第二处是递归终止条件的判断错误,应该是判断“fabs(x1-x0)>0.00001”,而不是“fabs(x1-xo)>0.00001”
程序设计
1
程序定义了NxN的二维数组,并在主函数中自动赋值。请编写函数fun( int a[][N]),函数的功能是:使数组左下三角元素中的值全部置成0。
注意:部分源程序存在文件PROG1.C文件中。
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
原始程序
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define N 5
int fun ( int a[][N] )
/*不得改动此注释文字及位置,begein*/
{
}
/*不得改动此注释文字及位置,end*/
NONO( )
{
FILE *rf, *wf ;
int i, j, a[5][5] ;
rf = fopen("bc04.in", "r") ;
wf = fopen("bc04.out", "w") ;
for(i = 0 ; i < 5 ; i++)
for(j = 0 ; j < 5 ; j++)
fscanf(rf, "%d ", &a[i][j]) ;
fun(a) ;
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 5; j++ ) fprintf(wf, "%4d", a[i][j] );
fprintf(wf, "\n");
}
fclose(rf) ;
fclose(wf) ;
}
main ( )
{
int a[N][N], i, j;
printf("***** The array *****\n");
for ( i =0; i<N; i++ )
{
for ( j =0; j<N; j++ )
{
a[i][j] = rand()%10; printf( "%4d", a[i][j] ); }
printf("\n");
}
fun ( a );
printf ("THE RESULT\n");
for ( i =0; i<N; i++ )
{
for ( j =0; j<N; j++ ) printf( "%4d", a[i][j] );
printf("\n");
}
NONO( );
}
补全代码
int fun ( int a[][N] )
/*不得改动此注释文字及位置,begein*/
{
int i, j;
for(i = 0; i < N; i++) {
for(j = 0; j <= i; j++) {
a[i][j] = 0;
}
}
}
/*不得改动此注释文字及位置,end*/
2
请编一个函数double Pdt(int n,double pp[),它的功能是: 求出数组pp中n个数的整数部分的和,并返回此值。
例如:若输入4和11.91、 23.87、 35.79、 40.83, 则输出109.0, 整数部分的值应小于10的1 6次方。
注意:此程序存贮在PROG1.C中。
请勿改动主程序main、函数WriteData和函数compute中的任何内容,仅在函数Pdt的花括号中填入你编写的若干语句。
原始程序
#include <conio.h>
#include <stdio.h>
#include <math.h>
#define M 20
double Pdt( int n, double pp[] )
/*不得改动此注释文字及位置,begein*/
{
}
/*不得改动此注释文字及位置,end*/
/* 以下部分与考生答题无关, 考生不必阅读, 但不得进行任何修改 */
WriteData(double sum)
{
FILE *fp;
fp = fopen("dat62.dat", "w") ;
fprintf(fp, "%lf", sum);
fclose(fp);
}
compute( )
{
int i, n;
double pp[M];
FILE *fp;
fp = fopen("c9670603.in", "r");
fscanf(fp, "%d", &n );
for( i = 0; i < n; i++ )
fscanf(fp, "%lf", &pp[i] );
fclose(fp);
WriteData(Pdt(n, pp));
}
main( )
{
int i, m;
double tt[M];
printf("\nPlease enter number of numbers: ");
scanf("%d", &m );
printf("\nPlease enter %d decimal numbers: ", m);
for( i = 0; i < m; i++ )
scanf("%lf", &tt[i] );
printf( "\nThe product of their integer part is: %lf.", Pdt(m, tt) );
compute();
}
补全代码
double Pdt(int n, double pp[]) {
double sum = 0;
for (int i = 0; i < n; i++) {
sum += floor(pp[i]); // 求整数部分并累加
}
return sum;
}
3
函数fun的功能是:将s所指字符串中ASCII值为奇数的字符删除,串中剩余字符形成-个新串放在t所指的数组中。
例如,若s所指字符串中的内容为: “ABCDEFG12345”, 其中字符A的ASCI码值为奇数、… 字符1的ASCII码值也为奇数、…都应当删除,其它依此类推。最
后t所指的数组中的内容应是: “BDF24”。
注意:部分源程序存在文件PROG1.C中。
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
原始程序
#include <stdio.h>
#include <string.h>
void fun(char *s, char t[])
/*不得改动此注释文字及位置,begein*/
{
}
/*不得改动此注释文字及位置,end*/
main()
{
char s[100], t[100];void NONO ();
printf("\nPlease enter string S:"); scanf("%s", s);
fun(s, t);
printf("\nThe result is: %s\n", t);
NONO();
}
void NONO ()
{
char s[100], t[100] ;
FILE *rf, *wf ;
int i ;
rf = fopen("in.dat","r") ;
wf = fopen("out.dat","w") ;
for(i = 0 ; i < 10 ; i++) {
fscanf(rf, "%s", s) ;
fun(s, t) ;
fprintf(wf, "%s\n", t) ;
}
fclose(rf) ;
fclose(wf) ;
}
补全代码
void fun(char *s, char t[]) {
int i, j = 0;
for (i = 0; s[i] != '\0'; i++) {
if (s[i] % 2 == 0) {
// 如果是偶数,将其加入新串t中
t[j++] = s[i];
}
}
t[j] = '\0'; // 新串t以'\0'结尾
}
4
函数fun的功能是:将n个人员的考试成绩进行分段统计,考试成绩放在a数组中,各分数段的人数存到b数组中:成绩为60到69的人数存到b[0]中,成绩为70
到79的人数存到b[1],成绩为80到89的人数存到b[2],成绩为90到99的人数存到b[3], 成绩为100的人数存到b[4],成绩为60分以下的人数存到b[5]中。
例如,当a数组中的数据是:
93、 85、 77、 68、 59、 43、 94、 75、 98。
调用该函数后,b数组中存放的数据应是:
1、2、1、3、0、2。
注意:部分源程序存在文件PROG1.C中。
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
原始程序
#include <conio.h>
#include <stdio.h>
void fun(int a[], int b[], int n)
{
}
NONO( )
{
/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *rf, *wf ;
int a[100], b[6], i, j ;
rf = fopen("in1.dat", "r") ;
wf = fopen("bc01.dat","w") ;
for(i = 0 ; i < 10 ; i++) {
for(j = 0 ; j < 10 ; j++) fscanf(rf, "%d,", &a[j]) ;
fun(a, b, 9) ;
for(j = 0 ; j < 6 ; j++) fprintf(wf, "%d ", b[j]) ;
fprintf(wf, "\n") ;
}
fclose(rf) ;
fclose(wf) ;
}
main()
{
int i, a[100]={
93, 85, 77, 68, 59, 43, 94, 75, 98}, b[6];
fun(a, b, 9);
printf("the result is: ");
for (i=0; i<6; i++) printf("%d ", b[i]);
printf("\n");
NONO();
}
补全代码
void fun(int a[], int b[], int n)
{
int i;
for(i = 0; i < 6; i++) b[i] = 0; // 初始化b数组
for(i = 0; i <= n; i++) {
if(a[i] < 60) b[5]++; // 不及格
else if(a[i] < 70) b[0]++; // 60-69
else if(a[i] < 80) b[1]++; // 70-79
else if(a[i] < 90) b[2]++; // 80-89
else if(a[i] < 100) b[3]++; // 90-99
else b[4]++; // 100
}
}
5
编写函数fun,它的功能是计算下列级数和,和值由函数值返回。
S = 1 + x + x^2/2! + x^3/3! + x^4/4! + x^n/n!
例如,当n=10, x= 0.3时,函数值为1.349859。
注意:部分源程序在文件PROG1.C文件中。
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
原始程序
#include <stdio.h>
#include <math.h>
double fun(double x , int n)
/*不得改动此注释文字及位置,begein*/
{
}
/*不得改动此注释文字及位置,end*/
main()
{
void NONO ();
printf("%f\n", fun(0.3,10));
NONO();
}
void NONO ()
{
FILE *fp, *wf ;
int i, n ;
double s, x ;
fp = fopen("in.dat","r") ;
wf = fopen("out.dat","w") ;
for(i = 0 ; i < 10 ; i++) {
fscanf(fp, "%lf,%d", &x, &n) ;
s = fun(x, n) ;
fprintf(wf, "%f\n", s) ;
}
fclose(fp) ;
fclose(wf) ;
}
补全代码
double fun(double x, int n)
{
double sum = 1.0; // 初始化和为1
double term = 1.0; // 初始化每一项的值为1
int i;
for (i = 1; i <= n; i++) {
term *= x / i; // 计算每一项的值
sum += term; // 将每一项的值加入和中
}
return sum; // 返回和的值
}