Section 1.1 Greedy Gift Givers
Greedy Gift Givers
A group of NP (2 ≤ NP ≤ 10) uniquely named friends hasdecided to exchange gifts of money. Each of these friends might ormight not give some money to any or all of the other friends.Likewise, each friend might or might not receive money from any orall of
the other friends. Your goal in this problem is to deduce how much more money each person gives than they receive.
The rules for gift-giving are potentially different than youmight expect. Each person sets aside a certain amount of money togive and divides this money evenly among all those to whom he orshe is giving a gift. No fractional money is available, so dividing3
among 2 friends would be 1 each for the friends with 1 left over-- that 1 left over stays in the giver's "account".
In any group of friends, some people are more giving than others(or at least may have more acquaintances) and some people have moremoney than others.
Given a group of friends, no one of whom has a name longer than14 characters, the money each person in the group spends on gifts,and a (sub)list of friends to whom each person gives gifts, determinehow much more (or less) each person in the group gives than
theyreceive.
IMPORTANT NOTE
The grader machine is a Linux machine that uses standard Unixconventions: end of line is a single character often known as '\n'.This differs from Windows, which ends lines with two charcters,'\n' and '\r'. Do not let your program get trapped by this!
PROGRAM NAME: gift1
INPUT FORMAT
| Line 1: | The single integer, NP | |||
| Lines 2..NP+1: | Each line contains the name of a group member | |||
| Lines NP+2..end: | NP groups of lines organized like this:
|
SAMPLE INPUT (file gift1.in)
5
dave
laura
owen
vick
amr
dave
200 3
laura
owen
vick
owen
500 1
dave
amr
150 2
vick
owen
laura
0 2
amr
vick
vick
0 0
OUTPUT FORMAT
The output is NP lines, each with the name of a person followed bya single blank followed by the net gain or loss (final_money_value - initial_money_value) for that person. The names should be printed inthe same order they appear on line 2 of the input.
All gifts are integers. Each person gives the same integer amountof money to each friend to whom any money is given, and gives as muchas possible that meets this constraint. Any money not given is kept bythe giver.
SAMPLE OUTPUT (file gift1.out)
dave 302
laura 66
owen -359
vick 141
amr -150 此题关键在于如何处理字符串所表示人的名字上,就是如何通过一个名字找到此人的位置,下面写了find()函数 用于返回名字所在的位置/*
ID: ifayne1
LANG: C++
TASK: gift1
*/
#include <iostream>
#include <stdio.h>
#include <string> #define NP 11 using namespace std; struct _NP{
string name;
int have;
}N[NP]; int n; int find(string name)
{
int i;
for ( i=0; i<n; i++ )
{
if ( name == N[i].name ) break;
}
return i;
} void input()
{
int amount, divide;
string temp;
cin >> temp;
cin >> amount >> divide;
if ( divide != 0 )
{
N[find(temp)].have = N[find(temp)].have - (amount - amount % divide);
for ( int i=0; i<divide; i++ )
{
cin >> temp;
N[find(temp)].have += (amount / divide);
}
}
} int main()
{
freopen("gift1.in", "r", stdin);
freopen("gift1.out", "w", stdout);
int i;
cin >> n;
for ( i=0; i<n; i++ )
{
cin >> N[i].name;
N[i].have = 0;
}
for ( i=0; i<n; i++ )
input();
for ( i=0; i<n; i++ )
cout << N[i].name << " " << N[i].have << endl;
return 0;
}
Section 1.1 Greedy Gift Givers的更多相关文章
- USACO Section 1.1-2 Greedy Gift Givers
Greedy Gift Givers 贪婪的送礼者 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少. 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那 ...
- USACO Section 1.1 Greedy Gift Givers 解题报告
题目 问题描述 有若干个朋友,朋友之间可以选择互相赠送一些有价值的礼物.一个人可以选择将一部分钱分给若干个朋友,例如某人送给其他两个人钱,总共赠送3元,两个人平均分,原本应该是每人1.5元,但是只能取 ...
- USACO Training Section 1.1 贪婪的送礼者Greedy Gift Givers
P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers 题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一 ...
- USACO . Greedy Gift Givers
Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...
- 119 - Greedy Gift Givers
Greedy Gift Givers The Problem This problem involves determining, for a group of gift-giving frien ...
- 1.1.4 PROB Greedy Gift Givers
Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...
- Greedy Gift Givers
Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...
- 洛谷 P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers
贪婪的送礼者Greedy Gift Givers 难度:☆ Code: #include <iostream> #include <cstdio> #include <c ...
- usaco training <1.2 Greedy Gift Givers>
题面 Task 'gift1': Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided t ...
随机推荐
- JAVA上传与下载
java下载指定地址的文件 package com.test; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...
- django-xadmin中APScheduler的启动初始化
环境: python3.5.x + django1.9.x + xadmin-for-python3 APScheduler做为一个轻量级和使用量很多的后台任务计划(scheduler)包,可以方便 ...
- 【Android Developers Training】 80. 管理网络使用
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- commons-logging 结合 log4j, 初始化生命周期 初探
-------commons-logging---------- Log log=LogFactory.getLog(clazz); LogFactory这是个抽象日志工厂,更像个工具? 通过线程上下 ...
- 9.session的生命周期
1.创建 当客户端第一次访问某个jsp或者Servlet的时候,服务器会为当前会话创建一个SessionId,每次客户端向服务端发送请求的时候,都会将此SessionId携带过去,服务端会对此Sess ...
- accp8.0转换教材第11章JAjax加护扩展理解与练习
①杂记:前面有原生态JavaScript实现ajax这里又多了更简单的方法实现ajax ②$.get()方法的常用参数 参数 类型 说明 url String 必选,规定发送地址 data Plain ...
- C#开发移动应用系列(4.调用系统应用,以及第三方应用(调用与被调用))
前言 上篇文章地址: C#开发移动应用系列(1.环境搭建) C#开发移动应用系列(2.使用WebView搭建WebApp应用) C#开发移动应用系列(3.使用照相机扫描二维码+各种基础知识) 写完这篇 ...
- ionic 最简单的路由形式,头部固定,下面tab切换-------一个简单的单页切换起飞了
<ion-header-bar class="bar-dark" align-title="left"> <h1 class="ti ...
- .net core建站踩坑记录
系统:win10 VS版本:2017 .NET Core 版本: 1.1 零.读取配置文件 参考:http://www.tuicool.com/articles/QfYVBvi 此版本无需添加其他组件 ...
- Spring实现IoC的多种方式
目录 一.使用XML配置的方式实现IOC 二.使用Spring注解配置IOC 三.自动装配 四.零配置实现IOC 五.示例下载 控制反转IoC(Inversion of Control),是一种设计思 ...