我正在嘗試洗掉斐波那契數列中的最后一個 0
因為我正在洗掉 return 0; 最后一個值顯示垃圾值
類似于 735150
我應該編輯什么以獲得所需的輸出
0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
因為我得到了輸出
0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 0
#include <iostream>
using namespace std;
class fibo
{
private:
unsigned long int n1,n2,final;
public:
fibo()
{
n1 = 0;
n2 = 1;
final = n1 n2;
}
fibo(int x1,int x2) //Parameterised Constructor
{
n1 = x1;
n2 = x2;
for (int x = 0;x<=8; x )
{
final = n1 n2;
cout << final << " ";
n1 = n2;
n2 = final;
}
}
int calc()
{
for (int x = 0;x<=8; x )
{
final = n1 n2;
cout << final << " ";
n1 = n2;
n2 = final;
}
return 0;
}
fibo(fibo &i); // Copy Constructor
};
fibo::fibo(fibo &i)
{
n1= i.n1;
n2 = i.n2;
final = i.final;
}
int main()
{
cout << "0 " ;
fibo f1(0,1);
fibo f2 = f1;
cout << f2.calc() << endl;
return 0;
}
uj5u.com熱心網友回復:
我非常清楚這不是代碼審查,而是 OP 要求提供一個更簡單的版本。
這不符合奇數復制建構式的要求,而是表明列印斐波那契數可以在幾行 (8) 行代碼中處理。
#include <iostream>
#include <vector>
#include <algorithm>
// This is a poor time to use a classes and objects,
// This is simply a straightforward algorithm.
// calculation is not an object
// As Nathan says, it is a bizarre design
// Generate numbers
std::vector<int> fibonacci_numbers(int N){
auto n0 = 0; // not used in output
auto n1 = 1; // first value in output
auto next_fib = [&n0,&n1](){
auto value = n1;
auto next = n0 n1;
n0 = n1;
n1 = next;
return value;
};
auto result = std::vector<int>(N);
std::generate(result.begin(), result.end(), next_fib);
return result;
}
// Print
void print_fibo(int N){
int n0 = 0; // not for printing
int n1 = 1;
while(N--){
auto next_fib = n0 n1;
std::cout << n1 << ", ";
n0 = n1;
n1 = next_fib;
}
}
int main() {
// seems like you want 19 numbers
// generating
auto fibs = fibonacci_numbers(9);
for(auto e:fibs){
std::cout << e << ", ";
}
// just printing
std::cout << '\n';
print_fibo(19);
std::cout << '\n';
return 0;
}
uj5u.com熱心網友回復:
我做了一些更改,如下面的代碼所示。首先,您不需要創建物件 f1 的副本。直接呼叫即可f1.calc()。Secondcalc的回傳型別更改為 void。第三,使用建構式初始化串列。您可以在此處查看作業程式。
#include <iostream>
using namespace std;
class fibo
{
private:
unsigned long int n1,n2,final;
public:
//use constructor initializer list
fibo(): n1(0), n2(1), final(n1 n2)
{
//n1 = 0;
//n2 = 1;
//final = n1 n2;
}
fibo(int x1,int x2) //Parameterised Constructor
{
n1 = x1;
n2 = x2;
for (int x = 0;x<=8; x )
{
final = n1 n2;
cout << final << " ";
n1 = n2;
n2 = final;
}
}
void calc()
{
for (int x = 0;x<=8; x )
{
final = n1 n2;
cout << final << " ";
n1 = n2;
n2 = final;
}
//no need to return
//return 0;
}
fibo(fibo &i); // Copy Constructor
};
fibo::fibo(fibo &i)
{
n1= i.n1;
n2 = i.n2;
final = i.final;
}
int main()
{
cout << "0 1 " ;
fibo f1(0,1);
//no need to create new object
//fibo f2 = f1;
//call f1.calc() instead of f2.calc()
f1.calc();
return 0;
}
上面程式的輸出是:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
請注意,還有其他方法可以實作這一點。您可以在此處查看這些內容。
此外,在給定的示例代碼輸出,開始3個數字應該是0 1 1而不是0 1 2。您可以cout << "0 ";到cout << "0 1 ";得到正確的輸出0 1 1,因為我在我的編輯一樣。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321245.html
標籤:C
上一篇:最大資料包數
下一篇:如何創建多執行緒清潔器?
