20111012

ACM 10008 What's Cryptanalysis?

還沒想到好的方法……
利用一個array[26]從0到25分別對應A(a)到Z(z),
初始為0。
每次從stdin接收一個字母後,對應該字母的element + 1。
輸出時,從array[0]到array[25]找最大的element,
輸出之,輸出後把該element歸0。反覆,到所有elements為0為止。

/* ACM 10008
 * mythnc
 * 2011/10/13 09:10:55   
 * run time = 0.004
 */
#include <stdio.h>
#include <ctype.h>

int printout(int *s);

int main(void)
{
    int c;
    int alpha[26] = {0};

    scanf("%*d");
    while ((c = getchar()) != EOF)
        if (isupper(c))
            alpha[c - 'A']++;
        else if (islower(c))
            alpha[c - 'a']++;
    while (printout(alpha))
        ;
    return 0;
}

/* printout: print array s from high to low sequence */
int printout(int *s)
{
    int i, j, max;

    max = 0;
    for (i = 0; i < 26; i++)
        if (max < s[i]) {
            max = s[i];
            j = i;
        }
    if (max == 0)       /* if no letter have to be outputed */
        return 0;
    printf("%c %d\n", 'A' + j, max);
    s[j] = 0;
    return 1;
}

沒有留言: