-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgift1.cpp
85 lines (70 loc) · 1.54 KB
/
gift1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
ID: wxx54331
PROG: gift1
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
struct personStruct {
string name;
int initialMoney;
int finalMoney;
};
ifstream fin("gift1.in");
int getIndex(string name, vector<personStruct> person, int number);
int distribute(vector<personStruct> &person);
int main()
{
ofstream fout("gift1.out");
vector<personStruct> person;
personStruct onePerson;
string name;
int number;
fin >> number;
fin.get(); //读掉换行符
for(int i = 0; i < number; ++i)
{
getline(fin,name);
onePerson.name = name;
onePerson.initialMoney = 0;
onePerson.finalMoney = 0;
person.push_back(onePerson);
}
while(distribute(person))
;
for(int i = 0; i < number; ++i)
fout << person[i].name << " " << person[i].finalMoney - person[i].initialMoney << endl;
return 0;
}
int getIndex(string name, vector<personStruct> person)
{
for(int i = 0; i < person.size(); ++i)
if(name == person[i].name)
return i;
}
int distribute(vector<personStruct> &person)
{
string name;
int initialMoney, senderNum;
int index;
if(!getline(fin,name))
return 0;
fin >> initialMoney >> senderNum;
fin.get();
index = getIndex(name, person);
person[index].initialMoney += initialMoney;
if(senderNum == 0)
person[index].finalMoney += initialMoney;
else
person[index].finalMoney += initialMoney % senderNum;
for(int i = 0; i < senderNum; ++i)
{
getline(fin, name);
index = getIndex(name, person);
person[index].finalMoney += initialMoney / senderNum;
}
return 1;
}