정수론(Number theory)
백준 1019번: 책 페이지
cepiloth
2018. 7. 1. 19:05
반응형
https://www.acmicpc.net/problem/1019
1. 문제
지민이는 N쪽인 책이 한권 있다. 첫 페이지는 1쪽이고, 마지막 페이지는 N쪽이다. 각 숫자가 모두 몇 번이 나오는지 출력하는 프로그램을 작성하시오.
2. 알고리즘
키워드 - 정수론
참고 - https://www.slideshare.net/Baekjoon/baekjoon-online-judge-1019
3. 코드
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 | #include <iostream> #include <sstream> #include <string> #include <algorithm> #include <functional> #include <vector> #include <list> #include <queue> #include <deque> #include <map> #include <set> #include <stack> using namespace std; #define MAX_SIZE 100 #define INF 0x7fffffff #define CENDL "\n" /* * @memory - 2380 kb * @time - 56 ms */ typedef long long lld; int n; int ans[10]; void count(int x, lld p) { while(x){ ans[x % 10] += p; x /= 10; } } int main() { cin.tie(0); std::ios::sync_with_stdio(false); cin >> n; lld p = 1; int s = 1; while(s <= n){ while( n % 10 != 9 && s <= n){ count(n,p); --n; } if( n< s) break; while( s % 10 != 0 && s <= n){ count(s,p); ++s; } s /= 10; n /= 10; for(int i=0; i<=9; ++i) { ans[i] += (n-s+1) * p; } p*=10; } for(int i=0; i<10 ; i++) cout << ans[i] << " "; return 0; } | cs |
반응형