Wednesday, September 9, 2015

JungOl Question 1001 (http://jungol.co.kr/problem.php?id=1001)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.*;
public class Main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        
        for(;;){
         
         //input, two numbers
         int total, totalLeg;
            total = sc.nextInt();
            totalLeg = sc.nextInt();
            
            //input 0 -> stop program
            if(total == 0 || totalLeg == 0){
             break;
            }
            
            //input validation
            if(total > 1000 || total < 0 || totalLeg > 4000 || totalLeg < 0){
             System.out.println("INPUT ERROR!");
            }else{
             
             Boolean findFlag = false; 
                
             //Generate two numbers(dog and chick), sum of which are totalLeg(second input)
                for(int dog = 0;dog <= total;dog++){
                 
                 int chick = total  - dog;
                 int computedLeg = dog * 4 + chick * 2; 
                 
                 //if the number is found -> print "dog" and "leg" and break;
                 if(totalLeg == computedLeg){
                  System.out.println(dog + " " + chick);
                  findFlag = true;
                  break;
                 }
                }
                
                //No matching -> print "0"
                if(!findFlag){
                 System.out.println("0");
                }
                
            }
            
        }

 }

}

No comments:

Post a Comment