algorithm problem 15 - andstudy/forge GitHub Wiki
-
์ฑ ์ ๋ ฅ๋ถ๋ถ์ ๋ํ ํ๊ธ ๋ฒ์ญ์ด ๋๋ฌด ์ ๋งค๋ชจํธํด์ ์ฐพ์๋ดค๋๋ฐ.
-
์ญ์ ๋ฒ์ญ์ด ์ด์ํ๊ฑฐ์๋ค์.
contestant, problem, time, L Sample Input: 1 1 2 10 I 3 1 11 C 1 2 19 R 1 2 21 C 1 1 25 C
- ํ ๋ฌธ์ ๋ ํ์ง ๋ชปํ ํ๋ ์ถ๋ ฅ์ด ๋์ด์ผ ํ๋์?
- ์ถ๋ ฅ์ํด๋ ๋ ๊ฒ ๊ฐ์ต๋๋ค. (display only the contestants that have made a submission.)
- ์ญ์ ์ด๊ฒ๋ ์๋ชป์๊ฐ. ๋ง์ถ๊ฑด ๋ชป๋ง์ถ๊ฑด ๊ฒฐ๊ณผ๋ฅผ submission ํ ํ์ ์ถ๋ ฅํด์ผํ ๋ฏ ํฉ๋๋ค.
- ์ฒซ๋ฒ์งธ ๋ฌธ์ ๋ ํ๊ณ ๋๋ฒ์งธ ๋ฌธ์ ๋ I๋ง ๋๋ฒ์ ๊ฒฐ๊ตญ ๋ชป ํ์๋ค๋ฉด ์ด ํ์ ๋ฒ์ ์?
-
๋๋ฒ์งธ ๋ฌธ์ ๋ฒ์ 40์ ์ด ์ต์ข ๊ฒฐ๊ณผ์ ์ถ๊ฐ๊ฐ ๋๋๋ ์๋๋ ๊ทธ๊ฒ์ด ๋ฌธ์ ๋ก์ธ...
-
์ถ๊ฐ๊ฐ ์ ๋ ๋ฏ ํ๋ค. ๋ค ํผ์ค ์์๋๋ ํ์ฐธ ๋จ์๊ตฐ.. ํ..
- ์ผ๋จ 1๋ฌธ์ ๋ ํ์์ผ๋ ์ถ๋ ฅ์ ํด์ผํ๋ฏ๋ก ์ฒซ๋ฒ์งธ๋ฌธ์ ์์ ๊ฑธ๋ฆฐ ์๊ฐ+40์ ์ ๋ฒ์ ์ด ์ถ๋ ฅ๋ ๋ฏ ํฉ๋๋ค.
-
(Each line of output will contain a contestant number, the number of problems solved by the contestant and the time penalty accumulated by the contestant)
-
ํ. ๋ฌธ์ ๋ฅผ ๋ค์ ์ฝ์ด๋ณด๋ ์๋๋ค์ ... Correct ํ ๋ฌธ์ ๊ฐ 1๊ฐ ์ด์์ผ๋๋ง ์ถ๋ ฅํ๋์ค ์์๋๋ฐ. ํ์ง๋ชปํ์ด๋ ์ ์ถ๋ง ํด๋ submission ๋ง ํด๋ ์ถ๋ ฅํ๋๊ฑฐ์๋ค์. I ์ผ๊ฒฝ์ฐ ๋์ค์ Correct ํ์๋๋ง ์ต์ข ๋ฒ์ ์๊ฐ์ ์ ์ฉํ๊ณ .....๋๋ฌด ๋ฆ์ด์ ์์ ์ ๋ด์ผ
-
TDD ๊ฐ ์ฝ์ง ์๋ค์. Test case ์์ฑ์ ํ๋ก๊ทธ๋จ ์์ฑํ ํด๋ฒ๋ ธ์ต๋๋ค ์์
- Test Driven์ ๊ณ ์งํ ํ์๋ ์์ต๋๋ค.
- Test Accelerated Development (["TAD"])๋ก ํ์๋ฉด ๋ฉ๋๋ค.
-
submission ํ๋ฉด ์ถ๋ ฅํด์ฃผ๊ณ C ์ผ๋๋ง I ์ ์๊ฐ๋ฒ์ ์ ๊ณ์ฐํ๋๋ก ์์ ํด๋ณด์์ต๋๋ค.
import java.io.*;
import java.util.*;
public class ScoreBoardMain
{
ArrayList<Contestant> ctList = new ArrayList<Contestant>();
public static void main(String[] args)
{
ScoreBoardMain sbMain = new ScoreBoardMain();
int testCase = Integer.parseInt(ReadLine());
ReadLine();
/* testCase ๋งํผ */
while(testCase != 0)
{
sbMain.InitScoreBoard();
sbMain.RunScoreBoard();
sbMain.PrintScoreBoard();
testCase--;
}
}
public void InitScoreBoard()
{
ctList.clear();
}
/* ๋น์นธ์ด ๋ค์ด์ฌ๋๊น์ง ์ ์ํ Parse */
public void RunScoreBoard()
{
String in;
while( (in = ReadLine()) != null )
{
if( in.equals("") )
break;
ParseInputData(in);
}
}
public void PrintScoreBoard()
{
for(int i=0; i<ctList.size(); i++)
{
System.out.println(ctList.get(i).GetTeamNum() + " "
+ ctList.get(i).GetSolvedProblemCount() + " "
+ ctList.get(i).GetTotalPenaltyTime());
}
}
/* teamNum, problemNum, time, L type ํ์ฑ */
public void ParseInputData(String in)
{
String[] token = new String[5];
token = in.split(" ");
// L type ์ ์๊ด์์ด ๋ฌด์กฐ๊ฑด contestant ์ถ๊ฐ
Contestant cont = GetExistContestant(token[0]);
if( cont == null )
{
cont = new Contestant(token[0]);
ctList.add(cont);
}
cont.SubmitProblem(token[1],token[2],token[3]);
}
/* ArrayList ์ ์กด์ฌํ๋ Contestant ์ธ์ง ์ฒดํฌ.. ๋ค๋ฅธ๋ฐฉ๋ฒ์ ์๋ ใ
ใ
*/
public Contestant GetExistContestant(String teamNum)
{
if( ctList.size() == 0)
return null;
for(int i=0; i< ctList.size(); i++)
{
if(ctList.get(i).GetTeamNum() == Integer.parseInt(teamNum))
return ctList.get(i);
}
return null;
}
public static String ReadLine()
{
String data = null;
try
{
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
data = is.readLine();
}
catch(IOException e)
{
System.out.println("IOException " +e);
}
return data;
}
}
import java.util.*;
public class Contestant
{
private int teamNum;
private int solvedProblemCount;
private int totalPenaltyTime;
/* HashMap <ํ๋ฒํธ,๋ฌธ์ ๋น๋ฒ์ > */
HashMap<String,Integer> tScoreMap = new HashMap<String,Integer>();
public Contestant(String teamNum)
{
this.teamNum = Integer.parseInt(teamNum);
}
public void SubmitProblem(String problemNumber,String time, String type)
{
totalPenaltyTime += ComputeTime(problemNumber,time,type);
}
// type ๋ณ๋ก Time ๊ณ์ฐ.
public int ComputeTime(String num, String time,String type)
{
int resultTime = 0;
if( type.equals("C") ) // Correct
{
solvedProblemCount++;
if(tScoreMap.get(num) != null)
resultTime += tScoreMap.get(num);
resultTime += Integer.parseInt(time);
}
if( type.equals("I") ) // Incorrect
{
if( tScoreMap.get(num) != null)
tScoreMap.put(num, tScoreMap.get(num) + 20);
else
tScoreMap.put(num,20);
}
/* ๋๋จธ์ง 3 case ๋ ์ฒ๋ฆฌ ํ ๊ฒ ์์.
if( type == "R" ) // clarification Request
if( type == "U" ) // Unjudged
if( type == "E" ) // Erroneous submission
*/
return resultTime;
}
public int GetTeamNum()
{
return teamNum;
}
public int GetSolvedProblemCount()
{
return solvedProblemCount;
}
public int GetTotalPenaltyTime()
{
return totalPenaltyTime;
}
}
import junit.framework.TestCase;
public class ContestantTest extends TestCase
{
public void testSubmitProblem()
{
/* test 1 */
Contestant c1 = new Contestant("1");
c1.SubmitProblem("2","10","I");
assertEquals("1-1",c1.GetTotalPenaltyTime(),0);
c1.SubmitProblem("2","19","R");
assertEquals("1-3",c1.GetTotalPenaltyTime(),0);
c1.SubmitProblem("2","21","C");
assertEquals("1-4",c1.GetTotalPenaltyTime(),41);
c1.SubmitProblem("1","25","C");
assertEquals("1-5",c1.GetTotalPenaltyTime(),66);
/* test 2 */
Contestant c2 = new Contestant("2");
c2.SubmitProblem("2","30","C");
assertEquals("2-1",c2.GetTotalPenaltyTime(),30);
c2.SubmitProblem("2","19","R");
assertEquals("2-2",c2.GetTotalPenaltyTime(),30);
c2.SubmitProblem("3","50","I");
assertEquals("2-3",c2.GetTotalPenaltyTime(),30);
c2.SubmitProblem("3","10","C");
assertEquals("2-4",c2.GetTotalPenaltyTime(),60);
/* test 3 */
Contestant c3 = new Contestant("1");
c3.SubmitProblem("5","10","I");
assertEquals("3-1",c3.GetTotalPenaltyTime(),0);
c3.SubmitProblem("4","19","R");
assertEquals("3-2",c3.GetTotalPenaltyTime(),0);
c3.SubmitProblem("3","21","I");
assertEquals("3-3",c3.GetTotalPenaltyTime(),0);
c3.SubmitProblem("2","25","I");
assertEquals("3-4",c3.GetTotalPenaltyTime(),0);
}
public void testComputeTime()
{
Contestant c = new Contestant("1");
assertEquals("1",c.ComputeTime("1","10","I"),0);
assertEquals("2",c.ComputeTime("2","10","I"),0);
assertEquals("3",c.ComputeTime("1","11","C"),31);
assertEquals("4",c.ComputeTime("1","19","R"),0);
assertEquals("5",c.ComputeTime("2","10","I"),0);
assertEquals("6",c.ComputeTime("2","10","C"),50);
}
}
import junit.framework.TestCase;
public class ScoreBoardMainTest extends TestCase
{
ScoreBoardMain sbMain = new ScoreBoardMain();
public void testRunScoreBoard()
{
}
public void testParseInputData()
{
/* test 1 */
sbMain.ParseInputData("1 2 10 I");
sbMain.ParseInputData("1 2 19 R");
sbMain.ParseInputData("1 2 21 C");
sbMain.ParseInputData("1 1 25 C");
assertEquals("1-1",sbMain.ctList.size(),1);
assertEquals("1-2",sbMain.ctList.get(0).GetTeamNum(),1);
assertEquals("1-3",sbMain.ctList.get(0).GetSolvedProblemCount(),2);
assertEquals("1-4",sbMain.ctList.get(0).GetTotalPenaltyTime(),66);
sbMain.ctList.clear();
/* test 2 */
sbMain.ParseInputData("4 2 30 C");
sbMain.ParseInputData("1 2 19 R");
sbMain.ParseInputData("4 2 50 U");
sbMain.ParseInputData("4 5 25 E");
sbMain.ParseInputData("4 3 22 E");
assertEquals("2-1",sbMain.ctList.size(),2);
assertEquals("2-2",sbMain.ctList.get(0).GetTeamNum(),4);
assertEquals("2-3",sbMain.ctList.get(0).GetSolvedProblemCount(),1);
assertEquals("2-4",sbMain.ctList.get(0).GetTotalPenaltyTime(),30);
assertEquals("2-5",sbMain.ctList.get(1).GetTeamNum(),1);
assertEquals("2-6",sbMain.ctList.get(1).GetSolvedProblemCount(),0);
assertEquals("2-7",sbMain.ctList.get(1).GetTotalPenaltyTime(),0);
sbMain.ctList.clear();
/* test 3 */
sbMain.ParseInputData("5 3 20 I");
sbMain.ParseInputData("5 4 15 R");
sbMain.ParseInputData("5 4 30 I");
sbMain.ParseInputData("5 3 30 C");
assertEquals("3-1",sbMain.ctList.size(),1);
assertEquals("3-2",sbMain.ctList.get(0).GetTeamNum(),5);
assertEquals("3-3",sbMain.ctList.get(0).GetSolvedProblemCount(),1);
assertEquals("3-4",sbMain.ctList.get(0).GetTotalPenaltyTime(),50);
sbMain.ctList.clear();
}
public void testGetExistContestant()
{
Contestant c1 = new Contestant("1");
Contestant c2 = new Contestant("2");
Contestant c3 = new Contestant("3");
sbMain.ctList.add(c1);
assertEquals("1",sbMain.GetExistContestant("1"),c1);
assertEquals("1",sbMain.GetExistContestant("2"),null);
sbMain.ctList.add(c2);
sbMain.ctList.add(c3);
assertEquals("1",sbMain.GetExistContestant("5"),null);
assertEquals("1",sbMain.GetExistContestant("3"),c3);
}
}
import java.io.*;
import java.util.*;
public class ScoreBoardMain
{
ArrayList<Contestant> ctList = new ArrayList<Contestant>();
public static void main(String[] args)
{
ScoreBoardMain sbMain = new ScoreBoardMain();
int testCase = Integer.parseInt(ReadLine());
ReadLine();
/* testCase ๋งํผ */
while(testCase != 0)
{
sbMain.InitScoreBoard();
sbMain.RunScoreBoard();
sbMain.PrintScoreBoard();
testCase--;
}
}
public void InitScoreBoard()
{
ctList.clear();
}
/* ๋น์นธ์ด ๋ค์ด์ฌ๋๊น์ง ์ ์ํ Parse */
public void RunScoreBoard()
{
String in;
while( (in = ReadLine()) != null )
{
if( in.equals("") )
break;
ParseInputData(in);
}
}
public void PrintScoreBoard()
{
for(Contestant cnt : ctList){
System.out.println(cnt);
}
}
/* teamNum, problemNum, time, L type ํ์ฑ */
public void ParseInputData(String in)
{
String[] token = new String[5];
token = in.split(" ");
// L type ์ ์๊ด์์ด ๋ฌด์กฐ๊ฑด contestant ์ถ๊ฐ
Contestant cont = GetExistContestant(token[0]);
if( cont == null )
{
cont = new Contestant(token[0]);
ctList.add(cont);
}
cont.SubmitProblem(token[1],token[2],token[3]);
}
/* ArrayList ์ ์กด์ฌํ๋ Contestant ์ธ์ง ์ฒดํฌ.. ๋ค๋ฅธ๋ฐฉ๋ฒ์ ์๋ ใ
ใ
*/
public Contestant GetExistContestant(String teamNum)
{
if( ctList.size() == 0)
return null;
for(int i=0; i< ctList.size(); i++)
{
if(ctList.get(i).GetTeamNum() == Integer.parseInt(teamNum))
return ctList.get(i);
}
return null;
}
public static String ReadLine()
{
String data = null;
try
{
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
data = is.readLine();
}
catch(IOException e)
{
System.out.println("IOException " +e);
}
return data;
}
public void SortDataInput()
{
Contestant [] cnts = new Contestant[ctList.size()];
cnts = ctList.toArray(cnts);
Arrays.sort(cnts);
ctList = new ArrayList<Contestant>();
for(Contestant row : cnts){
ctList.add(row);
}
}
}
import java.util.*;
public class Contestant implements Comparable<Contestant>
{
private int teamNum;
private int solvedProblemCount;
private int totalPenaltyTime;
/* HashMap <ํ๋ฒํธ,๋ฌธ์ ๋น๋ฒ์ > */
HashMap<String,Integer> tScoreMap = new HashMap<String,Integer>();
public Contestant(String teamNum)
{
this.teamNum = Integer.parseInt(teamNum);
}
public void SubmitProblem(String problemNumber,String time, String type)
{
totalPenaltyTime += ComputeTime(problemNumber,time,type);
}
// type ๋ณ๋ก Time ๊ณ์ฐ.
public int ComputeTime(String num, String time,String type)
{
int resultTime = 0;
if( type.equals("C") ) // Correct
{
solvedProblemCount++;
if(tScoreMap.get(num) != null)
resultTime += tScoreMap.get(num);
resultTime += Integer.parseInt(time);
}
if( type.equals("I") ) // Incorrect
{
if( tScoreMap.get(num) != null)
tScoreMap.put(num, tScoreMap.get(num) + 20);
else
tScoreMap.put(num,20);
}
/* ๋๋จธ์ง 3 case ๋ ์ฒ๋ฆฌ ํ ๊ฒ ์์.
if( type == "R" ) // clarification Request
if( type == "U" ) // Unjudged
if( type == "E" ) // Erroneous submission
*/
return resultTime;
}
public int GetTeamNum()
{
return teamNum;
}
public int GetSolvedProblemCount()
{
return solvedProblemCount;
}
public int GetTotalPenaltyTime()
{
return totalPenaltyTime;
}
public int compareTo(Contestant o) {
if(this.solvedProblemCount > o.GetSolvedProblemCount())
return -1;
else if( this.solvedProblemCount < o.GetSolvedProblemCount())
return 1;
else if( this.totalPenaltyTime > o.GetTotalPenaltyTime())
return -1;
else if(this.totalPenaltyTime < o.GetTotalPenaltyTime())
return 1;
return 0;
}
public String toString(){
return teamNum + " " + solvedProblemCount + " " + totalPenaltyTime;
}
}
import junit.framework.TestCase;
public class ScoreBoardMainTest extends TestCase
{
ScoreBoardMain sbMain = new ScoreBoardMain();
public void testRunScoreBoard()
{
}
public void testParseInputData()
{
/* test 1 */
sbMain.ParseInputData("1 2 10 I");
sbMain.ParseInputData("1 2 19 R");
sbMain.ParseInputData("1 2 21 C");
sbMain.ParseInputData("1 1 25 C");
assertEquals("1-1",sbMain.ctList.size(),1);
assertEquals("1-2",sbMain.ctList.get(0).GetTeamNum(),1);
assertEquals("1-3",sbMain.ctList.get(0).GetSolvedProblemCount(),2);
assertEquals("1-4",sbMain.ctList.get(0).GetTotalPenaltyTime(),66);
sbMain.ctList.clear();
/* test 2 */
sbMain.ParseInputData("4 2 30 C");
sbMain.ParseInputData("1 2 19 R");
sbMain.ParseInputData("4 2 50 U");
sbMain.ParseInputData("4 5 25 E");
sbMain.ParseInputData("4 3 22 E");
assertEquals("2-1",sbMain.ctList.size(),2);
assertEquals("2-2",sbMain.ctList.get(0).GetTeamNum(),4);
assertEquals("2-3",sbMain.ctList.get(0).GetSolvedProblemCount(),1);
assertEquals("2-4",sbMain.ctList.get(0).GetTotalPenaltyTime(),30);
assertEquals("2-5",sbMain.ctList.get(1).GetTeamNum(),1);
assertEquals("2-6",sbMain.ctList.get(1).GetSolvedProblemCount(),0);
assertEquals("2-7",sbMain.ctList.get(1).GetTotalPenaltyTime(),0);
sbMain.ctList.clear();
/* test 3 */
sbMain.ParseInputData("5 3 20 I");
sbMain.ParseInputData("5 4 15 R");
sbMain.ParseInputData("5 4 30 I");
sbMain.ParseInputData("5 3 30 C");
assertEquals("3-1",sbMain.ctList.size(),1);
assertEquals("3-2",sbMain.ctList.get(0).GetTeamNum(),5);
assertEquals("3-3",sbMain.ctList.get(0).GetSolvedProblemCount(),1);
assertEquals("3-4",sbMain.ctList.get(0).GetTotalPenaltyTime(),50);
sbMain.ctList.clear();
}
public void testGetExistContestant()
{
Contestant c1 = new Contestant("1");
Contestant c2 = new Contestant("2");
Contestant c3 = new Contestant("3");
sbMain.ctList.add(c1);
assertEquals("1",sbMain.GetExistContestant("1"),c1);
assertEquals("1",sbMain.GetExistContestant("2"),null);
sbMain.ctList.add(c2);
sbMain.ctList.add(c3);
assertEquals("1",sbMain.GetExistContestant("5"),null);
assertEquals("1",sbMain.GetExistContestant("3"),c3);
}
/* ์ ๋ ฌ */
public void testSortResult()
{
/* test 1 */
sbMain.ParseInputData("1 2 10 I");
sbMain.ParseInputData("1 2 19 R");
sbMain.ParseInputData("1 2 21 C");
sbMain.ParseInputData("1 1 25 C");
sbMain.ParseInputData("4 2 30 C");
sbMain.ParseInputData("4 2 50 U");
sbMain.ParseInputData("4 5 25 C");
sbMain.ParseInputData("4 3 42 E");
sbMain.SortDataInput();
sbMain.PrintScoreBoard();
}
}
- ์์ง ์ถฉ๋ถํ ํ ์คํธ๋ฅผ ํ๊ฑฐ ์๋๊ตฌ์ ์ถ๋ ฅ๊ฒฐ๊ณผ๋ง ์ ๋๋ก ๋์ค๋ ํ์ธํ์ต๋๋ค. --;;
- 3๊ฐ์ ํด๋์ค๋ฅผ ์ ์ํ์ฌ ํ๋ก๊ทธ๋จ์ ๊ตฌํํ์ต๋๋ค.
- Scoreboard ํด๋์ค : ๋ฉ์ธ ํด๋์ค
- ScoreRecord ํด๋์ค : ์ ๋ ฅ ๋ฐ์ดํฐ ์ ์ฅ ํด๋์ค
- Team ํด๋์ค : ํ์ ๋ฒํธ(์์ด๋), ํผ ๋ฌธ์ ์ ๊ฑธ๋ฆฐ ์๊ฐ์ ์ ์ฅํ ํด๋์ค
import java.util.*;
import java.io.*;
public class Scoreboard {
public static void main(String args[]) throws IOException
{
// Map<ํ๋ฒํธ, ํ ๊ฐ์ฒด>
TreeMap<Integer, Team> teams;
// ์ผ์ด์ค ๊ฐฏ์ ์
๋ ฅ
String line = readLine();
int count = Integer.parseInt(line);
if (count <= 0)
{
System.out.println("Error: invalid number!!");
return;
}
// ๊ณต๋ฐฑ๋ผ์ธ ์
๋ ฅ
line = readLine();
if (line.length() > 0)
{
System.out.println("Error: must be a blank line");
return;
}
// ์ผ์ด์ค ์๋งํผ ๋ฃจํ
while (count > 0)
{
// Map<ํ๋ฒํธ, ํ๊ฐ์ฒด> ์ด๊ธฐํ
teams = new TreeMap<Integer, Team>();
// ๋ฐ์ดํฐ ์
๋ ฅ
line = readLine();
// ๊ณต๋ฐฑ ๋ผ์ธ์ด ๋์ฌ๋๊น์ง ๋ฃจํ
while (line.length() > 0)
{
// ScoreRecord ๊ฐ์ฒด ์์ฑ
ScoreRecord record = getScoreRecord(line);
// ์
๋ ฅ ์ค๋ฅ ์ฒดํฌ
if (record == null)
{
System.out.println("Error: Invalid data");
return;
}
// ScoreRecord๋ฅผ ๊ฐ์ง๋ Team ๊ฐ์ฒด ์ฐพ๊ธฐ
Team team = teams.get(record.getTeamNumber());
// ๊ฐ์ฒด๊ฐ ์์ผ๋ฉด Team ๊ฐ์ฒด ์์ฑ
if (team == null)
team = new Team();
// ํ์ ๊ธฐ๋ก ์
๋ ฅ
team.appendRecord(record);
teams.put(team.getTeamNumber(), team);
// ๋ฐ์ดํฐ ์
๋ ฅ
line = readLine();
} // while (line.length() > 0)
// ํ๋ณ๋ก ๊ฒฐ๊ณผ ์ถ๋ ฅ
for (Map.Entry<Integer, Team> entry : teams.entrySet())
{
int teamNumber = entry.getKey();
Team eTeam = entry.getValue();
System.out.println(teamNumber + " " + eTeam.getProblems() + " " + eTeam.getTimes());
}
count--;
} // while (count > 0)
}
static String readLine() throws IOException
{
BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ));
return reader.readLine();
}
static ScoreRecord getScoreRecord(String line)
{
StringTokenizer tokens = new StringTokenizer(line, " ");
if (tokens.countTokens() != 4)
{
System.out.println("Error: invalid input data");
return null;
}
return new ScoreRecord(line);
}
}
import java.util.*;
public class ScoreRecord {
private int teamNumber; // ํ๋ฒํธ
private int problem; // ๋ฌธ์
private int time; // ๊ฑธ๋ฆฐ์๊ฐ
private String format; // ๋ฌธ์ ์ํ
ScoreRecord(String line)
{
StringTokenizer tokens = new StringTokenizer(line, " ");
teamNumber = Integer.parseInt(tokens.nextToken());
problem = Integer.parseInt(tokens.nextToken());
time = Integer.parseInt(tokens.nextToken());
format = tokens.nextToken();
}
int getTeamNumber()
{
return teamNumber;
}
int getProblem()
{
return problem;
}
int getTime()
{
return time;
}
String getFormat()
{
return format;
}
void show()
{
System.out.println("teamNumber: " + teamNumber + ", problem: "
+ problem + ", time: " + time + ", format: " + format);
}
}
import java.util.*;
public class Team {
private int teamNumber; // ํ ๋ฒํธ
private TreeMap<Integer, Integer> scores; // Map<problem, time> ํ์ด ํผ ๋ฌธ์ ์ ํด๊ฒฐํ๋๋ฐ ๊ฑธ๋ฆฐ ์๊ฐ
Team()
{
teamNumber = 0;
scores = new TreeMap<Integer, Integer>();
}
int getTeamNumber()
{
return teamNumber;
}
void appendRecord(ScoreRecord record)
{
teamNumber = record.getTeamNumber();
String format = record.getFormat();
if (format.equals("I"))
append(record.getProblem(), 20);
if (format.equals("C"))
append(record.getProblem(), record.getTime());
}
int getProblems()
{
return scores.size();
}
int getTimes()
{
int times = 0;
for (Map.Entry<Integer, Integer> entry : scores.entrySet())
{
times += entry.getValue();
}
return times;
}
void append(int problem, int time)
{
if (!scores.containsKey(problem))
{
Integer penalty = new Integer(0);
scores.put(problem, penalty);
}
Integer times = scores.get(problem);
times += time;
scores.put(problem,times);
}
void show()
{
System.out.println("No: " + teamNumber);
for (Map.Entry<Integer, Integer> entry : scores.entrySet())
{
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
-
์ ๋ ฅ ๋ถ๋ถ ๋ง๋ค๊ธฐ๊ฐ ์ ์ด๋ฆฌ ๊ท์ฐฎ์์ง ใ ใ
-
judge ์ ์ ํด ๋ดค์ต๋๋ค. -.-;;;
/* @JUDGE_ID:parkpd 10038 C "test" */ /* @BEGIN_OF_SOURCE_CODE */ #include <iostream> #include <vector> #include <set> #include <strstream> #include <algorithm> using namespace std; #define _UNIT_TEST #ifdef _UNIT_TEST #include "../UnitTest++/src/UnitTest++.h" int main() { UnitTest::RunAllTests(); char temp; cin >> temp; return 0; } #endif enum State { NOT_TOUCHED = 0, INCORRECT, CORRECT }; class CScore { public: CScore() : m_Problem(0), m_Time(0), m_State(NOT_TOUCHED) { } void InsertData(int time, bool result); int m_Problem; int m_Time; State m_State; }; void CScore::InsertData(int time, bool result) { if (result) // ํ๊ธฐ ์ฑ๊ณต. { m_Time += time; m_State = CORRECT; } else // ์คํจํ๋ค๋ฉด { m_State = INCORRECT; m_Time += 20; // 20 ์ด ํจ๋ํฐ } } class CTeamScore { public: CTeamScore() : m_Id(-1), m_Corrects(0), m_Penalty(0) { for (int i = 0; i <= 9; ++i) { m_Scores[i].m_Problem = i; } } void InsertData(int id, int problem, int time, bool result); void CalcResult(); static bool IsMore(CTeamScore& a, CTeamScore& b); static bool IsLess(CTeamScore& a, CTeamScore& b); bool operator < (CTeamScore& other); int m_Id; int m_Corrects; int m_Penalty; CScore m_Scores[10]; }; void CTeamScore::InsertData(int id, int problem, int time, bool result) { m_Scores[problem].InsertData(time, result); CalcResult(); } void CTeamScore::CalcResult() { m_Corrects = 0; m_Penalty = 0; for (int i = 1; i <= 9; ++i) { if (CORRECT == m_Scores[i].m_State) { ++m_Corrects; m_Penalty += m_Scores[i].m_Time; } else if (INCORRECT == m_Scores[i].m_State) { m_Penalty += m_Scores[i].m_Time; } } } bool CTeamScore::IsMore(CTeamScore& a, CTeamScore& b) { return b < a; } bool CTeamScore::IsLess(CTeamScore& a, CTeamScore& b) { return a < b; } bool CTeamScore::operator < (CTeamScore& other) { // ์ ์ ๋ณด๊ณ if (m_Corrects != other.m_Corrects) { return m_Corrects < other.m_Corrects; } else { // ํจ๋ํฐ ๋ณด๊ณ if (other.m_Penalty != m_Penalty) { return other.m_Penalty < m_Penalty; } // ๋ฒํธ๋ฅผ ๋ณธ๋ค. return (other.m_Id < m_Id); } } class CScoreBoard { public: CScoreBoard() : m_InsertedCount(0) { m_TeamScores.resize(101); for (int i = 1; i <= 100; ++i) { m_TeamScores[i].m_Id = i; } } void InsertData(int contestant, int problem, int time, char result); void SortData(); void Print(ostream& output); typedef vector<CTeamScore> Teams; Teams m_TeamScores; int m_InsertedCount; }; void CScoreBoard::InsertData(int contestant, int problem, int time, char result) { if (result != 'C' && result != 'I') { return; } m_TeamScores[contestant].InsertData(contestant, problem, time, (result == 'C')); ++m_InsertedCount; } void CScoreBoard::SortData() { sort(m_TeamScores.begin(), m_TeamScores.end(), CTeamScore::IsMore); } void CScoreBoard::Print(ostream& output) { Teams::iterator it = m_TeamScores.begin(); while (-1 != it->m_Id) { CTeamScore& score = *it; output << score.m_Id << " " << score.m_Corrects << " " << score.m_Penalty << "\n"; ++it; } } #ifndef _UNIT_TEST int main() { return 0; } #else TEST_FIXTURE(CScore, InsertDataWithoutFailed) { CHECK_EQUAL(NOT_TOUCHED, m_State); InsertData(10, true); CHECK_EQUAL(CORRECT, m_State); CHECK_EQUAL(10, m_Time); } TEST_FIXTURE(CScore, InsertDataWithFailed) { CHECK_EQUAL(NOT_TOUCHED, m_State); InsertData(10, false); CHECK_EQUAL(INCORRECT, m_State); CHECK_EQUAL(20, m_Time); } TEST_FIXTURE(CScore, Example1) { InsertData(10, false); InsertData(21, true); CHECK_EQUAL(41, m_Time); } TEST_FIXTURE(CScore, Example2) { InsertData(11, true); CHECK_EQUAL(CORRECT, m_State); CHECK_EQUAL(11, m_Time); } TEST_FIXTURE(CTeamScore, Example1) { InsertData(1, 2, 10, false); CHECK_EQUAL(0, m_Corrects); CHECK_EQUAL(20, m_Penalty); InsertData(1, 2, 21, true); CHECK_EQUAL(1, m_Corrects); CHECK_EQUAL(41, m_Penalty); InsertData(1, 1, 25, true); CHECK_EQUAL(2, m_Corrects); CHECK_EQUAL(66, m_Penalty); } TEST_FIXTURE(CTeamScore, Example2) { InsertData(3, 1, 11, true); CHECK_EQUAL(1, m_Corrects); CHECK_EQUAL(11, m_Penalty); } TEST_FIXTURE(CScoreBoard, InsertData) { InsertData(1, 2, 10, 'I'); InsertData(3, 1, 11, 'C'); InsertData(1, 2, 19, 'R'); InsertData(1, 2, 21, 'C'); InsertData(1, 1, 25, 'C'); CHECK_EQUAL(2, m_TeamScores[1].m_Corrects); CHECK_EQUAL(66, m_TeamScores[1].m_Penalty); CHECK_EQUAL(1, m_TeamScores[3].m_Corrects); CHECK_EQUAL(11, m_TeamScores[3].m_Penalty); } struct FixtureCompare { FixtureCompare() { a.m_Id = 1; b.m_Id = 2; } CTeamScore a; CTeamScore b; }; TEST_FIXTURE(FixtureCompare, Compare) { // ๊ฐ์ฅ ์ฐ์ ์์๋ ๋ฌธ์ ๋ง์ถ ํ์ b.m_Corrects = 1; CHECK(a < b); CHECK(!(b < a)); // a ๋ ๊ฐ์ ์๋ฅผ ๋ง์ท๋ค๋ฉด, ํจ๋ํฐ๋ฅผ ๋ณธ๋ค. a.m_Corrects = 1; a.m_Penalty = 1; CHECK(a < b); CHECK(!(b < a)); b.m_Penalty = 10; CHECK(b < a); CHECK(!(a < b)); // ๋ ๋ค ์กฐ๊ฑด์ด ๊ฐ์ ๋๋ ํ ๋ฒํธ ์์๋๋ก a.m_Penalty = 10; CHECK(b < a); CHECK(!(a < b)); } TEST_FIXTURE(CScoreBoard, SortData) { InsertData(1, 2, 10, 'I'); InsertData(3, 1, 11, 'C'); InsertData(1, 2, 19, 'R'); InsertData(1, 2, 21, 'C'); InsertData(1, 1, 25, 'C'); SortData(); CHECK_EQUAL(2, m_TeamScores[0].m_Corrects); CHECK_EQUAL(66, m_TeamScores[0].m_Penalty); CHECK_EQUAL(1, m_TeamScores[1].m_Corrects); CHECK_EQUAL(11, m_TeamScores[1].m_Penalty); Print(cout); } #endif /* @END_OF_SOURCE_CODE */
-
์์ค๊ฐ ๋๋ ์ ธ ์์ต๋๋ค .UVA submit ํฌ๊ธฐ ใ
-
Judge.java
public class Judge{ private int contestant; private int problem; private int time; private String status; public Judge(int contestant , int problem , int time , String status){ this.contestant = contestant; this.problem = problem; this.time = time; this.status = status; } public int getContestant() { return contestant; } public int getProblem() { return problem; } public String getStatus() { return status; } public int getTime() { return time; } -
SolveResult.java
import java.util.HashSet; import java.util.Set; public class SolveResult implements Comparable<SolveResult>{ private int contestant; private int problemTotal = 0; private int totalTime = 0; private Set<Integer> solvedProblemSet = new HashSet<Integer>(); public SolveResult(int contestant){ this.contestant = contestant; } public void addJudge(Judge judge){ if(judge.getStatus().equals("C")){ solvedProblemSet.add(judge.getProblem()); problemTotal+= 1; totalTime += judge.getTime(); } if(judge.getStatus().equals("I")){ // ๋ฌธ์ ๋ฅผ ํผ ์ด๋ ฅ์ด ์์ผ๋ฉด if(solvedProblemSet.contains(judge.getProblem())){ //๋ฒ์ ์ ์ 20์ ๋ถ๊ณผ totalTime += 20; } } } public String toString(){ return contestant + " " + problemTotal + " " + totalTime; } public int getContestant() { return contestant; } public int compareTo(SolveResult o) { if(this.problemTotal>o.problemTotal) return -1; else if(this.problemTotal < o.problemTotal){ return 1; } else if(this.totalTime > o.totalTime){ return -1; } else if(this.totalTime < o.totalTime){ return 1; } return 0; } } -
Solver.java
import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.SortedSet; import java.util.Stack; import java.util.TreeSet; public class Solver { private Stack<Judge> judges = new Stack<Judge>(); public void inputJudge(int contestant , int problem , int time , String status) { judges.push(new Judge(contestant,problem,time,status)); } public SolveResult[] doSolve(){ Map<Integer, SolveResult> resultMap = new HashMap<Integer, SolveResult>(); while(!judges.isEmpty()){ Judge judge = judges.pop(); SolveResult solveResult = null; if(resultMap.containsKey(judge.getContestant())){ solveResult = resultMap.get(judge.getContestant()); }else if(judge.getStatus().equals("C")){ //๋ฌธ์ ๋ฅผ ํ์์ ๋๋ง solveResult = new SolveResult(judge.getContestant()); } if(solveResult!=null){ solveResult.addJudge(judge); resultMap.put(solveResult.getContestant(), solveResult); } } //๋งต์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌ ๋ ๋ฆฌ์คํธ์ ์ฝ์ Set<Integer> keys =resultMap.keySet(); SortedSet<SolveResult> sortedList = new TreeSet<SolveResult>(); for(Integer key : keys){ sortedList.add(resultMap.get(key)); } SolveResult[] resultArray = new SolveResult[sortedList.size()]; return sortedList.toArray(resultArray); } } -
SolverTest.java
import junit.framework.TestCase; public class SolverTest extends TestCase { public void testSolverInput(){ Solver solver = new Solver(); solver.inputJudge(1,2,10,"I"); solver.inputJudge(3,1,11,"C"); solver.inputJudge(1,2,19,"R"); solver.inputJudge(1,2,21,"C"); solver.inputJudge(1,2,25,"C"); SolveResult [] results = solver.doSolve(); assertEquals(results.length,2); for(SolveResult result : results){ System.out.println(result); } } }