排名統計問題(c實現)

來源:互聯網
上載者:User
問題描述:
參加任何一項比賽,都會涉及到排名統計問題,由此可見排名統計是多麼的重要.這裡假設有一項比賽,共有n(1<=n<=200)個人蔘加,那麼請問共有多少種不同的排名(注意:可以很多參賽者在同一個名次,即參賽者名次可以並列)?
資料輸入n,輸出為不同的排名總數.
如輸入
1
2
3
輸出為
1
3
13

//程式的效能還不行,演算法基本沒問題了
==================code================
/**
*Copyright (C) aprin at Xiamen University
*2005-4-24
*/
#include <stdio.h>
#define MAX_N 200
#define MAX_LEN 1000

int cheng(int *a1, int len_a1, int *a2, int len_a2);
int c_fun(int n, int r, int *c) {
  int i, j, k, len, temp[3], len_temp, shang[MAX_LEN], len_shang;

  /*計算pr/n*/
  len=1;
  c[0]=1;
  for(i=0; i<r; i++) {
    j=n-i;
    len_temp=0;
    while(j!=0) {
      temp[len_temp]=j%10;
      len_temp++;
      j/=10;
    }
    len=cheng(c, len, temp, len_temp);
  }

  /*/r!*/
  for(i=0; i<MAX_LEN; i++)
    shang[i]=0;
  for(i=2; i<=r; i++) {
    int temp=len-1, yushu;

    len_shang=0;
    yushu=c[temp];
    while(temp>=0) {
      shang[len_shang]=yushu/i;
      len_shang++;
      yushu%=i;
      temp--;
      yushu=yushu*10+c[temp];
    }
    /*ji suan du yu de 0*/
    k=0;
    while(shang[k]==0)
      k++;
    /*倒轉,轉移*/
    for(j=0; j<MAX_LEN; j++)
      if(j<(len_shang-k))/* 實際的長度*/
        c[j]= shang[len_shang-1-j];
      else
        c[j]= 0;
    len= len_shang-k;
  }

  return len;
}
int cheng(int *a1, int len_a1, int *a2, int len_a2) {
  int temp[MAX_LEN], i, j;

  for(i=0; i<MAX_LEN; i++)
    temp[i]=0;
  for(i=0; i<len_a2; i++) {
    for(j=0; j<len_a1; j++) {
      temp[i+j]=temp[i+j]+a1[j]*a2[i];
      temp[i+j+1]=temp[i+j+1]+temp[i+j]/10;
      temp[i+j]=temp[i+j]%10;
    }
    if(temp[i+len_a1]>10) {/*ke neng cun zai de zui gao wei chu li*/
      temp[i+len_a1+1]=temp[i+len_a1+1]+temp[i+len_a1]/10;
      temp[i+len_a1]=temp[i+len_a1]%10;
    }
  }
  i=MAX_LEN-1;
  while(temp[i]==0)
    i--;
  for(j=0; j<MAX_LEN; j++)
    a1[j]=temp[j];
  return i+1;
}
int add(int *a1, int len_a1, int *a2, int len_a2) {
  int i,len;

  len=(len_a1>len_a2)?len_a1:len_a2;
  for(i=0; i<len; i++) {
    a1[i+1]+=(a1[i]+a2[i])/10;
    a1[i]=(a1[i]+a2[i])%10;
  }
  len=MAX_LEN;
  while(a1[len-1]==0)
    len--;
  return len+1;
}
int ok(int *renshu, int i, int n) {
  int tot, j;

  tot=0;
  for(j=0; j<=i; j++)
    tot+=renshu[j];
  return tot==n;
}
void output(int *result, int len) {
  int i;

  for(i=len-2; i>=0; i--)
    printf("%d", result[i]);
  putchar('/n');
}
int main(void) {
  int n, i, j, k;
  int renshu[MAX_N];
  int result[MAX_LEN], len;

  scanf("%d", &n);

  for(i=0; i<MAX_LEN; i++)
    result[i]=0;
  len=0;

  for(i=0; i<n; i++) {
    for(j=0; j<=i; j++)
      renshu[j]=1;
    do {
      if(ok(renshu, i, n)) {
    int result_mid[MAX_LEN], len_mid, temp, total_temp;

    len_mid=1;
    result_mid[0]=1;
    total_temp=n;
    for(temp=0; temp<i; temp++) {
      int zuhe[MAX_LEN], len_zuhe;

            len_zuhe=c_fun(total_temp, renshu[temp], zuhe);
          total_temp-=renshu[temp];
        len_mid=cheng(result_mid, len_mid, zuhe, len_zuhe);
    }
    len=add(result, len, result_mid, len_mid);
      }
      renshu[0]++;
      for(j=0; j<i; j++)
    if(renshu[j]>(n-i)) {
      renshu[j+1]++;
      renshu[j]=1;
    }
    }
  while(renshu[i]<=(n-i));
  }

  output(result, len);
  getch();
  return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.