24h購物| | PChome| 登入
2012-05-13 08:26:55| 人氣1,116| 回應0 | 上一篇 | 下一篇

[UVA][Math] 10338 - Mischievous Children

推薦 0 收藏 0 轉貼0 訂閱站台

Problem C

Mischievous Children

Input: standard input

Output: standard output

Time Limit: 1 second

Memory Limit: 32 MB

 

Adam’sparents put up a sign that says “CONGRATULATIONS”. The sign is so big thatexactly one letter fits on each panel. Some of Adam’s younger cousins got boredduring the reception and decided to rearrange the panels. How many unique wayscan the panels be arranged (counting the original arrangement)?

 

Input / Output

 

Thefirst line of input is a single non-negative integer. It indicates the numberof data sets to follow. Its value will be less than 30001.

 

Eachdata set consists of a single word, in all capital letters. For each word,output the number of unique ways that the letters can be rearranged (countingthe original arrangement). Use the format shown in Sample Output, below.

 

Eachword will have at most 20 letters. There will be no spaces or otherpunctuation.

 

Thenumber of arrangements will always be able to fit into an unsigned long int. Note that 12! is thelargest factorial that can fit into an unsignedlong int.

 

 

Sample

 

Sample Input

 

3

HAPPY

WEDDING

ADAM

 

Sample Output

 

Data set 1: 60

Data set 2: 2520

Data set 3: 12



#include <stdio.h>
#include <string.h>

int main() {
    int t, Case = 0, i;
    char str[20];
    long long f[22] = {1, 1};
    for(i = 2; i <= 20; i++)
        f[i] = f[i-1]*i;
    scanf("%d", &t);
    while(t--) {
        scanf("%s", str);
        int letter[26] = {}, len = strlen(str);
        for(i = 0; str[i]; i++)
            letter[str[i]-'A']++;
        long long ans = f[len];
        for(i = 0; i < 26; i++)
            ans /= f[letter[i]];
        printf("Data set %d: %lld\n", ++Case, ans);
    }
    return 0;
}

台長: Morris
人氣(1,116) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Math] 1264 - Binary Search Tree
此分類上一篇:[UVA] 615 - Is It A Tree?

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文