c和c++的一些训练题(6)(杨辉三角)

杨辉三角的示意图:

《c和c++的一些训练题(6)(杨辉三角)》

程序很简单,我弄的有些复杂,贴出来看看,以后有时间修正,没时间就算了。

// yanghui_triangle.cpp : 定义控制台应用程序的入口点。
//编一个杨辉三角

#include "stdafx.h"
#include <iostream>

using namespace std;

//阶乘(递归实现)
int factorial(int n)
{
	if(n==1)
	{
		return 1;
	}

	return n*factorial(n-1);
}
int calu(int a, int b)
{
	int c=1;
	for(int i=0; i<b; i++)
	{
		c=c*(a-i);
	}
	return c;
}
int _tmain(int argc, _TCHAR* argv[])
{
	int n;
	int value;
	cout<<"请输入行数:";
	cin>>n;
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<=i; j++)
		{
			if(i==0)
			{
				cout<<"1";
			}
			else
			{
				if(j==0)
				{
					value=1;
				}
				else
				{
					value=calu(i,j)/factorial(j);	
				}
				cout<<value<<" ";
				
			}
			
		}
		cout<<endl;
		
	}
	//cout<<factorial(n);
	system("pause");
	return 0;
}

懒得去尝试排版了,简单的给出结果吧。

《c和c++的一些训练题(6)(杨辉三角)》


    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/jiang111_111shan/article/details/44266031
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞