【USACO】【Section1.1】Greedy Gift Givers
2013-11-23 12:19
375 查看
小白题,也没啥好说的。关键我的算法感觉特别菜的一点是每次要遍历数组从人名找对应的编号,这个效率就很低了。看了ANALYZE里面也是这样的。不过它比我好的一点是我多余设置了initial_money变量,确实是不需要的。还有一点就是有可能接受礼物的人是0个,要判断。如果是0就不用60~67行了,而且60行的分母也会为0;
/*ID:Moment1991 PROG:gift1 LANG:C++ Test 1: TEST OK [0.000 secs, 3504 KB] Test 2: TEST OK [0.000 secs, 3504 KB] Test 3: TEST OK [0.000 secs, 3504 KB] Test 4: TEST OK [0.000 secs, 3504 KB] Test 5: TEST OK [0.000 secs, 3504 KB] Test 6: TEST OK [0.000 secs, 3504 KB] Test 7: TEST OK [0.000 secs, 3504 KB] Test 8: TEST OK [0.000 secs, 3504 KB] Test 9: TEST OK [0.000 secs, 3504 KB] */ #include <iostream> #include <fstream> #include <string.h> using namespace std; typedef struct PERSON{ char name[15]; int intial_money; int final_money; }P; P p[10]; int np; int find_pos(char person[]) { for(int i =0;i < np;i++) { //cout << person<<endl; if(strcmp(person,p[i].name) == 0) return i; } return -1; } int main() { ifstream cin("gift1.in"); ofstream cout("gift1.out"); cin >> np; for(int i = 0;i <np;i ++) { cin >> p[i].name; } for(int i = 0;i < np;i++) { char giver[15]; int giver_money,receivers_num,each_money,giver_pos,receivers_pos; char receiver[15]; cin >> giver; giver_pos = find_pos(giver); cin >> p[giver_pos].intial_money; cin >> receivers_num; if(receivers_num != 0){ each_money = p[giver_pos].intial_money / receivers_num; p[giver_pos].final_money -= each_money * receivers_num; for(int j = 0;j < receivers_num;j++) { cin >> receiver; receivers_pos = find_pos(receiver); p[receivers_pos].final_money += each_money; } } } for(int i = 0;i <np;i ++) cout << p[i].name <<" "<<p[i].final_money<<endl; }
相关文章推荐
- USACO-Section1.1 Greedy Gift Givers
- USACO - Chapter1 Section 1.1 - Greedy Gift Givers
- USACO Section1.1 Greedy Gift Givers
- USACO Section 1.1 Greedy Gift Givers
- USACO-Section1.1 Greedy Gift Givers [hash表]
- USACO Section 1.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:Section 1.1 Greedy Gift Givers
- USACO Section 1.1 Greedy Gift Givers
- USCAO-Section1.1 Greedy Gift Givers
- USACO-Section1.1 Greedy Gift Givers
- USACO Section 1.1: Greedy Gift Givers
- 【USACO1.1_2】★Greedy Gift Givers 贪婪的礼物送礼者
- USACO-Greedy Gift Givers(贪婪的送礼者)-Section1.2<2>
- usaco 1.1 greedy gift givers
- USACO 1.1 Greedy Gift Givers
- Section 1.1 Greedy Gift Givers