728x90
320x100
10757번: 큰 수 A+B
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
📌 접근 방법
- 매우 큰 수들을 더해야하기 때문에 문자열로 바꾸어 덧셈을 해준다
반응형
✅ Pass Code
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
string str1="",str2="", result="";
cin>>str1>>str2;
int size1=str1.length();
int size2=str2.length();
int c=0;
while(size1>0||size2>0){
int a=0,b=0;
if(size1>0){
a=str1[--size1]-'0';
}
if(size2>0){
b=str2[--size2]-'0';
}
int add = a+b+c;
c=add/10;
add=add%10;
result+=add+'0';
}
if(c>0){
result+=c+'0';
}
for(int i=result.length()-1; i>=0; i--){
cout<<result[i];
}
return 0;
}
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/008.gif)
728x90
반응형
'Coding Test > Baekjoon' 카테고리의 다른 글
[BOJ/백준/C++] 2231번 분해합 (1) | 2024.01.26 |
---|---|
[BOJ/백준/C++] 1018번 체스판 다시 칠하기 (1) | 2024.01.26 |
[BOJ/백준/C++] 2869번 달팽이는 올라가고 싶다 (0) | 2024.01.26 |
[BOJ/백준/C++] 1193번 분수찾기 (1) | 2024.01.26 |
[BOJ/백준/C++] 2903번 중앙 이동 알고리즘 (1) | 2024.01.26 |