zoukankan      html  css  js  c++  java
  • 杭电_ACM_Buy the Ticket

    Problem Description
    The \\\\\\\"Harry Potter and the Goblet of Fire\\\\\\\" will be on show in the next few days. As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won’t you?

    Suppose the cinema only has one ticket-office and the price for per-ticket is 50 dollars. The queue for buying the tickets is consisted of m + n persons (m persons each only has the 50-dollar bill and n persons each only has the 100-dollar bill).

    Now the problem for you is to calculate the number of different ways of the queue that the buying process won\\\\\\\'t be stopped from the first person till the last person. 
    Note: initially the ticket-office has no money. 

    The buying process will be stopped on the occasion that the ticket-office has no 50-dollar bill but the first person of the queue only has the 100-dollar bill.
     
    Input
    The input file contains several test cases. Each test case is made up of two integer numbers: m and n. It is terminated by m = n = 0. Otherwise, m, n <=100.
     
    Output

                For each test case, first print the test number (counting from 1) in one line, then output the number of different ways in another line.
     
    Sample Input
    3 0
    3 1
    3 3
    0 0
     
    Sample Output
    Test #1:
    6
    Test #2:
    18
    Test #3:
    180
    View Code
     1 #include <stdio.h>
     2 int factical[205][400];
     3 int result[400];
     4 //get the factical from 1 to 200
     5 void work()
     6 {
     7     int carry, length, i, j;
     8     factical[1][0] = 1;
     9     factical[1][1] = 1;
    10     for (i = 2; i <= 200; i++)
    11     {
    12         length = factical[i - 1][0];
    13         carry = 0;
    14         for (j = 1; j <= length; j++)
    15         {
    16             carry += factical[i - 1][j] * i;
    17             factical[i][j] = carry % 10;
    18             carry /= 10;
    19         }
    20         while (carry)
    21         {
    22             factical[i][++length] = carry % 10;
    23             carry /= 10;
    24         }
    25         factical[i][0] = length;
    26     }
    27 }
    28 int main()
    29 {
    30     int m, n, count, length, carry, remainder, i, j, num;
    31     count = 0;
    32     work();
    33     while (scanf("%d %d", &m, &n) != EOF)
    34     {
    35         if (!m && !n)
    36             break;
    37         count++;
    38         //if m < n, the result is zero.
    39         if (m < n)
    40         {
    41             printf("Test #%d:\n", count);
    42             puts("0");
    43             continue;
    44         }
    45         num = n + m;
    46         carry = 0;
    47         length = factical[num][0];
    48         //multiple m + 1 - n
    49         for (i = 1; i <= length; i++)
    50         {
    51             carry += factical[num][i] * (m + 1 - n);
    52             result[i] = carry % 10;
    53             carry /= 10;
    54         }
    55         while (carry)
    56         {
    57             result[++length] = carry % 10;
    58             carry /= 10;
    59         }
    60         //divide m + 1
    61         remainder = 0;
    62         for (j = length; j > 0; j--)
    63         {
    64             remainder = remainder * 10 + result[j];
    65             result[j] = remainder / (m + 1);
    66             remainder %= (m + 1);
    67         }
    68         while (!result[length])
    69         {
    70             length--;
    71         }
    72         //formatting printing
    73         printf("Test #%d:\n", count);
    74         for (i = length; i > 0; i--)
    75         {
    76             printf("%d", result[i]);
    77         }
    78         puts("");
    79     }
    80     return 0;
    81 }

    Key points

    firstly, this's catalan question.

    secondly, the number of 200! is 375.

    thirdly, in some cases, the answer is zero, you must care about.

    last but not the least, if your code is wrong, you must check the answer step by step, you will be successful.

  • 相关阅读:
    解释器模式
    享元模式
    中介者模式
    职责链模式
    命令模式
    桥接模式
    Java迭代器Iterator
    [Swift]LeetCode1247. 交换字符使得字符串相同 | Minimum Swaps to Make Strings Equal
    [Swift]LeetCode1248. 统计「优美子数组」| Count Number of Nice Subarrays
    [Swift]LeetCode1239. 串联字符串的最大长度 | Maximum Length of a Concatenated String with Unique Characters
  • 原文地址:https://www.cnblogs.com/chuanlong/p/2757209.html
Copyright © 2011-2022 走看看