본문 바로가기

백준/언랭 탈출하기

[Java] 백준 11382번 : 꼬마 정민

문제

꼬마 정민이는 이제 A + B 정도는 쉽게 계산할 수 있다. 이제 A + B + C를 계산할 차례이다!

입력

첫 번째 줄에 A, B, C (1 ≤ A, B, C ≤ 10^12)이 공백을 사이에 두고 주어진다.

출력

A+B+C의 값을 출력한다.

 

정답 코드

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    
    Long A, B, C;
    A = sc.nextLong();
    B = sc.nextLong();
    C = sc.nextLong();
    
    System.out.println(A+B+C);
  }
}

+ int와 Long의 차이

int : 최소 16bit(2byte), [-32.768, +32.767]

long : 최소 32bit(4byte), [-2,147,486,648 ~ +2,147,483,647]

- long으로 선언 (X), Long으로 선언(O)

 

틀린 코드들

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in); 
    int A=0;
    int B=0;
    int C=0;
    System.out.println(A+B+C);
  }
}

틀린 이유 : 이따위로 선언하면 0+0+0 = 0 임

 

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in); 
    int A=nextInt();
    int B=nextInt();
    int C=nextInt(); 
    System.out.println(A+B+C);
  }
}

틀린 이유 : A, B, C 변수 선언 안함

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    
    int A, B, C;
    A = sc.nextInt();
    B = sc.nextInt();
    C = sc.nextInt();
    
    System.out.println(A+B+C);
  }
}

 

+ 입력 받을 때, 변수 선언하고 입력 객체 넣는 것 잊지말기

문제 똑바로 읽고 자바에 익숙해지자