BUPT 2021 autumn calculation introduction eighth experiment

Posted by NoReason on Sat, 22 Jan 2022 10:46:18 +0100

BUPT 2021 autumn calculation introduction eighth experiment

Recursive evaluation one

Experiment 6_ 1_ Recursive evaluation I (100 points)
Existing function f(n), n is an integer greater than or equal to 0. f(n) = 0 when n is equal to 0, f(n)=f(n-1) +n when n is greater than 0
3

Note: this problem requires recursive solution, and global variables are not allowed, otherwise there is no score.

Function interface definition:
int fuc(int n) ;
Where n is the parameter passed in by the user. The value of N does not exceed 200. The return value of the function is the calculation result of the corresponding f(n).

Example of referee test procedure:
#include<stdio.h>

int fuc(int n) ;

int main()
{
int n ;

scanf("%d",&n); 
printf("%d\n",fuc(n));

return 0 ;

}

/*Please fill in the answer here/
/
Input example:
2
Output example:
9

#include<stdio.h>

int    fuc(int n) ;

int main()
{
    int n ;

    scanf("%d",&n); 
    printf("%d\n",fuc(n));

    return 0 ;
}
int fuc (int n )
{
	if(n == 0)
		{
			return 0;
		}
		else {
			return fuc(n-1) + n * n *n; 
		}
}

Recursive evaluation two

Experiment 6_ 2_ Recursive Evaluation II (100 points)
Existing sequence: s = a + a + 3 + a + 6 +... + a + 3 X n

Please write a recursive program for s.

The input has only one line, which is two positive integers separated by spaces, representing n (0 < n) and a (1 < a) respectively

The output also has only one line, which is the value of s in this case. (the test case ensures that all integers can be stored in int).

Note: this problem requires recursive solution, and global variables are not allowed. No score will be given in other ways.

Function interface definition:
int getSum(int n , int a) ;
Where n and a are parameters passed in by the user. The function must calculate the result.

Example of referee test procedure:

#include <stdio.h>

int getSum(int n , int a) ;

int main()
{
int n , a ;

   scanf( "%d%d" , &n , &a );       
   printf( "%d\n" , getSum( n , a ) );

return 0;

}

#include <stdio.h>

int getSum(int n , int a) ;

int main()
{
       int        n , a ; 

       scanf( "%d%d" , &n , &a );       
       printf( "%d\n" , getSum( n , a ) );

    return 0;
}
int getSum( int n , int a)
{
	if (n == 0)
	{
		return a;
	}
	else {
		return getSum ( n-1 , a) + a + 3*n;
		 }
}

/*Please fill in the answer here/
/
Input example:
1 2
Output example:
7

Recursive maximum

Experiment 6_ 3_ Recursive maximum (100 points)
Writing a function can read in n (0 < n < 100) integers and find the maximum value of these n integers.

This problem requires recursive solution, and global variables are not allowed. In all submissions of this question, as long as there is non recursive submission, no matter whether other submissions are recursive or not, this question will be given 0 point.

Function interface definition:
int findMax(int n) ;
Where n is the parameter passed in by the user. The value of n is greater than 0 and less than 100; The function must return the largest of the N integers read in.

Example of referee test procedure:
#include <stdio.h>

int findMax(int n) ;

int main()
{
int n ;

scanf("%d", &n); 
printf("%d\n" , findMax( n ) ) ; 

return 0;

}

/*Please fill in the answer here/
/
Input example:
6
15 30 34 10 89 5
Output example:
89

#include <stdio.h>

int findMax(int n) ;

int main()
{   
    int n ; 

    scanf("%d", &n); 
    printf("%d\n" , findMax( n ) ) ; 

    return 0;
}
/*int findMax ( int n )
{
	int num , max;
	if(n == 1)
		{
			scanf("%d", num);
			return num ;
		}
		else {
			scanf("%d", &num);
			if ( num > findMax(n -1))
				{
					return num ;
				}
				else {
					return findMax(n - 1);
				}
		}
}*/
int findMax(int n)
{
    int x,max;
    if(n==1)
    {
        scanf("%d",&x);
        return x;
    }
    else
    {
        scanf("%d",&x);
        max=findMax(n-1);
        return max>x?max:x;
    }
    return max;
}

I learned from other people's writing QAQ

Convert binary system to decimal system


Example of referee test procedure:
#include<stdio.h>

int convert(int n);

int main()
{
int n ;

scanf("%d",&n);

printf("%d\n",convert(n)) ;
return 0 ;    

}

/*Please fill in the answer here/
/
Input sample:
101010
Output example:
42

#include<stdio.h>

int convert(int n);

int main()
{
    int        n ;

    scanf("%d",&n);

    printf("%d\n",convert(n)) ;
    return 0 ;    
}
int convert ( int n )
{
	if( n == 1|| n== 0)
		{
			return n;
		}
		else {
			return convert(n / 10) * 2 + (n %10) ;
		}
}

Decomposition prime

Experiment 6_ 9_ Prime decomposition (100 points)
Design recursive function void void printfactor (int, int); Print the result of prime decomposition of n.

When void printFactor(60,1) is executed, the printing effect is:

60=223*5.

For a description of prime decomposition, see the handout.

The design program, knowing a data range [a,b], and a < = B, requires prime decomposition of each number. You can also design other auxiliary functions, such as isPrime(n), which judges prime numbers.

Input and output requirements: input two positive integers a and b, representing the decomposed interval, meeting 1 < = a < = b < = 100000, and b-A < = 100. Output b-a+1 line, that is, the decomposition of b-a+1 number.

Note: this problem requires recursive solution, and global variables are not allowed. No score will be given in other ways.

Function interface definition:
void printFactor( int, int );
The first parameter is the integer to be decomposed, and the second parameter needs to be designed by ourselves. Function has no return value.

Example of referee test procedure:
#include<stdio.h>

void printFactor( int, int );

int main()
{
int a,b,i ;

scanf( "%d%d", &a, &b );
for( i = a ; i <= b ; i++ )
    printFactor( i , 1 ) ;    

return 0;

}

/*Please fill in the answer here/
/
Input example:
100 105
Output example:
100=2255
101=101
102=2317
103=103
104=22213
105=357

#include<stdio.h>

void printFactor( int, int ); 

int main()
{
    int a,b,i ;

    scanf( "%d%d", &a, &b );
    for( i = a ; i <= b ; i++ )
        printFactor( i , 1 ) ;    

    return 0;
}
void printFactor( int a, int b ) 
{
	if(a ==1)
	{
		printf("%d=%d\n", a, a);
		return ;
	}
	if(b == 1)
	  {
	  	printf("%d=",a );
	  	b -- ;
	  }
	int i= 2;
	while ( a %i != 0)
		{
			i ++;
		}
	if((a/i)==1)
	{
		printf("%d\n", i);
		return ;
	}
	else {
         printf("%d*", i);
     	 printFactor(a/i, b);
        	}  
	
}

Reverse order output

Experiment 6_ 8_ Integer output in reverse order (100 points)
Please write a function that outputs an integer in reverse order.

Note: this problem requires recursive solution, and arrays and global variables are not allowed. If the requirements are violated, no points will be given.

Function interface definition:
void reverse(int n) ;
Where n is the parameter passed in by the user. The value of n is greater than 0 and does not exceed the range of int; The return value of the function is null, and its function is to output n in reverse order. The test case ensures that the N end of the input is not 0.

Example of referee test procedure:
#include <stdio.h>

void reverse(int n) ;

int main()
{
int n;

scanf("%d",&n);
reverse(n) ;
printf("\n");
return 0;

}

/*Please fill in the answer here/
/
Input example:
1234
Output example:
4321

#include <stdio.h>

void reverse(int n) ; 

int main()
{
    int     n;

    scanf("%d",&n);
    reverse(n) ;
    printf("\n");
    return 0;
}
void reverse(int n)
{
    printf("%d",n%10);
    if((n=n/10)!=0)
	{
        reverse(n);
    }
}

greatest common divisor

Experiment 6_ 7_ Maximum common divisor (100 points)
Design recursive function int GCD(int a,int b); Calculates the maximum common divisor of positive integers a and B and returns. For example, GCD(32,48) is 16.

GCD(a,b) is recursively defined as:

GCD(a,b)=GCD(b,a MOD b) when a MOD b ≠ 0

GCD(a,b)=b when a MOD b=0

Input and output requirements: input two positive integers a and b, and output the maximum common divisor of the two numbers, accounting for one line.

Note: this problem requires recursive solution, and global variables are not allowed. No score will be given in other ways.

Function interface definition:
int GCD(int a , int b );
Where a and b are parameters passed in by the user. The values of a and b do not exceed the range of int. The return value of the function is the maximum common divisor of a and b.

Example of referee test procedure:
#include<stdio.h>

int GCD(int a , int b );

int main()
{
int a , b ;

scanf("%d%d", &a , &b );
printf( "%d\n" , GCD( a, b ) ) ;

return 0 ;    

}

/*Please fill in the answer here/
/
Input example:
32 48
Output example:
16

#include<stdio.h>

int GCD(int a , int b );

int main()
{
    int        a , b ;

    scanf("%d%d", &a , &b );
    printf( "%d\n" , GCD( a, b ) ) ;

    return 0 ;    
}
int GCD(int a, int b )
{
	int temp ;
	if ( a<b)
	{
		temp = b;
		b= a;
		a= temp;
	 } 
	if (a % b == 0)
		{
			return b ;
			
		}
		else {
			return GCD ( b , a%b);
		}
}

Topics: C C++ Algorithm