문제 출처
강아지와 병아리
|
풀이..
package com.syjung; import java.util.Scanner; public class problem1 { boolean calc_value(String input ) { int ani_sum, count_regs, max_count; int x,y; boolean find_answer=false; // 문자열을 공백을 기준으로 숫자로 분리하기 String [] values = input.split(" ", 2); // Parameter counter 체크 if( values.length != 2 ) { System.out.println("Error!! Parameter counters are not 2."); return false; } // 문자열을 숫자로 변환, 동물숫자 합과 다리갯수 ani_sum = max_count = Integer.parseInt(values[0]); count_regs = Integer.parseInt(values[1]); if( ani_sum > 1000 ) { System.out.println("Error!! 동물의 합은 1000을 초과 할수 없습니다."); return true; } if( count_regs > 4000 ) { System.out.println("Error!! 동물의 합은 4000을 초과 할수 없습니다."); return true; } if( ani_sum == 0 && count_regs== 0 ) { System.out.println("종료!! 감사합니다~"); return false; } // 동물숫자 합과, 다리갯수의 합과 일치하는 x,y를 찾는다. for( x =0; x < max_count; x++ ) { for(y=0; y < max_count; y++ ) { if( x + y == ani_sum ) { if( 4*x + 2 * y == count_regs ) { System.out.println( String.format( "정답, 강아지 :%d, 병아리:%d ", x, y ) ); find_answer = true; break; } } } } if ( find_answer == false ) { System.out.println( "정답이 없습니다."); } return true; } /** * @param args */ // Create a single shared Scanner for keyboard input private static Scanner scanner = new Scanner( System.in ); // Program execution starts here public static void main ( String [] args ) { problem1 prb1 = new problem1(); String input; do { // Prompt the user System.out.print( "Type some data for the program: " ); // Read a line of text from the user. input = scanner.nextLine(); // Display the input back to the user. System.out.println( "input = " + input ); } while( prb1.calc_value(input) ); System.out.println( "프로그램이 정상 종료되었습니다." ); } // end main method }