자바 백준 1357번 뒤집힌 덧셈
입력 받은 X와 Y를 String으로 입력을 받은 뒤 for문으로 역순으로 바꿔준다. 그 뒤 Integer.parseInt를 사용하면 백의 자리가 0이거나, 백의 자리와 십의 자리가 0일 때 자동으로 0을 없애준다. 이를 이용해 풀면된다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Scanner sc = new Scanner(System.in); | |
String st1 = ""; | |
String st2 = ""; | |
String st3 = ""; | |
String num1 = sc.next(); | |
String num2 = sc.next(); | |
for(int i=num1.length()-1; i>=0; i--) { | |
st1 = st1+String.valueOf(num1.charAt(i)); | |
} | |
for(int i=num2.length()-1; i>=0; i--) { | |
st2 = st2+String.valueOf(num2.charAt(i)); | |
} | |
int ch= Integer.parseInt(st1)+Integer.parseInt(st2); | |
String num3 = String.valueOf(ch); | |
for(int i=num3.length()-1; i>=0; i--) { | |
st3 = st3+String.valueOf(num3.charAt(i)); | |
} | |
System.out.println(Integer.parseInt(st3)); | |
} | |
} |
'백준 알고리즘 > JAVA' 카테고리의 다른 글
자바 백준 9375번 패션왕 신해빈 (0) | 2018.08.13 |
---|---|
자바 백준 7785번 회사에 있는 사람 (0) | 2018.08.11 |
자바 백준 2864번 5와 6의 차이 (0) | 2018.08.08 |
자바 백준 1652번 누울 자리를 찾아라 (0) | 2018.08.07 |
자바 백준 2869번 달팽이는 올라가고 싶다 (0) | 2018.08.05 |