본문 바로가기
Programming/Contest

GCJ Qualification Round Africa 2010

by NAMP 2012. 2. 13.

   

Here are some choice problems for new competitors:

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

댓글