ผลต่างระหว่างรุ่นของ "01204212/scores"
ไปยังการนำทาง
ไปยังการค้นหา
Jittat (คุย | มีส่วนร่วม) (→Input) |
Jittat (คุย | มีส่วนร่วม) (→Codes) |
||
แถว 37: | แถว 37: | ||
== Codes == | == Codes == | ||
+ | You should modify the hash table code from [[01204212/spreadsheet]]. Use the main class below. | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | package adt_ex_w12_htablescores0; | ||
+ | import java.io.BufferedReader; | ||
+ | import java.io.IOException; | ||
+ | import java.io.InputStreamReader; | ||
+ | import java.util.HashMap; | ||
+ | import java.util.Map; | ||
+ | |||
+ | public class Main { | ||
+ | |||
+ | HashTable table; | ||
+ | |||
+ | public static void main(String[] args) throws IOException { | ||
+ | Main m = new Main(); | ||
+ | |||
+ | m.process(); | ||
+ | } | ||
+ | |||
+ | Main() { | ||
+ | table = new HashTable(); | ||
+ | } | ||
+ | |||
+ | private void process() throws IOException { | ||
+ | BufferedReader reader = new BufferedReader( | ||
+ | new InputStreamReader(System.in) ); | ||
+ | |||
+ | int m = Integer.parseInt(reader.readLine()); | ||
+ | |||
+ | for(int i=0; i<m; i++) { | ||
+ | String[] items = reader.readLine().split(" "); | ||
+ | int additionalScore = Integer.parseInt(items[1]); | ||
+ | int currentScore = table.get(items[0]) + additionalScore; | ||
+ | |||
+ | System.out.println(currentScore); | ||
+ | table.put(items[0],currentScore); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> |
รุ่นแก้ไขเมื่อ 23:00, 23 พฤศจิกายน 2559
- This is part of 01204212
Write a program that maintains scores for each team in a world-wide game competition.
Your program will read a list of names and additional scores and print out the current score of that team. Initially, every team has score of 0.
เนื้อหา
Input
First line: an integer M, the total number of updates (M <= 1,000,000) The number of teams will not be more than 100,000.
Next 'M lines: a team name which is a string consisting of 'A'-'Z','a'-'z','0'-'9' and '_' of length at most 20 and an additional score which is an integer of value at most 1,000.
Output
For each line of the input, output the current score of that team.
Examples
Input
5 hello 10 world 15 hello 123 Hello 15 Hello-world 100
Output
10 15 133 15 100
Codes
You should modify the hash table code from 01204212/spreadsheet. Use the main class below.
package adt_ex_w12_htablescores0;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Main {
HashTable table;
public static void main(String[] args) throws IOException {
Main m = new Main();
m.process();
}
Main() {
table = new HashTable();
}
private void process() throws IOException {
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in) );
int m = Integer.parseInt(reader.readLine());
for(int i=0; i<m; i++) {
String[] items = reader.readLine().split(" ");
int additionalScore = Integer.parseInt(items[1]);
int currentScore = table.get(items[0]) + additionalScore;
System.out.println(currentScore);
table.put(items[0],currentScore);
}
}
}