BOJ | 2588번

송치헌·2021년 5월 27일
0
post-thumbnail

Python 풀이

A, B = int(input()), int(input()) # A와 B를 각각 문자열로 입력받는 동시에 정수형으로 변환
# A = 472
# B = 385
print(A*(B%10)) # B%10 == 5
print(A*(B%100//10)) # (B%100)//10 == 85//10 == 8
print(A*(B//100)) # B//100 == 3
print(A*B)

처음에 풀었던 풀이는

A, B = input().split()

print(int(A)*int(B[2]))
print(int(A)*int(B[1]))
print(int(A)*int(B[0]))
print(int(A)*int(B))

이거였는데 런타임 에러(Value Error)가 떴다. 이유는 찾지 못하였음...

>>> A, B = input().split()
472 385

>>> print(type(int(A)))
<class 'int'>
>>> print(type(int(B[2])))
<class 'int'>
>>> print(type(int(B[1])))
<class 'int'>
>>> print(type(int(B[0])))
<class 'int'>
>>> print(type(int(B)))
<class 'int'>


결과도 오류없이 잘 나온다...이 부분에 대해서는 좀 더 공부가 필요한 것 같다.

C++ 풀이

#include <iostream>

using namespace std;

int main()
{
    int A, B;
    cin >> A >> B; // A == 472, B == 385
    int st, nd, rd; // 1st, 2nd, 3rd -> B의 각 자리수를 저장하기 위한 변수
    st = (B % 10); // 5
    nd = (B % 100 - st); // 85 - 5 == 80
    rd = (B % 1000 - nd - st); // 385 - 80 - 5 == 300
    cout << A * st << endl << A * nd / 10 << endl 
    	<< A * rd / 100 << endl << A * (st + nd + rd);
}
profile
https://oraange.tistory.com/ 여기에도 많이 놀러와 주세요

0개의 댓글