문제: https://www.acmicpc.net/problem/1019
우선 bruteForce 방법으로 몇 개의 숫자를 넣어 규칙을 찾아 문제를 풀었습니다.
규칙은 99, 999, 999 그리고 199, 299, 399, 499 등을 넣어보면 발견할 수 있습니다.
my solving
c++
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | #include <fstream> #include <iostream> #include <cstring> #include <climits> #include <algorithm> #include <string> #include <queue> #include <vector> using namespace std; typedef long long ll; ll ret[10] = { 0, }; void printNumber() { for (int i = 0; i < 10; i++) { if (i != 0) cout << " "; cout << ret[i]; } cout << endl; } void calcDigit(int n) { while (n > 0) { ret[n % 10]++; n /= 10; } } void bruteForce(int n) { memset(ret, 0, sizeof(ret)); for (int i = 1; i <= n; i++) calcDigit(i); printNumber(); } void solve(ll n) { memset(ret, 0, sizeof(ret)); string str = to_string(n); queue<int> num; for (int i = 0; i < str.size(); i++) num.push(str[i] - '0'); vector<int> firstNumbers; while (num.size() > 1) { int firstNum = num.front(); if (firstNum == 0) { num.pop(); firstNumbers.push_back(firstNum); continue; } int digit = num.size() - 1; string strOnes = ""; string strOneZeros = "1"; for (int i = 0; i < digit; i++) { strOnes += '1'; strOneZeros += '0'; } ll ones = stoll(strOnes); ll oneZeros = stoll(strOneZeros); ll temp1 = digit * (pow(10, digit - 1)) * firstNum; ll temp2 = temp1 - ones; ret[0] += temp2; for (int i = 1; i < 10; i++) ret[i] += temp1; for (int i = 1; i < firstNum; i++) ret[i] += oneZeros; if (firstNumbers.empty() == false) { int remaining = digit; for (int i = 9; i <= 9 * pow(10, digit - 1); i *= 10) { ret[0] += remaining * i; remaining--; } } for (vector<int>::iterator it = firstNumbers.begin(); it != firstNumbers.end(); it++) ret[*it] += (firstNum * oneZeros); firstNumbers.push_back(firstNum); calcDigit(firstNum * oneZeros); num.pop(); } int lastNum = num.front(); for (int i = 1; i <= lastNum; i++) { ret[i]++; for (vector<int>::iterator it = firstNumbers.begin(); it != firstNumbers.end(); it++) ret[*it]++; } printNumber(); } int main() { ll n; cin >> n; solve(n); return 0; } | cs |
'Algorithm, Data structure > Solved Algorithmic Problem' 카테고리의 다른 글
BAEKJOON 1022 - 소용돌이 예쁘게 출력하기 (2) | 2016.08.04 |
---|---|
BAEKJOON 1021 - 회전하는 큐 (0) | 2016.08.03 |
BAEKJOON 1018 - 체스판 다시 칠하기 (0) | 2016.08.02 |
BAEKJOON 1017 - 소수 쌍 (0) | 2016.08.01 |
BAEKJOON 1016 - 제곱 ㄴㄴ 수 (0) | 2016.07.31 |