谁会c++,帮我编一下

来源:百度知道 编辑:UC知道 时间:2024/05/07 12:46:52
杨辉三角形
Description
打印出杨辉三角形的前n行。
Input
输入一个正整数n ( n <20 )。
Output
杨辉三角形。
Sample Input
5
Sample Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

// test.cpp : 定义控制台应用程序的入口点。
//特地加了许多注释,希望你看得懂..

#include "stdafx.h"
#include "iostream"
#include "string"
#include "iomanip"//setw()函数需要的头文件
using namespace std;

const int N = 20;//程序接收的最大行数

int _tmain(int argc, _TCHAR* argv[])
{
/*一般不要用拼音作为变量或函数名字,这里例外*/
int yanghui(int,int);
cout<<"Please input a number (between 0 and "<<N<<"):";
int n;
cin>>n;
if(n>N||n<0) {
cout<<"The number must between 0 and "<<N<<".\n";
exit(0);
}
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<setw(6)<<yanghui(i,j);
}
cout<<endl;
}
return 0;
}
/*计算返回杨辉三角 第m行 第n个数 的值*/
int yanghui(int m,int n){
/*每行第1个和最后一个数字都为1*/
if (n==1||m==n) return 1;
/*第m行的第n个数等于 第(m