[백준BOJ2588 C++] 곱셈
[백준BOJ2588 C++] 곱셈
문제
https://www.acmicpc.net/problem/2588
처음에 생각한 방법
(2) 위치에 들어갈 값을 string으로 받은 후, 인덱스/int형 변환으로 이용해서 계산하기이후 생각해서 작성한 방법
연산을 이용해서 일의 자리수, 십의 자리수, 백의 자리수를 구하고 그대로 계산하기.
풀이
- 1의 자리 수:N % 10
ex) 428 / 10 = 42 ··· 8 - 10의 자리 수:(N % 100) / 10
ex) 428 / 100 = 4 ··· 28
28 / 10 = 2 ··· 8 - 100의 자리 수 :N / 100
428 / 100 = 4 ··· 28
제출
#include <iostream>
using namespace std;
int a, b;
int main() {
cin >> a;
cin >> b;
int first = a * (b % 10);
int second = a * ((b % 100) / 10);
int third = a * (b / 100);
int ans = first + (second * 10) + (third * 100);
cout << first << endl;
cout << second << endl;
cout << third << endl;
cout << ans;
return 0;
}
This post is licensed under CC BY 4.0 by the author.