algorithm problem 15 - andstudy/forge GitHub Wiki

๋ฌธ์ œ 15๋ฒˆ ๊ฒฝ์‹œ๋Œ€ํšŒ ์ ์ˆ˜ํŒ (Contest Scoreboard)

  • ์ฑ… ์ž…๋ ฅ๋ถ€๋ถ„์— ๋Œ€ํ•œ ํ•œ๊ธ€ ๋ฒˆ์—ญ์ด ๋„ˆ๋ฌด ์• ๋งค๋ชจํ˜ธํ•ด์„œ ์ฐพ์•„๋ดค๋Š”๋ฐ.

  • ์—ญ์‹œ ๋ฒˆ์—ญ์ด ์ด์ƒํ•œ๊ฑฐ์˜€๋„ค์š”.

      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
    
  • ์˜๋ฌธ Contest Scoreboard


  1. ํ•œ ๋ฌธ์ œ๋„ ํ’€์ง€ ๋ชปํ•œ ํŒ€๋„ ์ถœ๋ ฅ์ด ๋˜์–ด์•ผ ํ•˜๋‚˜์š”?
  • ์ถœ๋ ฅ์•ˆํ•ด๋„ ๋ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. (display only the contestants that have made a submission.)
  • ์—ญ์‹œ ์ด๊ฒƒ๋„ ์ž˜๋ชป์ƒ๊ฐ. ๋งž์ถ”๊ฑด ๋ชป๋งž์ถ”๊ฑด ๊ฒฐ๊ณผ๋ฅผ submission ํ•œ ํŒ€์€ ์ถœ๋ ฅํ•ด์•ผํ• ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.
  1. ์ฒซ๋ฒˆ์งธ ๋ฌธ์ œ๋Š” ํ’€๊ณ  ๋‘๋ฒˆ์งธ ๋ฌธ์ œ๋Š” 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 ํ–ˆ์„๋•Œ๋งŒ ์ตœ์ข…๋ฒŒ์ ์‹œ๊ฐ„์— ์ ์šฉํ•˜๊ณ  .....๋„ˆ๋ฌด ๋Šฆ์–ด์„œ ์ˆ˜์ •์€ ๋‚ด์ผ

Java ๋ฒ„์ „

  • TDD ๊ฐ€ ์‰ฝ์ง€ ์•Š๋„ค์š”. Test case ์ž‘์„ฑ์€ ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑํ›„ ํ•ด๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค ์•„์•…

    • Test Driven์„ ๊ณ ์ง‘ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.
    • Test Accelerated Development (["TAD"])๋กœ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • submission ํ•˜๋ฉด ์ถœ๋ ฅํ•ด์ฃผ๊ณ  C ์ผ๋•Œ๋งŒ I ์˜ ์‹œ๊ฐ„๋ฒŒ์ ์„ ๊ณ„์‚ฐํ•˜๋„๋ก ์ˆ˜์ •ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

ScoreBoardMain.java

    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;		
    	}
    
    }

Contestant.java

    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;
    	}	
    	
    }

ContestantTest.java

    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);
    	}
    
    }

ScoreBoardMainTest.java

    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);
    	}
    
    }

Study ๊ฒฐ๊ณผ

ScoreBoardMain.java

    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);
    		}
    	}
    
    }

Contestant .java

    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;
    	}
    	
    }

ScoreBoardMainTest.java

    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 ํด๋ž˜์Šค : ํŒ€์˜ ๋ฒˆํ˜ธ(์•„์ด๋””), ํ‘ผ ๋ฌธ์ œ์™€ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์„ ์ €์žฅํ•œ ํด๋ž˜์Šค

Scoreboard.java

    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);
    	}
    }

ScoreRecord.java

    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);
    	}
    	
    }

Team.java

    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());
    		}
    	}
    }

ParkPD

  • ์ž…๋ ฅ ๋ถ€๋ถ„ ๋งŒ๋“ค๊ธฐ๊ฐ€ ์™œ ์ด๋ฆฌ ๊ท€์ฐฎ์€์ง€ ใ…Žใ…Ž

  • 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 */
    

kukuman

  • ์†Œ์Šค๊ฐ€ ๋‚˜๋ˆ ์ ธ ์žˆ์Šต๋‹ˆ๋‹ค .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);
      		}
      	}
      }
    
โš ๏ธ **GitHub.com Fallback** โš ๏ธ