知ing

最新计算机二级C语言上机试题汇编100套

NCRE研究组 编 / 高等教育出版社

゛Zirro、Y 上传

查看本书

82套: 

给定程序中,函数fun的功能是:找出100999之间(含100999)所有整数中各位上数字之和为xx为一正整数)的整数,然后输出;符合条件的整数个数作为函数值返回。 

例如,当x值为5时,100999之间各位上数字之和为5的整数有:104113122131140203212221230302311320401410500。共有15个。当x值为27时,各位数字之和为27的整数是:999。只有1个。 

请在程序的下划线处填入正确的内容并把下划线删除使程序得出正确的结果。 

注意:源程序存放在考生文件夹下的BLANK1.C中。 

不得增行或删行,也不得更改程序的结构! 

给定源程序: 

#include <stdio.h> 

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)); 

解题思路: 

第一处:使用while循环找出100999之间所有整数,所以应填:999。 

第二处:s2是求十位数字,所以应填:t/10。 

第三处:各位数字之和为x,所以应填:x。 

*************************************************** 

给定程序MODI1.C中函数fun的功能是:从低位开始取出长整型变量s中偶数位上的数,依次构成一个新数放在t中。高位仍在高位,低位仍在低位。 

例如,当s中的数为:7654321时,t中的数为:642。 

请改正程序中的错误,使它能得出正确的结果。 

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 

给定源程序: 

#include <stdio.h> 

/ ************found************/ 

void fun (long s, long t) 

{ long sl=10; 

s /= 10; 

*t = s % 10; 

/ ************found************/ 

while ( s < 0) 

{ s = s/100; 

*t = s%10*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); 

解题思路: 

第一处:在函数fun体中,t是一个指针型变量,因此定义形参时也应定义指针。 

第二处:条件应该s>0,所以应改为:while(s>0)。 

*************************************************** 

学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s,请编写函数fun,它的功能是:按分数的高低排列学生的记录,高分在前。 

注意部分源程序在文件PROG1.C文件中。 

请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 

给定源程序: 

#include <stdio.h> 

#define N 16 

typedef struct 

{ char num[10]; 

int s; 

} STREC; 

int fun( STREC a[] ) 

main() 

{ STREC s[N]={{"GA005",85},{"GA003",76},{"GA002",69},{"GA004",85}, 

\TAB \TAB {"GA001",91},{"GA007",72},{"GA008",64},{"GA006",87}, 

\TAB \TAB {"GA015",85},{"GA013",91},{"GA012",64},{"GA014",91}, 

\TAB \TAB {"GA011",66},{"GA017",64},{"GA018",64},{"GA016",72}}; 

int i;FILE *out ; 

fun( s ); 

printf("The data after sorted :\n"); 

for(i=0;i<N; i++) 

{ if( (i)%4==0 )printf("\n"); 

printf("%s %4d ",s[i].num,s[i].s); 

printf("\n"); 

out = fopen("c:\\test\\out.dat","w") ; 

for(i=0;i<N; i++) 

{ if( (i)%4==0 && i) fprintf(out, "\n"); 

fprintf(out, "%4d ",s[i].s); 

fprintf(out,"\n"); 

fclose(out) ; 

解题思路: 

本题是按结构体中成绩s进行降序排列,其结果仍存入当前结构体中。 

参考答案: 

#include <stdio.h> 

#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; 

※※※※※※※※※※※※※※※※※※※※※※※※※ 


查看更多