100 sets of grade II C operation questions in the national computer grade examination question bank (the 82nd set)

Set 82:

In the given program, the function of the function fun is to find out the integer in which the sum of the upper digits of all integers between 100 and 999 (including 100 and 999) is x (x is a positive integer), and then output it; The number of qualified integers is returned as the function value.
For example, when the x value is 5, the integers whose sum of the numbers on each bit between 100 and 999 is 5 include 104, 113, 122, 131, 140, 203, 212, 221, 230, 302, 311, 320, 401, 410 and 500. There are 15 in total. When the x value is 27, the integer with the sum of each number as 27 is 999. Only one. Please fill in the correct content in the underline of the program and delete the underline to make the program get the correct result.
Note: the source program is stored in blank1 under the examinee folder In C.
Do not add or delete lines, nor change the structure of the program!
Given source program:

#include <stdio.h> 
fun(int x) 
{ int n, s1, s2, s3, t; 
n=0; 
t=100; 
while(t<=__1__){ 
s1=t%10; s2=(__2__)%10; s3=t/100; 
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)); 
} 

Problem solving ideas:
First: use the while loop to find all integers between 100 and 999, so: 999 should be filled in.
The second place: s2 is to find ten digits, so it should be filled in: t/10.
The third place: the sum of all figures is x, so you should fill in: X.

Given program modi1 The function of function fun in C is to take the number on the even bit of long integer variable s from the low bit, form a new number in turn and put it in t. High is still high, low is still low.
For example, when the number in s is 7654321, the number in t is 642.
Please correct the mistakes in the program so that it can get the correct results.
Note: do not change the main function, do not add or delete lines, and do not change the structure of the program!
Given source program:

#include <stdio.h> 
void fun (long s, long t) 
{ long sl=10; 
s /= 10; 
*t = s % 10; 
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); 
} 

Problem solving ideas:
First: in the function fun body, t is a pointer variable, so the pointer should also be defined when defining the formal parameter.
Second: the condition should be s > 0, so it should be changed to: while (s > 0).

Students' records are composed of student number and grades. The data of N students have been put into the structure array s in the main function. Please write the function fun. Its function is to arrange students' records according to the level of scores, with high scores at the top.
Note: some source programs are in the file prog1 C file.
Do not change anything in the main function and other functions. Only fill in the curly brackets of the function fun with some statements you have written.
Given source program:

#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}, 
  {"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<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) ; 
} 

Problem solving ideas:
This question is arranged in descending order according to the score s in the structure, and the results are still stored in the current structure.
Reference answer:

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

Added by irandoct on Sat, 19 Feb 2022 08:57:09 +0200