728x90
반응형
brute force문제이다.
입력 수가 1억이하이고, 시간 제한이 1초이므로 실제로 이 수를 이어서 써서 자릿수를 구하게 되면 시간초과가 날 것이다.
입력수의 길이를 구하고 길이 -1 까지의 자릿수를 모두 더한 후,
(입력수 - 10 ^ (length - 1) + 1) * length와 값을 더했다.
만일 120이라고 가정했을 때,
120의 길이는 3이다.
3 - 1 = 2의 자릿수를 모두 더한다.
9 - 1 + 1 + (99 - 10 + 1) * 2
(120 - 100 + 1)* 3 을 더하면 252이다.
//수 이어쓰기 1
#include <iostream>
#include <string>
using namespace std;
int getNumCount(int tenNum, int nineNum){
return nineNum - tenNum + 1;
}
int main(void){
string numStr;
cin >> numStr;
string tenStr= "1", nineStr = "9";
int numLength = 0;
for(int i = 0; i < numStr.size() - 1; i++){
numLength += (getNumCount(stoi(tenStr), stoi(nineStr))) * tenStr.size();
tenStr += "0";
nineStr += "9";
}
numLength += (stoi(numStr) - stoi(tenStr) + 1) * tenStr.size();
cout << numLength;
}
반응형
'algorithm' 카테고리의 다른 글
[algorithm] 백준 - 조합 0의 개수 (0) | 2020.11.04 |
---|---|
[algorithm] 백준 - 리모컨 (0) | 2020.11.04 |
[algorithm] 백준 - 가장 긴 바이토닉 부분 수열 (0) | 2020.11.03 |
[algorithm] 백준 - 합분해 (0) | 2020.11.03 |
[algorithm] 백준 - 제곱수의 합 (0) | 2020.11.03 |