16 day simulation

[Problem description] It is known that m and n are integers and satisfy the following two conditions: ① m, n∈{1,2,...,k}, that is, 1≤m, n≤k ②(n 2 -m*n -M 2) 2 =1 Your task is to program input a positive integer k (1≤k≤10 9 ), find a set of m and n that satisfy the above two conditions, and maximize the value of m 2 +n 2. For example, input k=1995 from the keyboard, then output: m=987 n=1597.
[Input sample] 1995
[Output sample] This
question m=987 n=1597 is rather mysterious. First of all, the most violent idea is simulation, but according to normal evaluation, simulation will explode in time, so some changes are made. First output all the possible values ​​of n and m, and you will find that these two are in Fibonacci increments, and n is one level faster than m, so it is easy to handle. According to the simulation, only the ones smaller than k The output of the penultimate and penultimate is just fine, just go to the code-
#include
using namespace std;
int main(){ int k; int a[10001] = {}; a[1] = 1; a[ 2] = 1; scanf("%d" ,&k); for(int i = 3;;i++){ a[i] = a[i-1] + a[i-2]; if(a[i ]> k){ printf(“m=%d\n” ,a[i-2]); printf(“n=%d\n” ,a[i-1]); return 0; } } }













Guess you like

Origin blog.csdn.net/Skywalker_____/article/details/112756605