cpptemplate.cpp

来自「杨辉三角形的输出方法设计」· C++ 代码 · 共 61 行

CPP
61
字号
//=====================================
// title: 数组排序
// author: cjj
// date: 2007-10-09
/* Description: 
*/
//=====================================

#include<iostream.h> 
#include <string>

//杨辉三角 
const int NUM = 10;
void yahui(int* result, int num); 

int main()
{
	int yh[NUM]; 
	memset(yh, 0, sizeof(int) * NUM); 

	//打印 
	for (int i = 0; i < NUM; i++)
	{
		yahui(yh, i + 1); 
		for (int j = 0; j < 2 * NUM - 1; j++)
		{
			if (j % 2 != 0)
				cout << ' ';
			else
			{
				if (yh[j / 2])
					cout << yh[j / 2];
				else
					cout << ' ';
			}
		} 
		cout << endl;
	}
} 

void yahui(int* result, int num)
{
	if (num == 1)
		result[0] = 1;
	else if (num == 2)
		result[0] = result[1] = 1;
	else
	{
		int* temp; 
		temp = new int[num - 1]; 
		memset(temp, 0, sizeof(int) * (num - 1)); 
		yahui(temp, num - 1); 
		result[0] = result[num - 1] = 1; 
		for (int i = 1; i < num - 1; i++)
		{
			result[i] = temp[i - 1] + temp[i];
		} 
		delete temp;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?