主题:打印杨辉三角形的小程序
@ johnny1208 :
老兄,为了完成您布置的作业,昨晚花了一晚上,今天早晨又折腾了一上午,终于搞定了。
[quote]
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
。。。。。。
上面这个是杨辉三角,要打印他很简单,用个2唯数组写个百把十句就OK了。但要是限定只允许你使用循环嵌套做,你不会数学原理能行吗?所以代数还是很重要滴,不学是不行滴。[/quote]
问题出处:http://www.programfan.com/club/showbbs.asp?id=47781
这是我的小程序:
#include <iostream>
#include <stdio.h>
using namespace std;
// define function prototype
void yhtriangle(int& x);
int main()
{
int lines;
cout<<"This is a little prog to print YangHui triangle ."<<endl;
cout<<"Please enter the lines what you want to print: "<<endl;
cin>>lines;
yhtriangle(lines);
cout<<endl<<endl<<endl;
system("pause");
return 0;
}
void yhtriangle(int& x)
{
int *yanghui;
int *temp; //to save operation value.
int n;
int blank; //counter the space numbers
int counter;
//cin>>x; // I decide to input x from main()
//make two dynamic arrays
yanghui = new int[x];
temp = new int[x];
//initialize arrayes
for(n = 0; n < x; n++)
{
yanghui[n] = 0;
temp[n] = 0;
}
yanghui[0] = 1;
temp[0] = 1;
blank = x;
//circulate print triangle
for(counter = 0; counter < x; counter++)
{
for(n = 0; n < blank; n++)
cout<<" ";
for(n = 0; n < counter; n++)
{
yanghui[n+1]=temp[n+1]+temp[n];
cout<<yanghui[n]<<" "<<flush;
temp[n] = yanghui[n];
}
cout<<endl;
blank--;
}
delete new int[x]; //release dynamic array's memory.
}
程序运行正常,但打出的三角形不美观,希望大家帮忙改进。
老兄,为了完成您布置的作业,昨晚花了一晚上,今天早晨又折腾了一上午,终于搞定了。
[quote]
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
。。。。。。
上面这个是杨辉三角,要打印他很简单,用个2唯数组写个百把十句就OK了。但要是限定只允许你使用循环嵌套做,你不会数学原理能行吗?所以代数还是很重要滴,不学是不行滴。[/quote]
问题出处:http://www.programfan.com/club/showbbs.asp?id=47781
这是我的小程序:
#include <iostream>
#include <stdio.h>
using namespace std;
// define function prototype
void yhtriangle(int& x);
int main()
{
int lines;
cout<<"This is a little prog to print YangHui triangle ."<<endl;
cout<<"Please enter the lines what you want to print: "<<endl;
cin>>lines;
yhtriangle(lines);
cout<<endl<<endl<<endl;
system("pause");
return 0;
}
void yhtriangle(int& x)
{
int *yanghui;
int *temp; //to save operation value.
int n;
int blank; //counter the space numbers
int counter;
//cin>>x; // I decide to input x from main()
//make two dynamic arrays
yanghui = new int[x];
temp = new int[x];
//initialize arrayes
for(n = 0; n < x; n++)
{
yanghui[n] = 0;
temp[n] = 0;
}
yanghui[0] = 1;
temp[0] = 1;
blank = x;
//circulate print triangle
for(counter = 0; counter < x; counter++)
{
for(n = 0; n < blank; n++)
cout<<" ";
for(n = 0; n < counter; n++)
{
yanghui[n+1]=temp[n+1]+temp[n];
cout<<yanghui[n]<<" "<<flush;
temp[n] = yanghui[n];
}
cout<<endl;
blank--;
}
delete new int[x]; //release dynamic array's memory.
}
程序运行正常,但打出的三角形不美观,希望大家帮忙改进。