回 帖 发 新 帖 刷新版面

主题:打印杨辉三角形的小程序

@ 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.

}

程序运行正常,但打出的三角形不美观,希望大家帮忙改进。

回复列表 (共16个回复)

沙发

你又是用C++编的呀~

板凳

面里有几句错误,我改了一下:

#include <iostream>
#include <stdio.h>
#include <iomanip.h>    //add this statement;

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;
    cout<<"lines = "<<flush;
    cin>>lines;
    cout<<endl<<endl<<endl<<endl;
    
    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++)     //should be '<=';
        {
            yanghui[n+1]=temp[n+1]+temp[n];    
            cout<<setw(4)<<yanghui[n]<<" "<<flush;   //insert setw(4);
            temp[n] = yanghui[n];
        }
                                
        cout<<endl;
        blank--;
    }
    delete new int[x];   //release dynamic array's memory.

}


3 楼

我打印的15行的三角形:lines = 15

                                 1
                               1    1
                             1    2    1
                           1    3    3    1
                         1    4    6    4    1
                       1    5   10   10    5    1
                     1    6   15   20   15    6    1
                   1    7   21   35   35   21    7    1
                 1    8   28   56   70   56   28    8    1
               1    9   36   84  126  126   84   36    9    1
             1   10   45  120  210  252  210  120   45   10    1
           1   11   55  165  330  462  462  330  165   55   11    1
         1   12   66  220  495  792  924  792  495  220   66   12    1
       1   13   78  286  715 1287 1716 1716 1287  715  286   78   13    1
     1   14   91  364 1001 2002 3003 3432 3003 2002 1001  364   91   14    1

4 楼

在图形中是用特征的
除了所有的1外,其他的数均为其上方的两个数只和;
这样就能够是你的程序变的十分简单你只要先打印出所有的1在用一个2重循环即可解决问题。

5 楼

[quote]你只要先打印出所有的1在用一个2重循环即可解决问题[/quote]
是啊,问题是很简单,但电脑可没有这么聪明,
它只管埋头干活,一路运行下去,不可能再回头重复打印的,
哈哈。

6 楼

呵呵,真是想不到我在一个讨论中的发言居然浪费了楼主一个晚上的时间。在此我要对楼主说声真的对不住。记得我们上次讨论的问题是阁下的帖子:编程不是为了做代数。我现在还是那个观点。不做代数是不行的。下面是使用数学公式(组合)来做的:
void main()
{
    int n,i,j,c;
    cout<<"n=";
    cin>>n;
    if(n>13)
        cout<<"输入的数值太大!"<<endl;
    else
    {
        for(i=0;i<n;i++)
        {
            for(j=1;j<15-i;j++)
            cout<<"  ";
            c=1;
            cout<<c<<"   ";
            for(j=1;j<=i;j++)
            {
                c=c*(i-j+1)/j;
                if(c<100)
                    if(c<10)
                        cout<<c<<"   ";
                    else
                        cout<<c<<"  ";
                else
                    cout<<c<<" ";
            }
            cout<<endl;
        }
    }
}
由于n的值超过了13之后打印空格的控制有变化所以我限定了n的值不能大于13。
我和楼主的程序的代码的区别在于楼主主要是计算需打印的数字。而我则是重点考虑空格的控制。使用了恰当的数学公式简化了我的编程。所以学数学对编程是很有用的。
也请大家看了之后多指教指教。看到楼主这么热情,希望能和楼主成为好朋友。
本来是该昨天就回的,但是我昨天去了我奶奶家现在才看到帖子,真是不好意思。sorry了楼主

7 楼

引用
      ------------------------------------------------------
       是啊,问题是很简单,但电脑可没有这么聪明,
       它只管埋头干活,一路运行下去,不可能再回头重复打印的,
       哈哈
      ------------------------------------------------------

只要你告诉他方法电脑也会走回头路的。

8 楼

@ johnny1208 :

谢谢您,我现在正为我的算法水平太差而发愁呢,论坛里的好多简单的问题,对我来说一点头绪也没有,不知从哪下手去解决,看来得去找一本好书补补。
另外,我这一句:
[quote]delete new int[x];   //release dynamic array's memory.[/quote]
有错误,干脆删了这句,会好点吧。


9 楼

呵呵,一起努力,我也是菜鸟,我们一起学习。

10 楼

可以将三角形对半分开,只计算前一半,后一半用相应行的倒顺序就可以显示,因为三角形是对称的。

我来回复

您尚未登录,请登录后再回复。点此登录或注册