题意:对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少。 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人。 然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱。 给出一群朋友,没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和将收到他的礼物的人的列表,
请确定每个人收到的比送出的钱多的数目。
题解:就是每个人的钱相互分,算一下
代码:
/*
ID: lishicao
PROG: gift1
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
using namespace std ;
ifstream fin ( "gift1.in" ) ;
ofstream fout ( "gift1.out" ) ;
int main()
{
string name[15] ;
string temp ;
int N , i , j , k , cnt , Gift , num ;
int money[15] ;
memset( money , 0 , sizeof( money ) ) ;
fin >> N ;
for( i = 0 ; i < N ; i ++ )
fin >> name[i] ;
for( i = 0 ; i < N ; i ++ )
{
fin >> temp ;
for( j = 0 ; j < N ; j ++ )
if( temp == name[j] ) break ;
fin >> Gift >> num ;
if( num > 0 ) {
Gift = ( Gift / num ) * num ;
money[j] -= Gift ;
}
for( k = 0 ; k < num ; k ++ )
{
fin >> temp ;
for( cnt = 0 ; cnt < N ; cnt ++ )
if( temp == name[cnt] ) break ;
money[cnt] += Gift / num ;
}
}
for( i = 0 ; i < N ; i ++ )
fout << name[i] << " " << money[i] << endl ;
return 0 ;
}
官方题解:
The hardest part about this problem is dealing with the strings representing people's names.
We keep an array of Person structures that contain their name and how much money they give/get.
The heart of the program is the lookup() function that, given a person's name, returns their Person structure. We add new people with addperson().
Note that we assume names are reasonably short.
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define MAXPEOPLE 10
#define NAMELEN 32
typedef struct Person Person;
struct Person {
char name[NAMELEN];
int total;
};
Person people[MAXPEOPLE];
int npeople;
void
addperson(char *name)
{
assert(npeople < MAXPEOPLE);
strcpy(people[npeople].name, name);
npeople++;
}
Person*
lookup(char *name)
{
int i;
/* look for name in people table */
for(i=0; i<npeople; i++)
if(strcmp(name, people[i].name) == 0)
return &people[i];
assert(0); /* should have found name */
}
int
main(void)
{
char name[NAMELEN];
FILE *fin, *fout;
int i, j, np, amt, ng;
Person *giver, *receiver;
fin = fopen("gift1.in", "r");
fout = fopen("gift1.out", "w");
fscanf(fin, "%d", &np);
assert(np <= MAXPEOPLE);
for(i=0; i<np; i++) {
fscanf(fin, "%s", name);
addperson(name);
}
/* process gift lines */
for(i=0; i<np; i++) {
fscanf(fin, "%s %d %d", name, &amt, &ng);
giver = lookup(name);
for(j=0; j<ng; j++) {
fscanf(fin, "%s", name);
receiver = lookup(name);
giver->total -= amt/ng;
receiver->total += amt/ng;
}
}
/* print gift totals */
for(i=0; i<np; i++)
fprintf(fout, "%s %d\n", people[i].name, people[i].total);
exit (0);
}
分享到:
相关推荐
USACO题目,Greedy Gift Givers
2 [1.1] 贪婪的送礼者Greedy Gift Givers 3 [1.1] 黑色星期五Friday the Thirteenth 4 [1.1] 坏掉的项链 Broken Necklace 5 [1.2] 命名那个数字 Name That Number 6 [1.2] 挤牛奶Milking Cows 7 [1.2] 方块转换 ...
usaco 2005年比赛的解题报告以及测试数据
包括usaco2004年比赛的解题报告以及测试数据
包括usaco2003年比赛的解题报告及测试数据
包括usaco2003年比赛的解题报告及测试数据
usaco 合集,包括英文原题和中文译题,测试数据以及答案,很全啊!usaco 合集usaco 合集usaco 合集usaco 合集
usaco5.2解题报告1
usaco2.1解题报告1
usaco2.3解题报告1
usaco1.3解题报告1
usaco2.4解题报告1
usaco解题报告,就是usaco.training.gateway上面的题目全解
usaco历年测试数据
usaco 2010-2011 nov news,喜欢usaco的朋友可以看看
某些USACO题目的答案,很详细,代码清晰结构良好,算法高效易于调试
USACO题集及答案
usaco的总结和心得 包括了对题目的分了和总结 以及对题目的解法概括
usaco的解题报告和标程,有翻译,还算不错的资源
Usaco 2007 Open 的解题报告和测试数据