-
백준 1019번: 책 페이지정수론(Number theory) 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. 코드
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273#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 반응형'정수론(Number theory)' 카테고리의 다른 글
백준 1065번: 한수 (0) 2018.07.03 백준 13241번: 최소공배수 (0) 2018.07.03 백준 4673번: 셀프 넘버 (0) 2018.06.29 백준 10610번: 30 (0) 2018.06.29 백준 5086번: 배수와 약수 (0) 2018.06.21