`

算法作业—数字三角形问题

阅读更多
#include"iostream"
#include"fstream"
using namespace std;

int N;
int a[100][100];

/**计算和最大路径的值**/
int sumMax() {
	int i,j;
	int sum = 0;
	for(i = N; i > 0; i--) {
		for(j = 0; j < N; j++) {
			if(a[i][j] > a[i][j+1]) {
				a[i-1][j] += a[i][j];
			} else {
				a[i-1][j] += a[i][j+1];
			}
		}
	}
	return a[0][0];
}

/**主函数,输入和输出数据**/
void main() {
	int i,j,n = 1;
	ifstream infile("input.txt",ios::in);
	ofstream outfile("output.txt",ios::out);
	if(!infile) {
		cerr<<"open error!"<<endl;
		exit(1);
	} else {
		infile>>N;
		for(i = 1; i < N+1; i++) {
			for(j = 0; j < n; j++) {
				infile>>a[i][j];
			}
			n ++;
		}
		infile.close();
		cout<<endl;
	}
	if(!outfile) {
		cerr<<"open error!"<<endl;
		exit(1);
	} else {
		outfile<<sumMax();
		outfile.close();
	}
}
 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics