USACO-Section1.1 Greedy Gift Givers
来源:互联网 发布:seo一专员 编辑:程序博客网 时间:2024/05/23 21:37
2017-05-25
题目大意:
对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少。 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人。 然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱。 给出一群朋友,没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和将收到他的礼物的人的列表, 请确定每个人收到的比送出的钱多的数目。
题解:
将人抽象成了一个结构体,具有的资金,朋友的个数以及朋友的名字作为其属性,然后直接根据题目进行线性表查找,因为数据量比较小——人数NP,2<= NP<=10,所以不会超时。但是后面学习到了数据结构之后知道Hash表也是可以解决的,并且处理数据的范围也扩大了很多。代码是用结构体解决的问题。
代码:
#include<fstream> #include<cstring> #include<queue> using namespace std; struct peo{ int owMoney ,friMoney , numOfFri ; string name , friendName[15]; }; peo p[15]; int n; int main(){ ofstream cout("gift1.out"); ifstream cin("gift1.in"); cin >> n; for(int i = 1;i <= n;i++){ cin >> p[i].name; p[i].friMoney = 0; } string temp; int i,j,t; for(i = 1;i <= n;i++){ cin >> temp; for(j = 1;j <= n;j++){ if(p[j].name == temp) break; } cin >> p[j].owMoney >> p[j].numOfFri; for(t = 1;t <= p[j].numOfFri;t++){ cin >> p[j].friendName[t]; } } for(i = 1;i <= n;i++){ if(p[i].owMoney == 0) continue; for(j = 1;j <= p[i].numOfFri;j++){ for(t = 1;t <= n;t++){ if(p[i].friendName[j] == p[t].name){ break; } } p[t].friMoney += (int)(p[i].owMoney / p[i].numOfFri); } p[i].owMoney = -1 * (int)(p[i].owMoney / p[i].numOfFri) * p[i].numOfFri; } for(int i = 1;i <= n;i++){ cout << p[i].name <<" " <<p[i].owMoney+p[i].friMoney << endl; } return 0; }
阅读全文
0 0
- USACO Section1.1 Greedy Gift Givers
- USACO-Section1.1 Greedy Gift Givers【查找】
- USACO-Section1.1 Greedy Gift Givers
- USACO-Section1.1 Greedy Gift Givers
- USACO-Section1.1 Greedy Gift Givers
- USACO-Section1.1 Greedy Gift Givers[...]
- USACO Section1.1 Greedy Gift Givers
- USACO-Section1.1 Greedy Gift Givers [hash表]
- [USACO] Section1.1 Greedy Gift Givers[线性时间查找]
- USCAO-Section1.1 Greedy Gift Givers
- Greedy Gift Givers(USACO)
- usaco--Greedy Gift Givers
- [USACO] Greedy Gift Givers
- USACO-----Greedy Gift Givers
- USACO | Greedy Gift Givers
- USACO->Greedy Gift Givers
- usaco 1-1-2 Greedy Gift Givers
- USACO-cha1-sec1.1 Greedy Gift Givers
- 【学习】视觉SLAM资源集锦
- [Leetcode]_29 Divide Two Integers
- 数据压缩原理与应用 JPEG解码
- MySQL Internals Manual
- MYSQL的C API之mysql_query
- USACO-Section1.1 Greedy Gift Givers
- Android 基本优化(一)
- Codeforces 797E Array Queries
- Http 206 文件断点续传下载原理
- A. 这是一道简单的水题~
- HYSBZ 1831 逆序对
- linux编程之日志服务与日志编程
- 云打印服务器
- Cygwin 命令行包管理