USACO 1.1.2 Greedy Gift Givers(gift1)
这道题大意就是几个人互送礼物,让你求每个人的盈利。
原题给的样例数据:
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
这题使用模拟算法就行了,就是注意输入的时候不要掉坑里了。
还有就是比如说自己有200快钱给3个人,每个人得到66块,剩下两块自己收,并计入答案。
还有就是想题目里的vick,分给0个人,千万不要除以了0。。对了为了找人名方便,可以把一个人的名字和编号存到一个map容器中,具体在代码里已经有了,就不细说了。
下面是我的程序:
/*
ID:aaabbbr1
LANG:C++
TASK:gift1
*/
#include <stdio.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;
struct person
{
string name;
int gave[10],have,cnt,receive;
person(){have=receive=0;}
}a[10];
map<string,int>link;//这只是方便从人名找到编号而已
void gavemoney(int x)
{
if(a[x].cnt==0)return;//小心除以0了(也就是如果给的人数为0就不给了)
int per=a[x].have/a[x].cnt;//计算每人多少钱
int tot=per*a[x].cnt;//计算一共给多少钱
a[x].receive+=a[x].have-tot;//计算自己的零钱 并汇入收到的钱里面
for(int i=0;i<a[x].cnt;i++)//给钱
a[ a[x].gave[i] ].receive += per;
}
void display(int x)
{
cout << a[x].name << " " << a[x].receive-a[x].have << "\n";
}
int main()
{
freopen("gift1.in","r",stdin);
freopen("gift1.out","w",stdout);
string tmp;
int n,x,t1,t2;
cin >> n;
for(int i=0;i<n;i++)
{
cin >> tmp;
a[i].name=tmp;
link[tmp]=i;
}
for(int i=0;i<n;i++)
{
cin >> tmp;
x=link[tmp];
cin >> t1 >> t2;
a[x].have=t1;
a[x].cnt=t2;
for(int j=0;j<a[x].cnt;j++)
{
cin >> tmp;
a[x].gave[j]=link[tmp];
}
}
for(int i=0;i<n;i++)
gavemoney(i);
for(int i=0;i<n;i++)
display(i);
fclose(stdin);
fclose(stdout);
return 0;
}
USACO 1.1.2 Greedy Gift Givers(gift1)的更多相关文章
- USACO Section 1.1-2 Greedy Gift Givers
Greedy Gift Givers 贪婪的送礼者 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少. 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那 ...
- 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 ...
- 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要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一 ...
- Java实现【USACO】1.1.2 贪婪的礼物送礼者 Greedy Gift Givers
[USACO]1.1.2 贪婪的礼物送礼者 Greedy Gift Givers 题目描述 对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少(and vice versa for th ...
- USACO . Greedy Gift Givers
Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...
- Greedy Gift Givers 贪婪的送礼者 USACO 模拟
1002: 1.1.2 Greedy Gift Givers 贪婪的送礼者 时间限制: 1 Sec 内存限制: 128 MB提交: 9 解决: 9[提交] [状态] [讨论版] [命题人:外部导入 ...
- Section 1.1 Greedy Gift Givers
Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends hasdecided to exchange gifts o ...
- USACO-Greedy Gift Givers(贪婪的送礼者)-Section1.2<2>
[英文原题] Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange ...
随机推荐
- [Chapter 3 Process]Practice 3.2 Including the initial parent process, how many processes are created by the program shown in Figure?
3.2 Including the initial parent process, how many processes are created by the program shown in Fig ...
- 将chrome浏览器的默认背景颜色修改为浅绿色,以减缓长时间看电脑的眼睛不舒服的问题
修改chrome文件夹中的Custom.css, 此文件里面默认内容是空的. 在其中添加下面这段代码: 你也可以选择自己的喜欢的颜色, 前提是你知道你想要更改的颜色的十六进制颜色值, 例如:#CCEB ...
- Word中调整编号和文字的间距
鼠标放在节文字上,不用选择该级别的所有节点,直接在某一节上右键-段落-制表位-默认制表位-设置1字符或其它.完成后该级别所有节的格式都自动调整,不用一个个调整. 但是设置其它段落格式还是需要在菜单上选 ...
- findall 、 search的使用
- ActiveMQ (三) Spring整合JMS入门
Spring整合JMS入门 前提:安装好了ActiveMQ ActiveMQ安装 Demo结构: 生产者项目springjms_producer: pom.xml <?xml versio ...
- day70-oracle 12-Java调用存储过程和存储函数
我们现在调用的是存储过程和存储函数.用CallableSatement调用存储函数和存储过程. RDBMS:关系数据库.使用标准方式调用存储过程.也就是说:在mysql中调用和在oracle中调用的写 ...
- css知多少(9)——float下篇(转)
css知多少(9)——float下篇 float内容比较多,咱们分上.下两篇来介绍,上篇已经写完,这是下篇.建议大家先把上篇看了,再来看下文,精彩内容不要掠过啊. 1. 清除float <上 ...
- vue-resource基础介绍
1.vue-resource 的请求api是按照rest风格设计的,它提供了7种请求api get(url, [data], [options]); head(url,[data],[options] ...
- CoreData的增删改查
首先使用CoreData创建Demo,勾上CoreData选项 然后创建Entity对象,点击Add Entity(+)按钮 生成Entity对象 重命名双击Entity选项,然后输入Person 设 ...
- Entity Framework Tutorial Basics(21):CRUD Operation in Connected Scenario
CRUD Operation in Connected Scenario: CRUD operation in connected scenario is a fairly easy task bec ...