1. In the following given programs, the function of function fun is to calculate and output the sum of the maximum 10 primes within max. high is passed from the main function to the fun function.
#include <stdio.h> #include <math.h> int fun( int high ) { int sum = 0, n=0, j, yes; /************found************/ while ((high >= 2) ___1___ (n < 10)) { yes = 1; for (j=2; j<=high/2; j++ ) if (high % j ==0 ){ /************found************/ yes=0; ___2___; } if (yes) { sum +=high; n++; } /************found************/ ___3___; } return sum ; } main ( ) { printf("%d\n", fun (100)); }
2. The character array xx input from the keyboard shall not exceed 80, and the characters shall be sorted from large to small, and the sorted results shall be stored in the string array xx.
#include <stdio.h> void main() { char xx[80] ; int numi,numj; char ch; printf("\n please input strings:"); scanf("%s",xx); for (numi=0;numi<strlen(xx);numi++) /***************found***************/ for (numj=___1___;numj<strlen(xx);numj++) if (xx[numi]<xx[numj]){ /***************found***************/ ___2___; xx[numi]=xx[numj]; /***************found***************/ ___3___;} printf("\n %s",xx); }
3. This problem requires writing a program to calculate the sequence 2 / 1 + 3 / 2 + 5 / 3 + 8 / 5 + Sum of the first N items of. Note that the sequence starts from item 2. The molecule of each item is the sum of the previous molecule and the denominator, and the denominator is the molecule of the previous item.
#include <conio.h> #include <stdio.h> double fun(int n) { int a,b,c,k; double s; s=0.0; a=2; /***************found***************/ ___1___; for (k=1;k<=n;k++) { s=s+(double)a/b; c=a; a=a+b;b=c; } /***************found***************/ ___2___; } main() { int n; /***************found***************/ printf("\n n="); scanf("%d",___3___); printf("\n the value of function is: %lf\n", fun(n)); }
3. Find the sum of the first n items of the following score sequence. The n value is input from the keyboard, and the value is returned to the main program output through the function value.
#include <conio.h> #include <stdio.h> double fun(int n) { int a,b,c,k; double s; s=0.0; a=2; /***************found***************/ ___1___; for (k=1;k<=n;k++) { s=s+(double)a/b; c=a; a=a+b;b=c; } /***************found***************/ ___2___; } main() { int n; /***************found***************/ printf("\n n="); scanf("%d",___3___); printf("\n the value of function is: %lf\n", fun(n)); }
4. In the given program, the function of function fun is: f(x)=1+x+x2 / +... + xn/n! Until | X / N|< 10-6, if x=2.5, the function value is 12.182494.
#include <stdio.h> #include <math.h> double fun(double x) { double f, t; int n; /**********found**********/ f = 1.0+___1___; t = x; n = 1; do { n++; /**********found**********/ t *= x/___2___; /**********found**********/ f += ___3___; } while (fabs(t) >= 1e-6); return f; } main() { double x, y; x=2.5; y = fun(x); printf("\nThe result is :\n"); printf("x=%-12.6f y=%-12.6f \n", x, y); }
5.
In the given program, the function of function fun is to find out all integers whose sum of numbers on each bit between 100 and x(x ≤ 999) is 15, and then output; The number of qualified integers is returned as the function value.
For example, when the value of n is 500, the integers whose sum of digits is 15 include 159, 168, 177, 186, 195, 249, 258, 267, 276, 285, 294, 339, 348, 357, 366, 375, 384, 393, 429, 438, 447, 456, 465, 474, 483, 492. There are 26.
Please fill in the correct content in the underline of the program and delete the underline. The program will 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!
#include <stdio.h> fun(int x) { int n, s1, s2, s3, t; /**********found**********/ n=__1__; t=100; /**********found**********/ while(t<=__2__) { s1=t%10; s2=(t/10)%10; s3=t/100; if(s1+s2+s3==15) { printf("%d ",t); n++; } /**********found**********/ __3__; } return n; } main() { int x=-1; while(x>999||x<0) { printf("Please input(0<x<=999): "); scanf("%d",&x); } printf("\nThe result is: %d\n",fun(x)); }
6. Ask K! (k < 13), the value of factorial is returned as the function value. For example, if k=10, 3628800 is output
#include <conio.h> #include <stdio.h> long fun ( int k) { /************found************/ if k > 0 return (k*fun(k-1)); /************found************/ else if ( k=0 ) return 1L; } NONO( ) {/* This function is used to open a file, input data, call a function, output data, and close a file. */ FILE *rf, *wf ; int i, k ; rf = fopen("gc02.in", "r") ; wf = fopen("gc02.out", "w") ; for(i = 0 ; i < 10 ; i++) { fscanf(rf, "%d,", &k) ; fprintf(wf, "%d!=%ld\n", k, fun ( k )) ; } fclose(rf) ; fclose(wf) ; } main() { int k = 10 ; printf("%d!=%ld\n", k, fun ( k )) ; NONO( ) ; }
7. Enter the values of x and y to find the lower 3-bit value of the Y power of the integer x. For example, the 6th power of integer 5 is 15625, and the lower 3 bits of this value are 625.
#include <stdio.h> long fun(int x,int y,long *p ) { int i; long t=1; /**************found**************/ for(i=1; i<y; i++) t=t*x; *p=t; /**************found**************/ t=t/1000; return t; } main() { long t,r; int x,y; printf("\nInput x and y: "); scanf("%ld,%ld",&x,&y); t=fun(x,y,&r); printf("\n\nx=%d, y=%d, r=%ld, last=%ld\n\n",x, y,r,t ); }
8. "Take out the numbers on the odd digits of the long integer variable s from the low order, form a new number in turn, and put it in T. for example, when the number in S is 7654321, the number in t is 7531."
#include <conio.h> #include <stdio.h> /************FOUND************/ void fun (long s, long t) { long sl=10; *t = s % 10; while ( s > 0) { s = s/100; *t = s%10 * sl + *t; /************FOUND************/ sl = sl*100; } } main() { long s, t; printf("\nPlease enter s:"); scanf("%ld", &s); fun(s, &t); printf("The result is: %ld\n", t); }
9. In the following given programs, the function fun realizes the following functions: count the number of digits with zero value in an unsigned integer, and pass it back to the main function through formal parameters; And the largest numeric value in each bit of the integer is returned as the function value. For example, if you enter an unsigned integer 30800, the number of numeric values with zero is 3, and the maximum numeric value on each bit is 8.
#include <stdio.h> int fun(unsigned n, int *zero) { int count=0,max=0,t; do { t=n%10; /**************FOUND**************/ if(t=0) count++; if(max<t) max=t; n=n/10; }while(n); /**************FOUND**************/ zero=count; return max; } main() { unsigned n; int zero,max; printf("\nInput n(unsigned): "); scanf("%d",&n); max = fun( n,&zero ); printf("\nThe result: max=%d zero=%d\n",max,zero); }
10. Find a prime number greater than the given integer m and immediately following M.
#include <conio.h> #include <stdio.h> int fun(int m) { int i, k ; for (i = m + 1 ; ; i++) { for (k = 2 ; k < i ; k++) /**************FOUND**************/ if (i % k != 0) break ; /**************FOUND**************/ if (k < i) return(i); } } NONO( ) {/* This function is used to open a file, input test data, call fun function, output data and close the file.*/ FILE *fp ; int i ; fp = fopen("gc06.out", "w") ; for(i = 1 ; i <= 10 ; i++) fprintf(fp, "%d\n", fun(888 + i * 88)) ; fclose(fp) ; } main( ) { int n ; printf("\nPlease enter n: " ) ; scanf("%d", &n ) ; printf("%d\n", fun(n)) ; NONO( ) ; }
11.
Write the function fun. The function is to calculate and output the sum of all factors of a given integer n (excluding 1 and itself). Specify that the value of n is not greater than 1000. For example, when the value of n is 855, 704 should be output.
#include <stdio.h> int fun(int n) { int sum=0,i; for(i=2;i<=n-1;i++) if(n%i==0) sum+=i; return sum; } NONO() {/* Please open the file in this function, input the test data, call the fun function, output the data, and close the file. */ FILE *rf, *wf ; int i, n, sum ; rf = fopen("bc.in", "r") ; wf = fopen("bc.out", "w") ; for(i = 0 ; i < 10 ; i++) { fscanf(rf, "%d", &n) ; sum = fun(n) ; fprintf(wf, "%d=%d\n", n, sum) ; } fclose(rf) ; fclose(wf) ; } main() { int n,sum; printf("Input n: "); scanf("%d",&n); sum=fun(n); printf("sum=%d\n",sum); NONO(); }
12. Please write the function fun. Its function is to calculate and output the sum of the square roots of all primes between 3 and n.
For example, if the main function inputs 100 to n from the keyboard, the output is sum = 148.874270.
Note: the value of n is required to be greater than 2 but not greater than 100. Some source programs are given as follows.
#include <math.h> #include <stdio.h> double fun(int n) { int i,j,k; double sum=0.0; for(i=3;i<=n;i++) {k=sqrt(i); for(j=2;j<=k;j++) if(i%j==0) break; if(j>=k+1) sum+=sqrt(i); } return sum; } NONO() {/* Please open the file in this function, input the test data, call the fun function, output the data, and close the file. */ FILE *rf, *wf ; int n, i ; double s ; rf = fopen("bc.in", "r") ; wf = fopen("bc.out", "w") ; for(i = 0 ; i < 10 ; i++) { fscanf(rf, "%d", &n) ; s = fun(n) ; fprintf(wf, "%lf\n", s) ; } fclose(rf) ; fclose(wf) ; } main() { int n; double sum; printf("\n\nInput n: "); scanf("%d",&n); sum=fun(n); printf("\n\nsum=%f\n\n",sum); NONO(); }
13. Design the program to find the smallest number greater than t in the Fibonacci sequence, and the result is returned by the function. The definition of Fibonacci sequence f (n) is: F (0) = 0, f (1) = 1; f (n) = f (N-1) + F (n-2), and calculate the results when t=1000 and t=3000 respectively.
#include <conio.h> #include <math.h> #include <stdio.h> int fun( int t) { } NONO ( ) {/* This function is used to open a file, input data, call a function, output data, and close a file. */ FILE *fp, *wf ; int i, n, s ; fp = fopen("bc06.in","r") ; if(fp == NULL) { printf("data file bc06.in non-existent!") ; return ; } wf = fopen("bc06.out","w") ; for(i = 0 ; i < 10 ; i++) { fscanf(fp, "%d", &n) ; s = fun(n) ; fprintf(wf, "%d\n", s) ; } fclose(fp) ; fclose(wf) ; } main() /* Main function */ { int n; n=1000; printf("n = %d, f = %d\n",n, fun(n)); NONO(); }
14.
If a number is exactly equal to the sum of all its factors (including 1 but not itself), it is called "perfect". For example, the factor of 6 is 1, 2, 3, and 1 + 2 + 3 = 6, so 6 is a "perfect number".
Calculate and output the sum of all "completion" within 1000.
#include <stdio.h> int fun() { } void NONO () {/* This function is used to open a file, input data, call a function, output data, and close a file. */ FILE *wf ; int a; wf = fopen("out.dat","w") ; a=fun() ; fprintf(wf, "2-1000 The sum of the completions between is:%d\n", a) ; } int main( ) { printf("2-1000 The sum of the completions between is:%d\n",fun()); NONO (); return 0; }
15. Write a function. When n is an even number, calculate 1 / 2 + 1 / 4 ++ 1/n; When the input n is an odd number, calculate 1 / 1 + 1 / 3 ++ 1/n. Call the function and output the result.