U E D R , A S I H C RSS

프로그래�/장보기

문제 보기
----
~java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Shopping {
	private static BufferedReader br;
	
	public static int processOneCase(int num) {
		String line = null;
		String [] contents;
		double [][] rates = new double[num][2];
		for(int i = 0; i < num; i++) {
			try {
				line = br.readLine();				
			} catch (IOException e) {
				e.printStackTrace();
			}
			contents = line.split(" ");
			int price = Integer.parseInt(contents[1]);
			int weight = Integer.parseInt(contents[0]);
			rates[i][0] = (double) price / weight;
			rates[i][1] = price;
		}
		
		double minRate = rates[0][0];
		int minRateIndex = 0;
		for(int i = 1; i < num; i++) {
			if (rates[i][0] < minRate) {
				minRate = rates[i][0];
				minRateIndex = i;
			}
			else if (rates[i][0] == minRate) {
				if (rates[i][1] < rates[minRateIndex][1]) {
					minRate = rates[i][0];
					minRateIndex = i;
				}				
			}
		}
		
		return (int) rates[minRateIndex][1];
	}
	
	public static void main(String[] args) {		
		br = null;
		
		try {
			br = new BufferedReader(new FileReader("test.txt"));
			String line = br.readLine();
			int testCase = Integer.parseInt(line);
			for(int i = 0; i < testCase; i++) {
				line = br.readLine();
				int result = processOneCase(Integer.parseInt(line));
				System.out.println(result);			
			}
			
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

시간 : 30m
----
타ì�´ë¨¸ë¥¼ 켜놓고 시간ì�„ 우선시하다보니 역시 Refactoringì�„ 하고 ì‹ì§€ 않았다. 요구사항ì�„ ì�½ê³  ê°�ì�´ íŒ� 오는 문제는 그냥 막 코딩하는 ì‹�ì�´ë‹¤. mainì—� 확 ì§‘ì–´ 넣고 풀어ë�„ 그냥 풀린 문제를 êµ³ì�´ 메소드로 나누고 여러 êµ°ë�°ì—�서 사용ë�˜ëŠ” 변수를 필드화하는 것ì�´ ì •ë§� 귀찮았다. 하지만 그러한 ë°©ì‹�으로 전환하고 습관화하는 능력ì�„ 키워야 겠다.
----
프로그래�
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:25
Processing time 0.0046 sec