编写一个程序,计算并输出前 N 个奇数斐波那契数,以逗号和 space 分隔。 N 从标准输入输入
Write a program that calculates and outputs the first N odd Fibonacci numbers, separated by comma and space. N is entered from the standard input
我正在尝试编写这个程序,但我认为我在逻辑上犯了一些错误。 This is my code。好的,所以我 运行 用一个循环来收集所有奇数,但我的最后一个数字是 0 或一些垃圾值。我是 C++ 的新手,我在 C 上花了更多时间,我假设我没有正确使用向量 class 或者我的逻辑是垃圾。我花了很多时间,我就是想不通。我确信这是一个非常简单的解决方案,但我看不出我做错了什么。感谢您的宝贵时间!
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;
}
在打印值时用这个循环替换
for(c=0;c<num;c++){
cout << holder[c] << "," << " ";
}
#include <iostream>
#include <vector>
using namespace std;
int fibonacciRecursion(int n){
if(n==1||n==2){
return 1;
}
return fibonacciRecursion(n-1)+fibonacciRecursion(n-2);
}
main()
{
int num; // sum of how many numbers the user wants to see
int i,c; // loop variable
vector<int> holder;
cout << "how many odd fibonacci numbers do you want to see:";
cin >> num;
for (i=1; i <= num*2; i++){
if((fibonacciRecursion(i)%2)!=0){
holder.push_back(fibonacciRecursion(i));
}
}
for(c=0;c<num;c++){
cout<<holder[c] << "," << " ";
}
return 0;
}
我想我想出了一个解决方案,但我认为它不是很有效。所以我 运行 循环 num*2 次,似乎错误就在那里。因为我们只保存奇数,但我忘记了循环仍在进行,然后一些垃圾值位于向量的最后一个槽中。因此,通过 运行ning num*2 而不是仅仅 num 次,我有点向自己保证向量中不会有任何垃圾。尽管如此,我还是在向量中保存了更多项目,白白浪费了内存。它有效,但我认为它可能更有效率。很高兴看到你们的想法。
问题是这样的:第一个 for 循环运行 num
次,其长度不足以为 holder
找到 num
个条目。这意味着 holder.size() < num
。在你的第二个 for 循环中,你迭代甚至多于 num
个数,这意味着你在向量内进行非法访问,即使它有 num
个条目你也会这样做(因为 c
会变得和 num
一样大,这不是合法的索引)。由于 push_back 将矢量放大到 2 的势能,因此这些在技术上是有效的条目,但如您所见包含垃圾数据。
简单修复:
用 while(holder.size() < num){
交换 for (i = 0, i < num; i++) {
。 (并且在第二个循环中 num+1
和 num
)
此外,您可能想用 for(unsigned int c=0; c < holder.size(); c++){
交换 for (c = 0; c < num + 1; c++) {
。保持大小正确是 vector class 的职责,不应从外部完成,这两个循环是不同的操作。
顺便说一句,开始在尽可能小的范围内声明变量(例如循环中的 for 循环迭代器)并将程序划分为逻辑步骤,例如函数 vector<int> create_odd_fibonacci_numbers(const unsigned int amount);
和 void print_vector(const vector<int>& vec);
.使调试更容易。也许不是在这个小例子中,但你只是想习惯那样做。
我正在尝试编写这个程序,但我认为我在逻辑上犯了一些错误。 This is my code。好的,所以我 运行 用一个循环来收集所有奇数,但我的最后一个数字是 0 或一些垃圾值。我是 C++ 的新手,我在 C 上花了更多时间,我假设我没有正确使用向量 class 或者我的逻辑是垃圾。我花了很多时间,我就是想不通。我确信这是一个非常简单的解决方案,但我看不出我做错了什么。感谢您的宝贵时间!
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;
}
在打印值时用这个循环替换
for(c=0;c<num;c++){
cout << holder[c] << "," << " ";
}
#include <iostream>
#include <vector>
using namespace std;
int fibonacciRecursion(int n){
if(n==1||n==2){
return 1;
}
return fibonacciRecursion(n-1)+fibonacciRecursion(n-2);
}
main()
{
int num; // sum of how many numbers the user wants to see
int i,c; // loop variable
vector<int> holder;
cout << "how many odd fibonacci numbers do you want to see:";
cin >> num;
for (i=1; i <= num*2; i++){
if((fibonacciRecursion(i)%2)!=0){
holder.push_back(fibonacciRecursion(i));
}
}
for(c=0;c<num;c++){
cout<<holder[c] << "," << " ";
}
return 0;
}
我想我想出了一个解决方案,但我认为它不是很有效。所以我 运行 循环 num*2 次,似乎错误就在那里。因为我们只保存奇数,但我忘记了循环仍在进行,然后一些垃圾值位于向量的最后一个槽中。因此,通过 运行ning num*2 而不是仅仅 num 次,我有点向自己保证向量中不会有任何垃圾。尽管如此,我还是在向量中保存了更多项目,白白浪费了内存。它有效,但我认为它可能更有效率。很高兴看到你们的想法。
问题是这样的:第一个 for 循环运行 num
次,其长度不足以为 holder
找到 num
个条目。这意味着 holder.size() < num
。在你的第二个 for 循环中,你迭代甚至多于 num
个数,这意味着你在向量内进行非法访问,即使它有 num
个条目你也会这样做(因为 c
会变得和 num
一样大,这不是合法的索引)。由于 push_back 将矢量放大到 2 的势能,因此这些在技术上是有效的条目,但如您所见包含垃圾数据。
简单修复:
用 while(holder.size() < num){
交换 for (i = 0, i < num; i++) {
。 (并且在第二个循环中 num+1
和 num
)
此外,您可能想用 for(unsigned int c=0; c < holder.size(); c++){
交换 for (c = 0; c < num + 1; c++) {
。保持大小正确是 vector class 的职责,不应从外部完成,这两个循环是不同的操作。
顺便说一句,开始在尽可能小的范围内声明变量(例如循环中的 for 循环迭代器)并将程序划分为逻辑步骤,例如函数 vector<int> create_odd_fibonacci_numbers(const unsigned int amount);
和 void print_vector(const vector<int>& vec);
.使调试更容易。也许不是在这个小例子中,但你只是想习惯那样做。