有大佬能看出这个代码有什么问题吗?
//求解循环汉诺塔
#include
#include "vector"
using namespace std;
class Solution {
public:
vectorfn;
vectorgn;
Solution(): fn(10000000, 0), gn(10000000, 0) {}
long long FN(int n) { //计算fn数组
if (n == 1) {
fn[1] = 1;
return fn[1];
} else {
if (gn[n - 1] == 0) { //位置不存在
fn[n] = ((2 * GN(n - 1) + 1) % 1000000007);
return fn[n];
} else {
fn[n] = ((2 * gn[n - 1] + 1) % 1000000007);
return fn[n];
}
}
}
long long GN(int n) { //计算gn数组
if (n == 1) {
gn[1] = 2;
return gn[1];
} else {
if (gn[n - 1] == 0 && fn[n - 1] == 0) {
gn[n] = ((2 * GN(n - 1) + FN(n - 1) + 2) % 1000000007);
return gn[n];
} else {
gn[n] = ((2 * gn[n - 1] + fn[n - 1] + 2) % 1000000007);
return gn[n];
}
}
}
void LoopHanoi(int n)
{
cout << FN(n) << " " << GN(n) << endl;
}
};
int main() {
Solution solution;
int n;
cin >> n;
solution.LoopHanoi(n);
}
c
#include
#include "vector"
using namespace std;
class Solution {
public:
vector
vector
Solution(): fn(10000000, 0), gn(10000000, 0) {}
long long FN(int n) { //计算fn数组
if (n == 1) {
fn[1] = 1;
return fn[1];
} else {
if (gn[n - 1] == 0) { //位置不存在
fn[n] = ((2 * GN(n - 1) + 1) % 1000000007);
return fn[n];
} else {
fn[n] = ((2 * gn[n - 1] + 1) % 1000000007);
return fn[n];
}
}
}
long long GN(int n) { //计算gn数组
if (n == 1) {
gn[1] = 2;
return gn[1];
} else {
if (gn[n - 1] == 0 && fn[n - 1] == 0) {
gn[n] = ((2 * GN(n - 1) + FN(n - 1) + 2) % 1000000007);
return gn[n];
} else {
gn[n] = ((2 * gn[n - 1] + fn[n - 1] + 2) % 1000000007);
return gn[n];
}
}
}
void LoopHanoi(int n)
{
cout << FN(n) << " " << GN(n) << endl;
}
};
int main() {
Solution solution;
int n;
cin >> n;
solution.LoopHanoi(n);
}
c
全部评论
牛客上只有文本太难看出来了,不如直接问chatgpt
相关推荐