跳至正文

USACO 1.1.2 Greedy Gift Givers

  • OI路程

  这个题目唯一要注意的就是被除数不为0的问题,别的的话就是别太粗心就是,代码如下:

/
LANG: C
ID: logoo2
PROG: gift1
/
#include <stdio.h>
#include <string.h>
struct peo{
        char name[15];
        int get, give;
}pep[10];
int n;

int getid(char str)
{
        int i;
        for(i = 0; i < n; i++){
                if(!strcmp(str, pep[i].name)){
                        return i;
                }
        }
        return –1;
}

int main(void)
{
        int i, j;
        char name[15], tmp[15];
        int t, s, m, av;
        freopen("gift1.in""r"stdin);
        freopen("gift1.out""w"stdout);
        scanf("%d\n", &n);
        for(i = 0; i < n; i++){
                scanf("%s\n", pep[i].name);
        }
        while(scanf("%s\n", name) == 1){
                t = getid(name);
                scanf("%d%d\n", &m, &s);
                if(s == 0){
                        av = 0;
                }else{
                        av = m / s;
                }
                pep[t].get += m – av
s;
                pep[t].give = m;
                for(i = 0; i < s; i++){
                        scanf("%s", tmp);
                        t = getid(tmp);
                        pep[t].get += av;
                }
        }
        for(i = 0; i < n; i++){
                printf("%s %d\n", pep[i].name, pep[i].get – pep[i].give);
        }
        return 0;
}

发表回复

您的电子邮箱地址不会被公开。