小白题,也没啥好说的。关键我的算法感觉特别菜的一点是每次要遍历数组从人名找对应的编号,这个效率就很低了。看了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[];
int intial_money;
int final_money;
}P;
P p[];
int np;
int find_pos(char person[])
{
for(int i =;i < np;i++)
{
//cout << person<<endl;
if(strcmp(person,p[i].name) == )
return i;
}
return -;
}
int main()
{
ifstream cin("gift1.in");
ofstream cout("gift1.out"); cin >> np;
for(int i = ;i <np;i ++)
{
cin >> p[i].name;
}
for(int i = ;i < np;i++)
{
char giver[];
int giver_money,receivers_num,each_money,giver_pos,receivers_pos;
char receiver[]; cin >> giver; giver_pos = find_pos(giver); cin >> p[giver_pos].intial_money;
cin >> receivers_num; if(receivers_num != ){
each_money = p[giver_pos].intial_money / receivers_num;
p[giver_pos].final_money -= each_money * receivers_num; for(int j = ;j < receivers_num;j++)
{
cin >> receiver;
receivers_pos = find_pos(receiver);
p[receivers_pos].final_money += each_money;
}
}
}
for(int i = ;i <np;i ++)
cout << p[i].name <<" "<<p[i].final_money<<endl;
}