北大POJ百练做题记录

1002:方便记忆的电话号码

#include <cstdio>
#include <cstring>
#include <map>
#include <iostream>
using namespace std;
int num[26] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9};

int parase(char c) {
    if (c >= '0' && c <= '9')
        return c-'0';
    if (c >= 'A' && c <= 'Z')
        return num[c-'A'];
    if (c >= 'a' && c <= 'z')
        return num[c-'a'];
}

int main() {
    int n;
    scanf("%d", &n);
    char str[300];
    map<int, int> m;
    while (n--) {
        scanf("%s", str);
        int len = strlen(str);
        int tel = 0;
        for (int i = 0; i < len; i++) {
            if (str[i] == '-')
                continue;
            tel = tel * 10 + parase(str[i]);
        }
        if (!m.count(tel)) {
            m[tel] = 1;
        } else {
            m[tel]++;
        }
    }
    bool flag = true;
    map<int, int>::iterator it = m.begin();
    for (; it != m.end(); it++) {
        if (it->second > 1) {
            flag = false;
            printf("%03d-%04d %d\n", it->first/10000, it->first%10000, it->second);
        }

    }
    if (flag)
        printf("No duplicates.\n");
    return 0;
}

Hangover

#include <cstdio>
int N[1000];
void precess() {
    double sum = 1/2;
    int n = 1;
    for (int i = 2; n <= 521; i++) {
        while (float(n)/100 < sum) {
            N[n++] = i-2;
        }
        sum += 1/float(i);
    }
}
int main () {
    double x;
    precess();
    while (scanf("%lf", &x)) {
        if (x == 0)
            break;
        int idx = x * 100;
        printf("%d card(s)\n", N[idx]);
    }
    return 0;
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页