我正在尝试在c ++中使用多维数组。我制作了一个程序,仅按特定顺序从一个角到另一个角运行此数组,但是我不知道如何在递归函数中转换此代码。
#include <fstream>
using namespace std;
ifstream f("2.in");
ofstream g("2.out");
int A[21][21],n,m,D[21][21],S[21][21];
int main()
{
f>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
f>>A[i][j];
//number of ways
for(int i=n;i>=1;i--)
for(int j=1;j<=n;j++)
if(i==n || j==1) D[i][j]=1;
else D[i][j]=D[i][j-1]+D[i+1][j-1]+D[i+1][j];
g<<D[1][n]<<endl;
//max_summ
for(int i=n;i>=1;i--)
for(int j=1;j<=n;j++)
S[i][j]=A[i][j]+max(max(S[i][j-1],S[i+1][j-1]),S[i+1][j]);
g<<S[1][n]<<endl;
return 0;
}
----------------
2.in file
----------------
3
1 2 3
-1 3 4
2 -1 -1
----------------
如何为number of ways
和max_summ
设置递归函数?
答案 0 :(得分:0)
如果您的问题与递归函数的概念有关,那么这是一个简单的示例。它将从3倒数到0或您调用的任何数字。
#include <iostream>
using namespace std;
int counting(int x){
cout << x << endl;
if(x>0) x = counting(--x);
return x;
}
int main(){
counting(3);
}