티스토리 뷰
https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWBC1lOad9IDFAWr
수가 최대 100자리수까지 될 수 있으므로 일반적인 자료형으로는 덧셈을 할 수 없다. string으로 두 수를 받은 후 자릿수를 맞춰준 후 덧셈을 구현한다.
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 | #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { int t; scanf("%d",&t); for(int k=1;k<=t;k++) { string a,b,c; cin>>a>>b; int n=a.size(),m=b.size(),len=max(n,m); if(n>m) for(int i=0;i<n-m;i++) b="0"+b; else for(int i=0;i<m-n;i++) a="0"+a; int w=0,sum; for(int i=len-1;i>=0;i--) { sum=a[i]-'0'+b[i]-'0'+w; if(sum>=10) w=1,c.push_back(sum-10+'0'); else w=0,c.push_back(sum+'0'); } if(w) c.push_back('1'); reverse(c.begin(),c.end()); cout<<'#'<<k<<' '<<c<<'\n'; } return 0; } | cs |
'Algorithm > SW Expert Academy' 카테고리의 다른 글
4530 극한의 청소 직업 (0) | 2018.09.04 |
---|---|
3282 0/1 Knapsack (0) | 2018.04.12 |
3304 최장 공통 부분 수열 (0) | 2018.04.11 |
3307 최장 증가 부분 수열 (0) | 2018.04.11 |
3809 화섭이의 정수 나열 (0) | 2018.04.11 |