回分類題庫
d163: OOP Lab Exercise: Debug with a debugger
出處:

Difficulity : 3
Accepted : 76 Times | Submit :113 Times | Clicks : 1378
Accepted : 74 Users | Submit : 75 Users | Accepted rate : 99%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2010-03-23 16:04

Content :

Given a code segment of generating pascal triangle, your job is to use debugger to find those errors and fix them. When you can generate a proper pascal triangle like the one from example output, you can feel free to submit your code and see the result.

Notice: You have to implement it in CPP and the content must a revision of the origin one.

The code are listed below:

 

 /*******************************************************************
*    PROJECT:     Lab assignment on debugging
*************************************************************************/


#include <iostream>
#include <iomanip>
using namespace std;

/*------------------------------------------------------------------*/

void skip(int n)
/*  PURPOSE:  To skip n spaces on a line
    RECEIVES: n - the number of spaces to skip
    REMARKS:  n should be non-negative
*/
{  int i;  /* a counter */

   for (i = 0; i <= n; i++ )
      cout << " ";
}
/*------------------------------------------------------------------*/

int factorial(int n)
/*  PURPOSE:  To calculate n factorial
    RECEIVES: n - calculate the factorial of n
    RETURNS:  n factorial
    REMARKS:  n must be >= 0.  Also if n is too large overflow may result
*/
{  int  product; /*  accumulator for the running product  */
   int  i; /*  a counter  */

   product = 1;
   for (i = 1; i <= n; i++)
   {   product = product + i ;
   }
   return(product);
}

/*------------------------------------------------------------------*/

int combination(int n, int k)
/* PURPOSE:  to calculate the number of combinations of n things taken
             k at a time (n choose k)
   RECEIVES: n - the number of items to choose from
             k - the number of items choosen
   RETURNS:  n choose k
   REMARKS:  n and k must be non-negative and k <= n.  This program uses
             the formula (n choose k) = n! / ( k! * (n-k)! ).
*/
{  int comb = factorial(n) / factorial(k) * factorial(n-k);

   return comb;
}

/*------------------------------------------------------------------*/

int main(void)
{  int nrows; /*  the number of rows to print  */
    int n; /*  a counter for the current row  */
    int k; /*  a counter for the current column  */
    int comb; /*  the number of combinations  */
    int spaces_to_skip; /*  spaces to skip  */

    while(cin >> nrows){

        spaces_to_skip = 36;

        for (n = 0; n < nrows; n = n + 2)
        {  skip(spaces_to_skip); /* space to make a triangle */
            spaces_to_skip = spaces_to_skip - 2;

            for (k = 0; k <= n; k++)
            {  comb = combination(n,k);
                cout << setw(4) << comb;
            }

            cout << "\n" ;
        }
    }

    return 0;
}

Input :

Each line is a positive integer N (0 < N <= 13), but the code segment had already done this.

Output :

Out a pascal triangle of N rows. The code segment shall done all the details. Please refer to the demo in class.

Sample Input :

1
2
3

Sample Output :

                                        1   1
                                        1   1
                                      1   2   1
                                        1   1
                                      1   2   1
                                    1   3   3   1

Hint :


  

Author :


  Solve it!   Status Forum (0)

C++
C
JAVA
19075. oop98703054 (2 ms , 354KB)
19069. grandline (2 ms , 358KB)
18966. oop98703033 (2 ms , 356KB)
18957. oop94306023 (2 ms , 350KB)
18952. oop98703016 (2 ms , 350KB)
沒有解題記錄 50729. nwgs524513cja (130 ms , 1924KB)
50728. nwgs524513cja (136 ms , 2154KB)
50727. nwgs524513cja (142 ms , 1950KB)

執行時間會受很多因素影響因此僅供參考,主機等級請看這裡