설명
입력: 정수 3개 (T, a(1~100), b(1~1,000,000))
출력: 정수 1개 (나머지 값(1 ~ 10))
첫 줄에서 테스트 케이스 T를 입력 받고 그 후 a와 b를 T번 받는다.
a를 b번 곱한값의 일의 자리 수를 출력한다.
링크
https://www.acmicpc.net/problem/1009
1009번: 분산처리
입력의 첫 줄에는 테스트 케이스의 개수 T가 주어진다. 그 다음 줄부터 각각의 테스트 케이스에 대해 정수 a와 b가 주어진다. (1 ≤ a < 100, 1 ≤ b < 1,000,000)
www.acmicpc.net
문제 해설
전체적인 코드는 다음과 같습니다.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
class CDistributed_Processing
{
private:
const int32_t modArr[10][4] = {
{ 10, 10, 10, 10 }, { 1, 1, 1, 1 },
{ 2, 4, 8, 6 }, { 3, 9, 7, 1 },
{ 4, 6, 4, 6 }, { 5, 5, 5, 5 },
{ 6, 6, 6, 6 }, { 7, 9, 3, 1 },
{ 8, 4, 2, 6 }, { 9, 1, 9, 1 }};
int32_t testCase;
std::vector<int32_t> resultVec;
void solve(int32_t a, int32_t b);
void print();
public:
CDistributed_Processing();
~CDistributed_Processing();
void Distributed_Processing();
};
CDistributed_Processing::CDistributed_Processing() {
// constructor
};
CDistributed_Processing::~CDistributed_Processing() {
// destructor
};
void CDistributed_Processing::Distributed_Processing() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
int32_t base;
int32_t exponent;
int32_t period;
std::cin >> testCase;
std::cin.ignore();
std::string inputStr = " ";
for (int32_t i = 0; i < testCase; i++)
{
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
ss >> base;
ss >> exponent;
period = base % 10;
solve(period, exponent);
}
print();
};
void CDistributed_Processing::solve(int32_t period, int32_t exponent) {
int exponentMod = (exponent - 1) % 4;
resultVec.push_back(modArr[period][exponentMod]);
};
void CDistributed_Processing::print() {
for (int32_t i = 0; i < testCase; i++)
{
std::cout << resultVec.at(i) << "\n";
}
};
int main()
{
CDistributed_Processing* cDistributed_Processing = new CDistributed_Processing();
cDistributed_Processing->Distributed_Processing();
delete cDistributed_Processing;
}
이 문제는 단순히 pow함수를 이용해서 값을 구하고 나머지를 구하는 식으로는 구할 수 없습니다.
(입력 최대값인 100을 1,000,000번 곱하는 연산을 처리하기도 힘들 뿐더러 저장하기가.... )
일의 자리 숫자를 제곱하면 일정 패턴으로 일의 자리 수가 반복됩니다.
이를 이용해서 배열을 만들고 접근해서 위와 같이 문제를 풀 수 있습니다.
풀이 코드 링크
https://github.com/dhk1999/coding_problems/blob/main/baekjoon/cpp/problem_1009.cpp
'Coding Problems(백준) > Cpp' 카테고리의 다른 글
백준 2745번 진법 변환 C++ 풀이 (0) | 2021.10.29 |
---|---|
백준 17869번 Simple Collatz Sequence C++ 풀이 (0) | 2021.10.24 |
백준 1032번 명령 프롬프트 C++ 풀이 (0) | 2021.10.24 |
백준 2557번 Hello World C++ 풀이 (0) | 2021.10.23 |