回分類題庫
d185: CP Lab Exercise: Debugger(Bonus)
出處:

Difficulity : N/A
Accepted : 14 Times | Submit :27 Times | Clicks : 1128
Accepted : 14 Users | Submit : 16 Users | Accepted rate : 88%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2010-11-17 15:34

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: The content must a revision of the origin one.

The code are listed below:

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


#include<stdio.h>

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

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++ )
        printf(" ");
}
/*------------------------------------------------------------------*/

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(scanf("%d", &nrows) == 1){

        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);
                printf("%4d", comb);
            }

            printf("\n");
        }
    }

    return 0;
}

 

Input :

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

Output :

Output 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
65134. CC501 (6 ms , 362KB)
26998. cp94702040 (6 ms , 232KB)
34224. king_of_laba (8 ms , 234KB)
27197. cp96703035 (8 ms , 240KB)
27019. cp99703032 (8 ms , 234KB)
26993. cp99703003 (8 ms , 230KB)
50739. nwgs524513cja (156 ms , 2164KB)

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