Notice
Recent Posts
Recent Comments
Link
«   2025/10   »
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 30 31
Archives
Today
Total
관리 메뉴

코딩로그

[백준/BOJ/C++] 1357번 뒤집힌 덧셈 본문

YJ/C++

[백준/BOJ/C++] 1357번 뒤집힌 덧셈

Team DAON 2021. 10. 14. 16:31

[문제]

어떤 수 X가 주어졌을 때, X의 모든 자리수가 역순이 된 수를 얻을 수 있다. Rev(X)를 X의 모든 자리수를 역순으로 만드는 함수라고 하자. 예를 들어, X=123일 때, Rev(X) = 321이다. 그리고, X=100일 때, Rev(X) = 1이다.

두 양의 정수 X와 Y가 주어졌을 때, Rev(Rev(X) + Rev(Y))를 구하는 프로그램을 작성하시오

 

[입력]

첫째 줄에 수 X와 Y가 주어진다. X와 Y는 1,000보다 작거나 같은 자연수이다.

 

[출력]

첫째 줄에 문제의 정답을 출력한다.

 

[Source Code]

#include <stdio.h>
#include <iostream>
#include <cmath>
using namespace std;

int number(int n){
    int count = 0;
    while(n >= 10){
        n /= 10;
        count++;
    }
    return ++count;
}

int reverse(int a){
    int count = number(a);
    
    double result = 0;
    for(double i = count - 1; i >= 0; i--){
        result += (a % 10) * pow(10, i);
        a /= 10;
    }
    
    return result;
}

int main(){
    int a, b;
    cin >> a >> b;
    
    int result = reverse(a) + reverse(b);
    cout << reverse(result);
}

[결과 화면]