Fibonacci的c++代码出现segmentation fault 5C

``````#include <iostream>
using namespace std;

long int Fibonacci (int N) {
if (N<=1){
return N;
}
else{
return Fibonacci(N-1)+Fibonacci(N-2);
}
}

int main  (int argc, char *argv[]) {
int input =atoi(argv[1]);
long int Result = Fibonacci(input);
cout<<"enter the Fibonacci number:"<<input<<" The result is:"<<Result<<endl;
}
``````

3个回答

``````#include<stdio.h>
#include<stdlib.h>

int main(int argc,char *argv[])
{
if(argc != 2)
{
printf("Usage:%s <int>\n",argv[0]);
exit(-1);
}
int input = atoi(argv[1]);
printf("Hellow World!\n");
return 0;
}
``````

``````// test-c11.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <stdint.h>
using namespace std;
class A
{
int a, b;
public:
A(int i, int j) :a(i), b(j) { cout << "constructor" << endl; }
A(A& a1)
{
a = a1.a;
b = a1.b;
cout << "copy constructor" << endl;
}
~A() { cout << "destructor" << endl; }
};

inline int Fibonacci(int N)
{
if (N <= 1) {
return N;
}
else {
return Fibonacci(N - 1) + Fibonacci(N - 2);
}
}

int64_t Fibonacci2(int64_t n)
{
if (n < 2)
return n;

int64_t n1 = 0;
int64_t n2 = 1;
int64_t index = 2;
int64_t nValue = 0;
while (index <= n) {
nValue = n1 + n2;
n1 = n2;
n2 = nValue;
++index;
}
return nValue;
}

int main(int argc, char *argv[])
{
int input = atoi(argv[1]);
int64_t Result = Fibonacci2(input);
cout << "enter the Fibonacci number:" << input << " The result is:" << Result << endl;
return 0;
}

``````

``````E:\Project\Sample\test-c11\Debug>test-c11.exe 111
enter the Fibonacci number:111 The result is:7515661444929089378
``````