例如,当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
int fun(int x)
{int n, s1, s2, s3, t;
n=0;
t=100;
/**********found**********/
while(t<=__1__){
/**********found**********/
s1=t; s2=(__2__); 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));
}
解题答案:
/**********第一空**********/
while(t<=999){
/**********第二空**********/
s1=t; s2=(t/10); s3=t/100;
/**********第三空**********/
if(s1+s2+s3==x)
****************************************** [NextPage] 二、改错题:给定程序MODI1.C中函数fun的功能是:从低位开始取出长整型变量s中偶数位上的数,依次构成一个新数放在t中。高位仍在高位,低位仍在低位。
例如,当s中的数为:7654321时,t中的数为:642。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
/************found************/
void fun (long s, long t)
{long sl=10;
s /= 10;
*t = s % 10;
/************found************/
while (s < 0)
{s = s/100;
*t = s*sl + *t;
sl = sl * 10;
}
}
main()
{long s, t;
printf("\nPlease enter s:"); scanf("%ld", &s);
fun(s, &t);
printf("The result is: %ld\n", t);
}
解题答案:
#include
/************found************/
void fun(long s, long *t)
/************found************/
while(s>0)
******************************************
[NextPage] 三、程序题:学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组 s中,请编写函数fun,它的功能是:按分数的高低排列学生的记录,高分在前。
注意: 部分源程序在文件PROG1.C文件中。请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
给定源程序:
#include
#define N 16
typedef struct
{char num[10];
int s;
} STREC;
void fun(STREC a[])
{
}
main()
{STREC s[N]={{"GA005",85},{"GA003",76}, {"GA002",69},{"GA004",85},{"GA001",91},{"GA007",72},{"GA008",64},{"GA006",87},{"GA015",85},{"GA013",91},{"GA012",64},{"GA014",91},{"GA011",66},{"GA017",64},{"GA018",64},{"GA016",72}};
int i;FILE *out ;
fun(s);
printf("The data after sorted :\n");
for(i=0;i
{if((i)%4==0)printf("\n");
printf("%s M ",s[i].num,s[i].s);
}
printf("\n");
out = fopen("out.dat","w");
for(i=0;i
{if((i)%4==0 && i) fprintf(out, "\n");
fprintf(out, "M ",s[i].s);
}
fprintf(out,"\n");
fclose(out);
}
参考答案:
#include
#define N 16
typedef struct
{ char num[10];
int s;
} STREC;
int fun( STREC a[] )
{
STREC tmp;
int i,j;
for(i = 0; i < N; i++)
for(j = i+1; j < N; j++)
if(a[i].s < a[j].s) {
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|