Here are some choice problems for new competitors:
- Africa 2010, Qualification Round: Store Credit, Reverse Words.
- Code Jam 2008, Round 1A: Minimum Scalar Product.
- Code Jam 2009, Qualification Round: Alien Language.
- Code Jam 2010, Round 1C: Rope Intranet.
- Code Jam 2010, Round 1B: File Fix-it.
- Code Jam 2010, Round 1A: Rotate.
원본 위치 <http://code.google.com/codejam/contests.html>
Problem
You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solution you provide will consist of the two integers indicating the positions of the items in your list (smaller number first).
Input
The first line of input gives the number of cases, N. N test cases follow. For each test case there will be:
- One line containing the value C, the amount of credit you have at the store.
- One line containing the value I, the number of items in the store.
- One line containing a space separated list of I integers. Each integer P indicates the price of an item in the store.
- Each test case will have exactly one solution.
Output
For each test case, output one line containing "Case #x: " followed by the indices of the two items whose price adds up to the store credit. The lower index should be output first.
Limits
5 ≤ C ≤ 1000
1 ≤ P ≤ 1000
Small dataset
N = 10
3 ≤ I ≤ 100
Large dataset
N = 50
3 ≤ I ≤ 2000
Sample
Input |
Output |
3 100 3 5 75 25 200 7 150 24 79 50 88 345 3 8 8 2 1 9 4 4 56 90 3 | Case #1: 2 3 Case #2: 1 4 Case #3: 4 5 |
원본 위치 <http://code.google.com/codejam/contest/351101/dashboard#s=p0>
맨 처음 줄은 총 개수를 의미하고
그 다음부터는 반복
자바 파일 읽기부터. | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;
public class Qualification_Round_Africa_2010 {
/** * @param args */ public static void main(String[] args) {
String strInputFileName = "A-small-practice.in"; String strOutputFileName = "A-small-practice.out";
if (strInputFileName.length() == 0) { // args.length 는 옵션 개수 System.err.println("Input Filename..."); System.exit(1); // 읽을 파일명을 주지 않았을 때는 종료 }
try { //////////////////////////////////////////////////////////////// BufferedReader in = new BufferedReader(new FileReader(strInputFileName)); String s;
while ((s = in.readLine()) != null) { System.out.println(s); } in.close(); //////////////////////////////////////////////////////////////// } catch (IOException e) { System.err.println("ERROR"); System.err.println(e); // 에러가 있다면 메시지 출력 System.exit(1); }
try { //////////////////////////////////////////////////////////////// BufferedWriter out = new BufferedWriter(new FileWriter(strOutputFileName)); String s = "출력 파일에 저장될 이런 저런 문자열입니다.";
out.write(s); out.newLine(); out.write(s); out.newLine();
out.close(); //////////////////////////////////////////////////////////////// } catch (IOException e) { System.err.println(e); // 에러가 있다면 메시지 출력 System.exit(1); } } } |
이제 분석해보자.
소스 | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.text.Format;
import javax.swing.plaf.SliderUI;
public class Qualification_Round_Africa_2010 {
/** * @param args */ public static void main(String[] args) {
String strInputFileName = "A-large-practice.in"; String strOutputFileName = "A-large-practice.out";
try { //////////////////////////////////////////////////////////////// BufferedReader in = new BufferedReader(new FileReader(strInputFileName)); BufferedWriter out = new BufferedWriter(new FileWriter(strOutputFileName)); String s;
int iTotalDataNumber = Integer.parseInt(in.readLine()); // 총 데이터 수
// Use Loop // 1. C, the amount of credit you have at the store. // 2. I, the number of items in the store. // 3. Each integer P indicates the price of an item in the store.
int iTheAmountOfCreaditYouHave = 0; int iTheNumberOfItemsinTheStore = 0; int iCaseNumber = 0; String strOut = ""; Boolean bFound = false;
while ((s = in.readLine()) != null) { iTheAmountOfCreaditYouHave = Integer.parseInt(s); iTheNumberOfItemsinTheStore = Integer.parseInt(in.readLine());
String[] array; s = in.readLine(); array = s.split(" ");
int[] aItemPrices = new int[array.length];
for (int idx = 0 ; idx < array.length; idx++) { aItemPrices[idx] = Integer.parseInt(array[idx]); }
iCaseNumber ++; bFound = false;
for (int idxFirst = 0; idxFirst < aItemPrices.length; idxFirst++) { for (int idxSecond = idxFirst+1; idxSecond < aItemPrices.length; idxSecond++) { if (iTheAmountOfCreaditYouHave == aItemPrices[idxFirst] + aItemPrices[idxSecond]) { strOut = String.format("Case #%d: %d %d", iCaseNumber, idxFirst+1, idxSecond+1); System.out.println(strOut); out.write(strOut); out.newLine(); bFound = true; break; } }
if (bFound == true) { break; } } }
in.close(); out.close(); //////////////////////////////////////////////////////////////// } catch (IOException e) { System.err.println("ERROR"); System.err.println(e); // 에러가 있다면 메시지 출력 System.exit(1); }
}
// 배열을 화면에, 요소별로 알기 쉽게 출력 public static void dumpArray(String[] array) { for (int i = 0; i < array.length; i++) System.out.format("array[%d] = %s%n", i, array[i]); } } |
------------------
결론은
'Programming > Contest' 카테고리의 다른 글
code jam 2012 일정. (0) | 2012.02.23 |
---|---|
Registration opens on Tuesday, March 13th, 2012. (0) | 2012.02.20 |
Language Popularity (0) | 2012.02.01 |
Google Code Jam (0) | 2012.02.01 |
Contest 사이트 목록 (0) | 2012.01.31 |
댓글