C++ introductory training - Fibonacci sequence, circle area (with code)

Fibonacci series topic background

        The recursive formula of the Fibonacci sequence is: Fn=Fn-1+Fn-2, where F1=F2=1.

        When n is relatively large, Fn is also very large. Now we want to know what is the remainder of dividing Fn by 10007.

input and output format

input format

The input contains an integer n.

output format

        Output a line containing an integer representing the remainder of dividing Fn by 10007.

        Explanation: In this question, the answer is to ask for the remainder of dividing Fn by 10007, so we only need to be able to calculate the remainder, instead of calculating the exact value of Fn first, and then divide the calculated result by 10007 to get the remainder and calculate directly The remainder is often simpler than calculating the original number first and then taking the remainder.

Input and output samples

input sample

10

output sample

55

Reference Code

#include<stdio.h>
#define M 10007
int main()
{
	int a1,a2;
	a1=a2=1;
	int sum=0,temp;
	long n;
	long i;
	scanf("%ld",&n);
	
	for(i=1;i<=n;i++)
	{
		sum=a1%M;
		temp=a2;
		a2=(a1+a2)%M;
		a1=temp; 
	}
	printf("%d\n",sum);
	return 0;
} 

Inference from one fact to another - the area of ​​a circle

topic background

        Given the radius r of a circle, find the area of ​​the circle.

input and output format

input format

        The input contains an integer r, representing the radius of the circle.

output format

        Output a line containing a real number, rounded to 7 decimal places, representing the area of ​​the circle.

        Explanation: In this problem, the input is an integer, but the output is a real number.

        For the problem of real number output, please be sure to read the requirements for real number output. For example, in this question, it is required to keep 7 digits after the decimal point, then your program must strictly output 7 decimal places. It is not acceptable to output too many or too few decimal places. , will be considered wrong.

        If there are no special instructions on the real number output, the rounding will be done according to rounding.

Input and output samples

input sample

4

output sample

50.2654825

Instructions/Tips

1 <= r <= 10000。


Reference Code

#include<iostream>
#include<cmath>
#include<iomanip> //在C++中设置精度的时候,需要导入该头文件 
using namespace std;
int main()
{
    int r;
    double s, PI;
    cin >> r;
    PI = atan(1.0) * 4; //PI=3.14149265358979323
    s = PI * r * r;
    cout.setf(ios::fixed);//1
    cout << fixed << setprecision(7) << s << endl; //保留小数点后7位 
    return 0;
}

Guess you like

Origin blog.csdn.net/leyang0910/article/details/130667410