1: Use function to approximate cosine function (15 points)
This problem requires the realization of a function. Use the following formula to find the approximate value of cos(x) until the absolute value of the last term is less than e:
cos(x)=x0/0!−x2/2!+x4/4!−x6/6!+⋯
Function interface definition:
double funcos( double e, double x );
The parameters passed in by the user are the upper error limit e and the independent variable x; The funcos function shall return the approximate value of cos(x) calculated by the given formula and meeting the error requirements. The input and output are within the double precision range.
Example of referee test procedure:
#include <stdio.h> #include <math.h> double funcos( double e, double x ); int main() { double e, x; scanf("%lf %lf", &e, &x); printf("cos(%.2f) = %.6f\n", x, funcos(e, x)); return 0; } /* Your code will be embedded here */
Input example:
0.01 -3.14
Output example:
cos(-3.14) = -0.999899
My code:
double funcos (double e,double x){ int flag=1,i=0; double item=1,factor=1,result=0; while(fabs(item)>e){ if(i!=0) //If i=0, calculate directly factor=factor*i*(i-1);//The title requirement is 0!, 2!··· Therefore, i*(i-1) is used to find factorial without conventional cycle item=flag*pow(x,i)/factor; result=result+item; i+=2; flag=-flag; } return result; }
2: Use the function to output the completion within the specified range (20 points)
This problem requires to realize a simple function for calculating the sum of integer factors, and use it to realize another function to output all completions between two positive integers m and n (0 < m ≤ n ≤ 10000). The so-called perfect number is that the number is exactly equal to the sum of factors other than yourself. For example, 6 = 1 + 2 + 3, where 1, 2 and 3 are factors of 6.
Function interface definition:
int factorsum( int number ); void PrintPN( int m, int n );
The function factorsum must return the sum of the factors of int number; The PrintPN function outputs the given range [m line by line, n] The factorization formula of the factor accumulation form of each perfection in, each perfection occupies one line, and the format is "perfection = factor 1 + factor 2 +... + factor k", in which the perfections and factors are given in increasing order. If there is No perfect number in a given interval, a line of "No perfect number" is output.
Example of referee test procedure:
#include <stdio.h> int factorsum( int number ); void PrintPN( int m, int n ); int main() { int m, n; scanf("%d %d", &m, &n); if ( factorsum(m) == m ) printf("%d is a perfect number\n", m); if ( factorsum(n) == n ) printf("%d is a perfect number\n", n); PrintPN(m, n); return 0; } /* Your code will be embedded here */
Input example 1:
6 30
Output example 1:
6 is a perfect number 6 = 1 + 2 + 3 28 = 1 + 2 + 4 + 7 + 14
Input example 2:
7 25
Output example 2:
No perfect number
My code:
int factorsum(int number){ int i,sum=0; for(i=1;i<number;i++){//be careful!! Excluding itself if(number%i==0) //Factor number sum=sum+i; } return sum; } void PrintPN(int m,int n){ int sum,x,i,flag=0; for(x=m;x<=n;x++){ sum=factorsum(x);//Call the first function to get the factor sum if(sum==x){ flag=1; //It's perfect printf("%d = 1",x); for(i=2;i<x;i++){// Output factors one by one with a loop if(x%i==0) printf(" + %d",i); } printf("\n");//Wrap output next completion } } if(flag==0) printf("No perfect number"); }
3: Verify Goldbach conjecture with function (20 points)
This problem requires to realize a simple function to judge prime numbers, and use this function to verify Goldbach's conjecture: any even number not less than 6 can be expressed as the sum of two odd prime numbers. A prime number is a positive integer that can only be divided by 1 and itself. Note: 1 is not prime, 2 is prime.
Function interface definition:
int prime( int p ); void Goldbach( int n );
The function prime returns 1 when the parameter p passed in by the user is a prime number, otherwise it returns 0; The function Goldbach outputs the prime decomposition of N in the format "n=p+q", where p ≤ q are prime numbers. Because such decomposition is not unique (for example, 24 can be decomposed into 5 + 19 and 7 + 17), it is required to output the solution with the smallest P among all solutions.
Example of referee test procedure:
#include <stdio.h> #include <math.h> int prime( int p ); void Goldbach( int n ); int main() { int m, n, i, cnt; scanf("%d %d", &m, &n); if ( prime(m) != 0 ) printf("%d is a prime number\n", m); if ( m < 6 ) m = 6; if ( m%2 ) m++; cnt = 0; for( i=m; i<=n; i+=2 ) { Goldbach(i); cnt++; if ( cnt%5 ) printf(", "); else printf("\n"); } return 0; } /* Your code will be embedded here */
Input example:
89 100
Output example:
89 is a prime number 90=7+83, 92=3+89, 94=5+89, 96=7+89, 98=19+79 100=3+97,
My code:
int prime (int p){ int flag=1,i; if(p==1) return 0; if(p==2) return 1; for(i=2;i<=sqrt(p);i++){ if(p%i==0){ flag=0; break; } } if(flag==1) return 1; else return 0; } void Goldbach(int n){ int i; for(i=2;i<=n;i++){ //i=2, because 1 is not a prime, so start directly from 2 if(prime(i)){ //Using the cycle can ensure that it increases from small to large if(prime(n-i)){ printf("%d=%d+%d",n,i,n-i); break; } } } }
4: Using function to sum special a series (20 points)
Given two positive integers a and n that are not more than 9, it is required to write a function to find the sum of a+aa+aaa + +... + aa * a (n a).
Function interface definition:
int fn( int a, int n ); int SumA( int a, int n );
The function fn must return a number composed of n a; SumA returns the required sum.
Example of referee test procedure:
#include <stdio.h> int fn( int a, int n ); int SumA( int a, int n ); int main() { int a, n; scanf("%d %d", &a, &n); printf("fn(%d, %d) = %d\n", a, n, fn(a,n)); printf("s = %d\n", SumA(a,n)); return 0; } /* Your code will be embedded here */
Input example:
2 3
Output example:
fn(2, 3) = 222 s = 246
My code:
int fn(int a,int n){ int result=0,b,i; for(i=0;i<=n;i++){ b=a*pow(10,i-1); result=result+b; } return result; } int SumA(int a,int n){ int i,sum=0; for(i=0;i<=n;i++){ sum=sum+fn(a,i); } return sum; }
5: Use the function to output the Fibonacci number within the specified range (20 points)
This problem requires to realize a simple function for calculating Fibonacci number, and use it to realize another function to output all Fibonacci numbers between two positive integers m and n (0 < m ≤ n ≤ 10000). The so-called Fibonacci sequence is a sequence that satisfies that any number is the sum of the first two terms (the first two terms are defined as 1).
Function interface definition:
int fib( int n ); void PrintFN( int m, int n );
The function fib must return the number of Fibonacci of the nth item; The PrintFN function outputs the given range [m in one line, n] For all Fibonacci numbers in, there is a space between adjacent numbers, and there shall be no redundant space at the end of the line. If there is No Fibonacci number in a given interval, a line of "No Fibonacci number" is output.
Example of referee test procedure:
#include <stdio.h> int fib( int n ); void PrintFN( int m, int n ); int main() { int m, n, t; scanf("%d %d %d", &m, &n, &t); printf("fib(%d) = %d\n", t, fib(t)); PrintFN(m, n); return 0; } /* Your code will be embedded here */
Input example 1:
20 100 7
Output example 1:
fib(7) = 13 21 34 55 89
Input example 2:
2000 2500 8
Output example 2:
fib(8) = 21 No Fibonacci number
My code:
int fib( int n){ int n1,n2,n3,i; n1=1; n2=1; if(n==1||n==2){ return n1; } for(i=2;i<n;i++){ n3=n1+n2; n1=n2; n2=n3; } return n3; } void PrintFN( int m,int n){ int i,count=0,num; for(i=1;i<=n+m;i++){ //Make the value range of i larger? emmmm i don't know what to say num=fib(i); if(num>=m&&num<=n){ (count==0)?printf("%d",num):printf(" %d",num); //Pay attention to the format!! count++; } if(num>n) //Jump out of range break; } if(count==0) //count is 0, and no num value satisfies the condition printf("No Fibonacci number"); }