杨辉三角,一个vector实现,不复制,不用队列。

昨晚写到纸上,今天晚上调通,本来想要动态数组,但是发现这块的知识还欠缺,用着有问题。自己基础知识一直有问题,真是心急,要学习的知识好多,都不知道先学哪个。

整个代码效率应该很低,因为进行了大量的判断。

目前没有在网上搜到一样的代码,有点小虚荣。

#include<iostream>
#include <vector>
using std::cin;
using std::vector;
using std::cout;
using std::endl;


void PrintTriangle(int N)
{
	vector<int> test(N);
	test[0] = 1;
	for (int i = 0; i <N; ++i)
	{
		cout << test[0]<<' ';
		int flag = 1;
		int index = 1;
		int t1 = 1;
		int t2;
		for (int j = 0; j < i;++j)
		{
			if (flag==0)
			{
				t1 = test[index];
			}
			if (flag==1)
			{
				t2 = test[index];
			}
			if (flag==1)
			{
				cout << test[index]<<' ';
				test[index] = test[index] + t1;
				++index;
				flag = 0;
				continue;
			}
			if (flag==0)
			{
				cout << test[index] << ' ';
				test[index] = test[index] + t2;
				++index;
				flag = 1;
				continue;
			}
		}
		if (index==N)
		{
			break;
		}
		test[index] = 1;
		cout << endl;
	}


}

int main()
{
	int testLength =5;
	PrintTriangle(testLength);

	system("pause");
	return 0;
}


posted on 2016-07-13 23:22  雪峰00  阅读(141)  评论(0)    收藏  举报

导航