쉬운문제!
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
String input1=sc.nextLine();
String arr1[]=input1.split("");
String input2=sc.nextLine();
String arr2[]=input2.split("");
Stack<Integer> st1=new Stack<>();
Stack<Integer> st2=new Stack<>();
Stack<Integer> st3=new Stack<>();
if(arr1.length>=arr2.length) {
for(int i=0;i<arr1.length;i++) {
st1.add(Integer.parseInt(arr1[i]));
}
for(int i=0;i<arr2.length;i++) {
st2.add(Integer.parseInt(arr2[i]));
}
}
else {
for(int i=0;i<arr1.length;i++) {
st2.add(Integer.parseInt(arr1[i]));
}
for(int i=0;i<arr2.length;i++) {
st1.add(Integer.parseInt(arr2[i]));
}
}
//st1의길이가 st2보다 무조건 길게 set
int min=st2.size();
int temp=0;
int upper=0;
for(int i=0;i<min;i++) {
temp=upper+st1.pop()+st2.pop();
if(temp>=10) {
st3.add(temp%10);
upper=temp/10;
}else {
st3.add(temp);
upper=0;
}
}
int max=st1.size();
for(int i=0; i<max ;i++) {
temp=upper+ st1.pop();
if(temp>=10) {
st3.add(temp%10);
upper=temp/10;
}
else {
st3.add(temp);
upper=0;
}
}
if(upper!=0)
st3.add(1);
int len=st3.size();
for(int i=0;i<len;i++) {
System.out.print(st3.pop());
}
}
}
'Computer Engineering > 알고리즘 테스트' 카테고리의 다른 글
백준[1654]-랜선자르기 (0) | 2021.02.05 |
---|---|
코드업[3130]- 소들의 헤어스타일 / java 데이터 타입 (0) | 2021.01.04 |
코드업[3321]- 최고의 피자🍕 (0) | 2021.01.03 |
코드업[3120]-리모컨 (0) | 2021.01.03 |
프로그래머스 코팅테스트 문제: 체육복 (0) | 2020.04.24 |