汉诺塔问题: // {驱动程序代码启动// C++的初始模板
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution{
public:
static int i=0;
void hanoi(vector<int>& v,int N,int n,int start,int end)
{
i++;
if(i==n)
{
v[0]=start;
v[1]=end;
return;
}
int other=6-(start+end);
hanoi(v,N-1,n,start,other);
hanoi(v,N-1,n,other,end);
}
vector<int> shiftPile(int N, int n){
// code here
Solution::i=0;
vector<int>v(2,0);
hanoi(v,N,n,1,3);
return v;
}
};
// { Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--){
int N, n;
cin>>N>>n;
Solution ob;
vector<int> ans = ob.shiftPile(N, n);
cout<<ans[0]<<" "<<ans[1]<<endl;
}
return 0;
} // } Driver Code Ends错误出现在类解决方案中,我不能声明它,C++禁止非常数静态成员的类内初始化,Solution::i.Please帮助解决上述问题。
发布于 2021-05-31 02:13:15
声明并定义静态数据成员,如
class Solution{
public:
static int i;
//...
};
int Solution::i = 0;尽管不清楚为什么变量i是用存储类说明符static声明的。
https://stackoverflow.com/questions/67764386
复制相似问题