记录编号 33462 评测结果 AAAATTTTTT
题目名称 韩国明星 最终得分 40
用户昵称 Gravatarfanzeyi 是否通过 未通过
代码语言 C 运行时间 13.212 s
提交时间 2011-11-10 19:18:16 内存使用 10.16 MiB
显示代码纯文本
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100010

typedef struct {
    char name[100];
    int heart;
} People;

People star[MAX];
int n, p;

unsigned int SDBMHash(char *str) {
    unsigned int hash = 0;
    while (*str){
        // equivalent to: hash = 65599*hash + (*str++);
        hash = (*str++) + (hash << 6) + (hash << 16) - hash;
    }
    return (hash & 0x7FFFFFFF);
}

int cmp(const void *a, const void *b) {
    People *c = (People*)a;
    People *d = (People*)b;
    return d->heart - c->heart;
}

void ChangeHeart(char *s, int change) {
    int i;
    for(i = 0; i < n; i++) {
        if(strcmp(star[i].name, s) == 0) {
            star[i].heart += change;
            return;
        }
    }
}

int main(int argc, char const *argv[]) {
    FILE *fin = fopen("star.in", "r");
    FILE *fout = fopen("star.out", "w");
    int i;
    int change;
    char tmp[100];
    memset(star, 0, sizeof(star));
    fscanf(fin, "%d", &n);
    for(i = 0; i < n; i++) {
        fscanf(fin, "%s", star[i].name);
    }
    fscanf(fin, "%d", &p);
    for(i = 0; i < p; i++) {
        fscanf(fin, "%s\n%d", tmp, &change);
        ChangeHeart(tmp, change);
    }
    qsort(star, n, sizeof(star[0]), cmp);
    for(i = 0; i < n; i++) {
        fprintf(fout, "%s\n%d\n", star[i].name, star[i].heart);
    }
    return 0;
}