zoukankan      html  css  js  c++  java
  • hdu 1261(排列组合)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1261

    思路:公式很好推,就是(n1+n2+n3+...nn)!/(n1!+n2!+...+nn!);

    然后毫无疑问要用到大数计算...发现用string,vector还是挺方便的。。。

    View Code
     1 #include<iostream>
     2 #include<vector>
     3 #include<string>
     4 #include<algorithm>
     5 using namespace std;
     6 int num[27];
     7 vector<string>facs;
     8 
     9 string Divide(const string &str,int n){
    10     int len=str.size()-1;
    11     string s="";
    12     int c=0,p=0;
    13     for(int i=0;i<=len;i++){
    14         c=p*10+str[i]-'0';
    15         p=c%n;
    16         c/=n;
    17         if(c==0&&s.size()==0)continue;
    18         else s+=(c+'0');
    19     }
    20     return s;
    21 }
    22 
    23 string Multiple(const string &str,int n){
    24     int len=str.size()-1;
    25     int c=0,p=0;
    26     string s="";
    27     for(int i=len;i>=0;i--){
    28         c=(str[i]-'0')*n+p;
    29         p=c/10;
    30         c%=10;
    31         s+=(c+'0');
    32     }
    33     while(p){
    34         c=p%10;
    35         p/=10;
    36         s+=(c+'0');
    37     }
    38     reverse(s.begin(),s.end());//字符串倒置
    39     return s;
    40 }
    41 
    42 void Calucate_Facs(){
    43     facs.push_back("1");//0的阶乘
    44     facs.push_back("1");//1的阶乘
    45     facs.push_back("2");//2的阶乘
    46     string tmp="2";
    47     for(int i=3;i<=26*12;i++){
    48         tmp=Multiple(tmp,i);
    49         facs.push_back(tmp);
    50     }
    51 }
    52 
    53 int main(){
    54     Calucate_Facs();//一开始打表计算阶乘
    55     int n;
    56     while(~scanf("%d",&n)&&n){
    57         string s="",str="";
    58         int sum=0;
    59         for(int i=0;i<n;i++){
    60             scanf("%d",&num[i]);
    61             sum+=num[i];
    62         }
    63         str=facs[sum];
    64         for(int i=0;i<n;i++){
    65             for(int j=2;j<=num[i];j++){
    66                 str=Divide(str,j);
    67             }
    68         }
    69         cout<<str<<endl;
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    python 类型注解
    python 装饰器
    python 高阶函数、柯里化
    python 生成器函数
    python 递归和匿名函数
    Python 函数返回值、作用域
    Python 练习题总结(待续)
    基于 K8S 集群安装部署 istio-1.2.4
    基于 kubeadm 部署 kubernetes(v1.17.0) 集群
    Gitlab 重置 root 密码
  • 原文地址:https://www.cnblogs.com/wally/p/2959566.html
Copyright © 2011-2022 走看看