輸入我想寫這個程序,但我認爲我在邏輯上犯了一些錯誤。 This is my code。好的,所以我運行一個週期來收集所有的ODD數字,但我的最後一個數字是0或一些垃圾值。我對C++相當陌生,我花了更多時間在C上,我假設我沒有使用vector類,或者我的邏輯是垃圾。我花了很多時間,我無法弄清楚。我相信這是一個非常簡單的解決方案,但我看不出我做錯了什麼。謝謝你的時間!編寫一個程序,計算並輸出前N個奇數Fibonacci數字,用逗號和空格分隔。 N是從標準輸入
main()
{
int num; // how many odd numbers the user wants to see
int first = 0; // first fibonacci number
int second = 1; // second fibonacci number
int next = 0; // basically the sum of the previous two numbers
vector<int> holder; // a place to store the odd numbers
holder.push_back(second); // adding 1, otherwise we would miss it
cout << "How many ODD numbers would you like to see?:";
cin >> num; // taking user's input
int c, i;
for (i = 0, i < num; i++) {
next = first + second;
first = second;
second = next;
if ((next % 2) != 0) {
holder.push_back(next);
}
}
for (c = 0; c < num + 1; c++) {
cout << holder[c] << "," << " ";
}
return 0;
}
請不要張貼您的代碼的截圖,只需將其粘貼在您的文章! – hlt
我嘗試了幾次,但它得到所有搞砸了,我很抱歉:(。 –
@GiboGibonski檢查[這篇文章](https://meta.stackoverflow.com/a/251362/5610030),看看你怎麼可以正確地爲你的問題添加代碼 – Shogunivar