C语言程序设计中的二项式分布序列

问题描述 投票:-1回答:2

我可以用下面的代码写出pascal三角图,但我希望打印出**。

(a+b)^p

**系列.我的愿望如下。输出样本。

(a+b)^p for unfolding p = 5
1
a^1 + b^1
a^2 + 2*a^1*b^1 + b^2
a^3 + 3*a^2*b^1 + 3*a^1*b^2 + b^3
a^4 + 4*a^3*b^1 + 6*a^2*b^2 + 4*a^1*b^3 + b^4
a^5 + 5*a^4*b^1 + 10*a^3*b^2 + 10*a^2*b^3 + 5*a^1*b^4 + b^5 

 #include <stdio.h>
 long paskal(int,int);
 int main() 
 {
   int n=0;
   int m=0;
   int k=0;
   int s=0;
   printf("Enter number of rows:\n");
   scanf("%d", &n);
   for(k = 0; n >= k; k++) {
        for(m = 0; k >= m; m++) {
              long f = paskal(k, m);
                if(k==1)
                    printf("%ld",f);
                else
                    printf("a^%ld %ld b^%ld",k,f,k);
        }
        printf("\n");
    }
    return 0;
 }

 long paskal(int n, int i) {
    if(n == i || i == 0)
      return 1;
    else
      return paskal(n-1, i) + paskal(n-1, i-1);

   }

  Enter number of rows:                                                                                                   
  5 
  11                                                                                                                      
  a^2 1 b^2a^2 2 b^2a^2 1 b^2                                                                                             
  a^3 1 b^3a^3 3 b^3a^3 3 b^3a^3 1 b^3                                                                                    
  a^4 1 b^4a^4 4 b^4a^4 6 b^4a^4 4 b^4a^4 1 b^4                                                                           
  a^5 1 b^5a^5 5 b^5a^5 10 b^5a^5 10 b^5a^5 5 b^5a^5 1 b^5

我为我的英语感到抱歉。谢谢你

c algorithm math series binomial-theorem
2个回答
0
投票

在你所希望的输出中,我建议不要打印出以下的幂数 a 当它是1时,所以 a^1 堪称 a.

那么你可以这样做 -- 很简单,去除 if 剔除 ^1如果你还想在输出中使用它

// Helper function
void factor(long f, char letter, int power) {
    if (f != 1)
        printf("*");
    if (power != 0) {
        printf("%c", letter);
        if (power != 1)
            printf("^%d", power);
    }
}

int main() {
    int n = 0;
    printf("Enter number of rows:\n");
    scanf("%d", &n);
    if (n > 0)
        printf("1\n");
    for (int k = 1; k < n; k++) {
        for (int m = 0; m <= k; m++) {
            if (m != 0)
                printf(" + ");
            long f = paskal(k, m);
            if (f != 1)
                printf("%ld", f);
            factor(f, 'a', k - m);
            factor(f, 'b', m);
        }
        printf("\n");
    }
    return 0;
}

0
投票

我解决了这个问题

#include <stdio.h>
int main() 
{
  int s=0;
  printf("Lütfen sayı giriniz:\n");
  scanf("%ld", &s);
 while (s < 0)
 {
   printf("Lütfen pozitif sayı giriniz:\n");
   scanf("%d", &s);
  }
  printf("(a+b)^%ld =",s);
  printf("\n");
  for(int k = 0; s >= k; k++) {

    hesapla(k);
    printf("\n");
   }
  return 0;
  }
  void yazdir(int n,int r, int deger)
 {
   if(r== 0){
      if (n-r != 0) {
        if(deger == 1){
            printf("(a^%ld) +",n-r);
         }else{
           printf(" %ld*(a^%ld) +",deger,n-r);
       }
      } else {
          printf("1");
     }
   } else if(r>0 && r<n){
      printf(" %ld*(a^%ld*b^%ld) +",deger,n-r,r);
   } else{
      if(deger==1){
        printf(" (b^%ld)",r);
      } else {
         printf(" %ld*(b^%ld)",deger,r);
     }
  }
 }
 void hesapla (int n){
    int r;
     int deger;
    for (r=0;r<=n;r++) {
      deger = kombinasyon(n,r);
      yazdir(n,r,deger);
    }
  }
  int kombinasyon ( int n, int k) {
  if(k == 0){
    return 1;
  }
    if(k>n-k){
     return kombinasyon(n,n-k);
    }
     return n*kombinasyon(n-1,k-1)/k;

   }

Lütfen sayı giriniz:                                                                                                                           
 5                                                                                                                                              
 (a+b)^5 =                                                                                                                                      
 1                                                                                                                                              
 (a^1) + (b^1)                                                                                                                                  
 (a^2) + 2*(a^1*b^1) + (b^2)                                                                                                                    
 (a^3) + 3*(a^2*b^1) + 3*(a^1*b^2) + (b^3)                                                                                                      
 (a^4) + 4*(a^3*b^1) + 6*(a^2*b^2) + 4*(a^1*b^3) + (b^4)                                                                                        
 (a^5) + 5*(a^4*b^1) + 10*(a^3*b^2) + 10*(a^2*b^3) + 5*(a^1*b^4) + (b^5) 
© www.soinside.com 2019 - 2024. All rights reserved.