망나니 AWOS의 일상
article thumbnail

1단계 10952번 A + B - 5

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.util.StringTokenizer;

class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        StringTokenizer st;
        int a;
        int b;
        
        while(true){
            st = new StringTokenizer(br.readLine());
            a = Integer.parseInt(st.nextToken());
            b = Integer.parseInt(st.nextToken());
           
            if(a == 0 && b == 0)
                break;
            bw.write(a + b + "\n");
            
        }
        bw.close();
    }
}

 

2단계 10951번 A + B - 5

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.util.StringTokenizer;

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        int a, b;
        StringTokenizer st;
        String a_str = "";
        // EOF일 때 종료
        while((a_str=br.readLine()) != null){
            st = new StringTokenizer(a_str);
            a = Integer.parseInt(st.nextToken());
            b = Integer.parseInt(st.nextToken());
            
            bw.write(a + b + "\n");
        }
        bw.close();
    }
}

 

3단계 1110번 더하기 사이클

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int newNum, sum;
        int arr[] = new int[2];

        // 첫 번째 사이클
        if(n<10){
            arr[0] = 0;
        }else{
            arr[0] = n/10;
        }
        arr[1] = n%10;
        sum = (arr[0] + arr[1]) % 10;
        newNum = arr[1]*10 + sum;

        System.out.println(cycleAdd(n, newNum));
    }

	// 두 번째 사이클부터
    public static int cycleAdd(int n, int b){
        int sum, cycle=1, newNum=b;
        int arr[] = new int[2];
        while(true){

            if(n==newNum){
                break;
            }
            cycle++;

            arr[0] = newNum/10;
            arr[1] = newNum%10;
            sum = (arr[0] + arr[1]) % 10;
            newNum = arr[1]*10 + sum;

        }
        return cycle;
    }

}
profile

망나니 AWOS의 일상

@AWOS

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!