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.0252 sec