是否有一种方法可以在A'中获得fibonacci的递归和迭代功能。环形?
我有这些代码,它们都在工作,除#1)fibonacci案例外。我几乎拥有所有语法的所有内容,但是输出仅给我提供了Main.cpp文件的几行。如果我删除fibonacci部分(情况#1),其他3个案例运行良好。
main.cpp:
#include <iostream>
#include <chrono>
#include "rec_fun.h"
using namespace std;
using namespace std::chrono;
int main()
{
cout<<"#1)"<<endl;
int n1 = 1;
cout<<"Fibonacci"<<endl;
printf("%5c %10s %10s %10s %10s", 'n', "Recursive", "time_r(s)", "Iterative", "time_i(s)");
int i;
savitch2::recursion recur;
for (i = 1; i < 50; i++)
{
high_resolution_clock::time_point startTime = high_resolution_clock::now();
int fibR = recur.fib_recursion(i);
high_resolution_clock::time_point endTime = high_resolution_clock::now();
std::cout << fixed;
duration<float> time_span = endTime - startTime;
high_resolution_clock::time_point startTime2 = high_resolution_clock::now();
int fibI = recur.fib_iterative(i);
high_resolution_clock::time_point endTime2 = high_resolution_clock::now();
std::cout << fixed;
duration<float> time_span2 = endTime2 - startTime2;
printf("%5d" "%10d" "%10f" "%10d" "%10f", i, fibR, time_span.count(),fibI, time_span2.count());
//recur.fib_recursion(i);// Replace with call to your function.
//recur.fib_iterative(i);
}
//std::cout << fixed;
//std::cout << "It took " << time_span.count() << " seconds.";
//std::cout << std::endl;
}
rec_fun.cpp:
#include<iostream>
#include <chrono>
#include "rec_fun.h"
#include <string>
using namespace savitch2;
using namespace std;
using namespace std::chrono;
int recursion::fib_iterative(int n) {
if(n == 1 || n == 2)
return 1;
int A[2][2] = { { 1, 1 },{ 1, 0 } };
int B[2][2] = { { 1, 1 },{ 1, 0 } };
int temp[2][2];
while (n >= 2) {
for (int i = 0; i < 2; i++)
for (int k = 0; k < 2; k++) {
temp[i][k] = 0;
for (int j = 0; j < 2; j++)
temp[i][k] += A[i][j] * B[j][k];
}
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
B[i][j] = temp[i][j];
n--;
}
return B[0][1];
}
int recursion::fib_recursion(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fib_recursion(n-1) + fib_recursion(n-2);
}
rec_fun.h:
#include<iostream>
#include <string>
using namespace std;
namespace savitch2
{
class recursion
{
public:
int fib_iterative(int n);
int fib_recursion(int n);
private:
int ones;
int tens;
string s;
};
}
fibonacci的输出应该是:
Fibonacci
n Recursive time_r(s) Iterative time_i(s)
1 1 0.000000 1 0.000000
2 1 0.000000 1 0.000000
3 2 0.000000 2 0.000000
4 3 0.000000 3 0.000001
. . .
43 433494437 2.015535 433494437 0.000002
44 701408733 3.270357 701408733 0.000002
45 1134903170 5.258732 1134903170 0.000002
输出我要得到:
#1)
Fibonacci
I have these pieces of codes and they are all working with the exception of #1) the Fibonacci case. I pretty much have everything syntax-wise, but the output is giving me just the few lines from the main.cpp file. If I remove the Fibonacci section (case #1), the other 3 cases run just fine.
main.cpp:
#include <iostream>
#include <chrono>
#include "rec_fun.h"
using namespace std;
using namespace std::chrono;
int main()
{
cout<<"#1)"<<endl;
int n1 = 1;
cout<<"Fibonacci"<<endl;
printf("%5c %10s %10s %10s %10s", 'n', "Recursive", "time_r(s)", "Iterative", "time_i(s)");
int i;
savitch2::recursion recur;
for (i = 1; i < 50; i++)
{
high_resolution_clock::time_point startTime = high_resolution_clock::now();
int fibR = recur.fib_recursion(i);
high_resolution_clock::time_point endTime = high_resolution_clock::now();
std::cout << fixed;
duration<float> time_span = endTime - startTime;
high_resolution_clock::time_point startTime2 = high_resolution_clock::now();
int fibI = recur.fib_iterative(i);
high_resolution_clock::time_point endTime2 = high_resolution_clock::now();
std::cout << fixed;
duration<float> time_span2 = endTime2 - startTime2;
printf("%5d" "%10d" "%10f" "%10d" "%10f", i, fibR, time_span.count(),fibI, time_span2.count());
//recur.fib_recursion(i);// Replace with call to your function.
//recur.fib_iterative(i);
}
//std::cout << fixed;
//std::cout << "It took " << time_span.count() << " seconds.";
//std::cout << std::endl;
}
rec_fun.cpp :
#include<iostream>
#include <chrono>
#include "rec_fun.h"
#include <string>
using namespace savitch2;
using namespace std;
using namespace std::chrono;
int recursion::fib_iterative(int n) {
if(n == 1 || n == 2)
return 1;
int A[2][2] = { { 1, 1 },{ 1, 0 } };
int B[2][2] = { { 1, 1 },{ 1, 0 } };
int temp[2][2];
while (n >= 2) {
for (int i = 0; i < 2; i++)
for (int k = 0; k < 2; k++) {
temp[i][k] = 0;
for (int j = 0; j < 2; j++)
temp[i][k] += A[i][j] * B[j][k];
}
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
B[i][j] = temp[i][j];
n--;
}
return B[0][1];
}
int recursion::fib_recursion(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fib_recursion(n-1) + fib_recursion(n-2);
}
rec_fun.h:
#include<iostream>
#include <string>
using namespace std;
namespace savitch2
{
class recursion
{
public:
int fib_iterative(int n);
int fib_recursion(int n);
private:
int ones;
int tens;
string s;
};
}
Output for Fibonacci should be this :
Fibonacci
n Recursive time_r(s) Iterative time_i(s)
1 1 0.000000 1 0.000000
2 1 0.000000 1 0.000000
3 2 0.000000 2 0.000000
4 3 0.000000 3 0.000001
. . .
43 433494437 2.015535 433494437 0.000002
44 701408733 3.270357 701408733 0.000002
45 1134903170 5.258732 1134903170 0.000002
Output I am getting:
#1)
Fibonacci
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论