'Algorithm/Programming-Challenges'에 해당되는 글 2건

  1. 2014.10.31 The 3n + 1
  2. 2014.10.30 Java 초기 입력/출력 틀 만들기
반응형

1. 문제 풀이


package id110101;


/* @BEGIN_OF_SOURCE_CODE */


/* @JUDGE_ID: 75154 100 Java "" */


import java.io.*;

import java.util.*;


public class The3nPlus1 {

public static void main(String[] args) {

The3nPlus1 main = new The3nPlus1();

main.begin();


}

private void begin() {

String line;

StringTokenizer st;

long first, second;

long maxNumber;

while ((line = readLine(255)) != null && !line.isEmpty()) {

st = new StringTokenizer(line);

first = Long.parseLong(st.nextToken());

second = Long.parseLong(st.nextToken());

maxNumber = doCalc(first, second);

System.out.println(String.format("%d %d %d", first, second, maxNumber));

}

}

private long doCalc(long first, long second) {

long value = 0;

long maxNumber = 0;

long number = 0;

for (value = first; value < second + 1; value++ ) {

number = doLoop(value);

maxNumber = maxNumber > number ? maxNumber : number;

}

return maxNumber;

}

private long doLoop(long number) {

long value = 0;

long count = 1;

for (value = number; value > 0; ) {

if (value == 1) break;

if (value % 2 == 1) {

value = 3 * value + 1;

} else {

value = value / 2;

}

count++;

}

return count;

}

private static String readLine(int maxLg) {

byte lin[] = new byte[maxLg];

int lg = 0;

int car = -1;

String line = "";

try {

while (lg < maxLg) {

car = System.in.read();

// 10: Line feed   13: Carriage return

if ((car < 0) || (car == 10) ) {

break;

}

lin[lg++] += car;

}

} catch (IOException e) {

return null;

}

if ((car < 0) && (lg == 0)) return null;

line = new String(lin, 0, lg);

line = line.replaceAll("[\n\r]", "");

return line;

}


}


/* @END_OF_SOURCE_CODE */


반응형
Posted by seungkyua@gmail.com
,
반응형

1. 디렉토리 구조

ProgrammingChallenges

  - classes

  - datafiles

     - id110101

        - The3nPlus1-in

        - The3nPlus1-out

  - src

     - id110101

        - The3nPlus1.java


  test.bat


2. test.bat

@echo off


set PACKAGE_NAME=id110101

set PROGRAM_NAME=The3nPlus1



set OUTPUT_DIR=classes

set DATA_DIR=datafiles


javac -d %OUTPUT_DIR% src/%PACKAGE_NAME%/%PROGRAM_NAME%.java

cd %OUTPUT_DIR%

java -cp . %PACKAGE_NAME%.%PROGRAM_NAME% < ../%DATA_DIR%/%PACKAGE_NAME%/%PROGRAM_NAME%-in > out


@echo on

fc out ../%DATA_DIR%/%PACKAGE_NAME%/%PROGRAM_NAME%-out


@echo off

cd ..


3. 기본 입출력 틀

package id110101;


/* @BEGIN_OF_SOURCE_CODE */


/* @JUDGE_ID: 75154 100 Java "" */


import java.io.*;

import java.util.*;


public class The3nPlus1 {

public static void main(String[] args) {

The3nPlus1 main = new The3nPlus1();

main.begin();


}

private void begin() {

String line;

StringTokenizer st;

long first, second;

while ((line = readLine(255)) != null && !line.isEmpty()) {

st = new StringTokenizer(line);

first = Long.parseLong(st.nextToken());

second = Long.parseLong(st.nextToken());

System.out.println(String.format("%d %d", first, second));

}

}

private static String readLine(int maxLg) {

byte lin[] = new byte[maxLg];

int lg = 0;

int car = -1;

String line = "";

try {

while (lg < maxLg) {

car = System.in.read();

// 10: Line feed   13: Carriage return

if ((car < 0) || (car == 10) ) {

break;

}

lin[lg++] += car;

}

} catch (IOException e) {

return null;

}

if ((car < 0) && (lg == 0)) return null;

line = new String(lin, 0, lg);

line = line.replaceAll("[\n\r]", "");

return line;

}


}


/* @END_OF_SOURCE_CODE */















반응형
Posted by seungkyua@gmail.com
,