teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2020/04/01 10:01

投稿

asm
asm

スコア15149

answer CHANGED
@@ -13,4 +13,71 @@
13
13
  };
14
14
  ```
15
15
 
16
- みたいな感じにしてやった方が読みやすいと思います。
16
+ みたいな感じにしてやった方が読みやすいと思います。
17
+
18
+
19
+ ---
20
+
21
+ ファイル入出力を省略し標準入出力にて代替すると
22
+
23
+ ```c++
24
+ #include <iostream>
25
+ #include <fstream>
26
+ #include <string>
27
+ #include <sstream>
28
+ #include <vector>
29
+
30
+
31
+ using namespace std;
32
+ using P = pair<int,int>;
33
+
34
+ class Student {
35
+ string name;
36
+ string code;
37
+
38
+ vector<P> scores;
39
+ public:
40
+ Student(istream& fin);
41
+ void print(ostream& fout) const;
42
+ };
43
+
44
+ Student::Student(istream& fin){
45
+ string line;
46
+
47
+ // name & code
48
+ getline(fin, line);
49
+ auto sep = line.rfind(" ");
50
+ name = line.substr(0, sep);
51
+ code = line.substr(sep+1);
52
+
53
+ // scores
54
+ for(getline(fin, line); !line.empty(); getline(fin, line)) {
55
+ istringstream ss(line);
56
+ int score, num;
57
+ ss >> score >> num;
58
+ scores.emplace_back(score, num);
59
+ }
60
+ };
61
+
62
+ void Student::print(ostream& fout) const {
63
+ fout << name << " " << code << " ";
64
+
65
+ long long total = 0;
66
+ double total_weight = 0;
67
+ for(const auto& [score, weight] : scores){
68
+ total += score * weight;
69
+ total_weight += weight;
70
+ }
71
+
72
+ fout << total / total_weight << "\n";
73
+ }
74
+
75
+ int main(){
76
+ vector<Student> s;
77
+ while(cin){
78
+ s.emplace_back(cin);
79
+ }
80
+
81
+ for(const auto& p : s) p.print(cout);
82
+ }
83
+ ```