zoukankan      html  css  js  c++  java
  • Cut the Cake(大数相乘)

     
    MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly one of her friends HZ took some (N) strawberries which MMM likes very much to decorate the cake (of course they also eat strawberries, not just for decoration). HZ is in charge of the decoration, and he thinks that it's not a big deal that he put the strawberries on the cake randomly one by one. After that, MMM would cut the cake into M pieces of sector with equal size and shape (the last one came to the party will have no cake to eat), and choose one piece first. MMM wants to know the probability that she can get all N strawberries, can you help her? As the cake is so big, all strawberries on it could be treat as points.
     
    Input
    First line is the integer T, which means there are T cases.
    For each case, two integers M, N indicate the number of her friends and the number of strawberry.
    (2 < M, N <= 20, T <= 400)
     
    Output
    As the probability could be very small, you should output the probability in the form of a fraction in lowest terms. For each case, output the probability in a single line. Please see the sample for more details.
     
    Sample Input
    2
    3 3
    3 4
     
    Sample Output
    1/3
    4/27
     
    主要是大数的乘法并约分;
     
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 const int MAX = 200;
     5 
     6 char ans[2*MAX],mul[MAX];
     7 
     8 int gcd(int a, int b)
     9 {
    10     if(b == 0)
    11         return a;
    12     return gcd(b,a%b);
    13 }
    14 
    15 void multiply(char*a,char*b,char*c)
    16 {//正着乘,从最高位开始;
    17     int *s;
    18     int i,j;
    19     int ca = strlen(a);
    20     int cb = strlen(b);
    21     s = (int*)malloc(sizeof(int)*(ca+cb));
    22     for(i = 0; i < ca+cb; i++)
    23         s[i] = 0;
    24 
    25     for(i = 0; i < ca; i++)
    26     {
    27         for(j = 0; j < cb; j++)
    28         {
    29             s[i+j+1] += (a[i]-'0')*(b[j]-'0');//i+j+1是为了防止最高位进位出现错误
    30         }
    31     }
    32 
    33     for(i = ca+cb-1; i >= 0; i--)
    34     {
    35         if(s[i] >= 10)
    36         {
    37             s[i-1] += s[i]/10;
    38             s[i] %= 10;
    39         }
    40     }
    41 
    42     i=0;
    43     while (s[i]==0)
    44         i++;//去除前导0
    45     for (j=0; i<ca+cb; i++,j++)
    46         c[j]=s[i]+'0';
    47     c[j]= 0;//将结果存储到字符数组
    48     free(s);
    49 }
    50 int main()
    51 {
    52     int test,i;
    53     scanf("%d",&test);
    54 
    55     while(test--)
    56     {
    57         int M,N;
    58         scanf("%d %d",&M,&N);
    59 
    60         memset(ans,0,sizeof(ans));
    61         memset(mul,0,sizeof(mul));
    62         ans[0] = '1';
    63         ans[1] = '';
    64 
    65         int flag = 1;
    66         int n = N;
    67         for(i = 1; i <= N-1; i++)
    68         {
    69             int m = M;
    70             if(flag == 1)
    71             {
    72                 int g = gcd(n,m);
    73                 if(g == 1)
    74                 {
    75                     flag = 0;
    76                 }
    77                 else
    78                 {
    79                     n/=g;
    80                     m/=g;
    81                 }
    82             }
    83             if(m >= 10)
    84             {
    85                 mul[0] = m/10+'0';
    86                 mul[1] = m%10+'0';
    87                 mul[2] = '';
    88             }
    89             else
    90             {
    91                 mul[0] = m+'0';
    92                 mul[1] = '';
    93             }
    94             multiply(ans,mul,ans);
    95         }
    96         printf("%d/%s
    ",n,ans);
    97     }
    98     return 0;
    99 }
    View Code
  • 相关阅读:
    多年收集的一些稀有软件1
    Object-C中使用NSKeyedArchiver归档(将各种类型的对象存储到文件中)
    转-- iOS 30多个iOS常用动画,带详细注释
    转-ios设备唯一标识获取策略
    微信授权
    Windows服务Demo
    查询某个时间段在另一个时间段里面的时间
    微服务官方文档链接
    c# html 转word
    Unreal4 入门(配置)
  • 原文地址:https://www.cnblogs.com/LK1994/p/3344689.html
Copyright © 2011-2022 走看看