Home > Back-end >  Using the function output Fibonacci Numbers within a specified range
Using the function output Fibonacci Numbers within a specified range

Time:11-05

Subject requires a simple function of Fibonacci Numbers to calculate, and another function, using the output of two positive integers m and n (0 & lt; M n 10000) or less or less between Fibonacci Numbers, all the so-called Fibonacci series is to satisfy any item Numbers are the first two and (the first two are defined as 1) sequence,
Function interface definition:
Int fib (int n);
M void PrintFN (int, int n);

Which function fib must return n Fibonacci Numbers; Function PrintFN to output in a row within a given range [m, n] all the Fibonacci Numbers, there is a space between adjacent Numbers, at the end of the line can not have surplus space, if not Fibonacci Numbers within a given interval, the output line "No Fibonacci number",
The referee sample testing program:
#include

Int fib (int n);
M void PrintFN (int, int n);

Int main ()
{
Int m, n, t;

The scanf (" % d % d % d ", & amp; M, & amp; N, & amp; T);
Printf (" fib (% d)=% d \ n ", t, fib (t));
PrintFN (m, n);

return 0;
}

/* * your code will be embedded in here/

Input the sample 1:
20 100 7

The output sample 1:
Fib (7)=13
21 and 55, 89

Enter the sample 2:
2000 2500 8

The output sample 2:
Fib (8)=21
No Fibonacci number


My code:
Int fib (int n) {
Int a=1;
Int b=1;
Int c=1;
If (n==1 | | n==2) {
C=1;
} else if (n & gt; 2) {
For (int I=3; I & lt;=n; I++) {
C=a + b;
A=b;
B=c.
}
}
Return the c;
}

Void PrintFN (int m, int n) {
Int a []={1};
Int flag=0;
int i=0;
int count=0;

While (fib (I + 1) & lt; 10000) {
A [I]=fib (I + 1);
i++;
The count=I;
}
For (int j=m; j <=n; J++) {
For (I=0; I & lt; The count. I++) {
If (j==a [I]) {
Printf (" % d ", j);
Flag=1;
}

}
}
If (flag==0) {
Printf (" No Fibonacci number ");
}
}

L want to please the great god teach why in DEVC++ can run normally, but No matter what the input range on zhejiang university PTA website is No Fibonacci number

CodePudding user response:

Dev a warning said iteration 1 c + + compiler will cause undefined behavior
  • Related