简体   繁体   English

Pascal在C中的三角形与组合

[英]Pascal's triangle in C with combinations

#include <stdio.h>
long factorial(int num)
{
    int counter;
    int fact = 1;
    for (counter = num; counter > 0; counter--) fact *= counter;
    return fact;
}

float combinations(int n, int k)
{
    int numerator = factorial(n);
    int denominator = factorial(k) * factorial(n-k);
    float fraction = numerator/denominator;
    return fraction;
}
int main()
{
    printf("How many rows of Pascal\'s triangle should I print?\t");
    int rows = GetInteger();
    int counter;
    int counter2;
    for (counter = 1; counter <= rows; counter++)
    {
        int y = rows-counter;
        for (; y > 0; y--) printf("   ");
        for (counter2 = 0; counter2 <= counter; counter2++)
                printf("%6.0lu", (long) combinations(counter, counter2));
        printf("\n");
    }
}

Every time I go past twelve rows, the numbers start to decrease. 每次我超过十二行,数字开始减少。 What am i doing wrong? 我究竟做错了什么?

And, GetInteger() is just a scanf() with a few touch ups. 并且, GetInteger()只是一个带有一些GetInteger()scanf() I am 100% sure it works perfectly. 我百分百肯定它完美无缺。

After 12th row factorial and so pascal triangle elements become too large so int type cannot hold them - so you get overflow (most probably values you get are wrapped around maximum int value). 在第12行因子之后,所以pascal三角形元素变得太大,因此int类型不能保持它们 - 所以你得到溢出(你得到的最可能的值包含在最大int值)。

PS why do you use 3 different types in your code (long, int, float)? PS为什么你在代码中使用3种不同的类型(long,int,float)? As k!*(nk)! 作为k!*(nk)! always divides n! 总是划分n! you do not need float value (you use integer division and cast result to long anyway). 你不需要浮点值(无论如何你使用整数除法和转换结果)。 Just use the biggest integer type you can, or some custom BigInt type that can hold integer numbers of arbitrary length - so you can show correct values for large row numbers. 只需使用最大的整数类型,或者一些可以保存任意长度的整数的自定义BigInt类型 - 这样就可以显示大行数的正确值。

Don't start from factorials. 不要从阶乘开始。 Start from the following facts about Pascal's triangle: 从关于Pascal三角形的以下事实开始:

  1. the nth row of the triangle has n elements (if we start counting from 1) 三角形的第n行有n个元素(如果我们从1开始计数)
  2. the first and last elements of each row are 1 每行的第一个和最后一个元素是1
  3. each element aside from the first and last one is the sum of the two elements diagonally above it (if the triangle is written in a symmetric way) 除了第一个和最后一个元素之外的每个元素是对角线上方的两个元素的总和(如果三角形以对称方式写入)

You will of course be limited by the size of the data type you are holding results in, but not any sooner than necessary (by intermediate results such as factorials). 当然,您将受限于您持有的数据类型的大小,但不会超过必要的时间(通过诸如阶乘的中间结果)。

INT_MAX is usually 2,147,483,647 INT_MAX通常为2,147,483,647
12! 12! is 479,001,600 是479,001,600
13! 13! is 6,227,020,800 but your function factorial(13) returns 1,932,053,504 (= 6,227,020,800 - 4,294,967,296) 是6,227,020,800,但你的函数factorial(13)返回1,932,053,504(= 6,227,020,800 - 4,294,967,296)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM