Page number calculation

Source: Internet
Author: User

/**
* Problem: the page number of a book is encoded from the Natural Number 1 to n. Each page number has no leading 0, that is, the 6th page is encoded as 6, instead of <br>
* 06, or 006. The requirement for the number counting problem is to calculate the total page number n of the given book and the number of times the knife is used in all the pages of the book. <br>
* 0, 1 ,...... 9 .. 1 <= n <= 10 ^ 9.
* Train of Thought: For a digit t (0 <= t <= 9), consider the single digit, ten digits ...... To the highest bit of n, the number of these bits is t, and then the score is <br>
* Do not add up, that is, the number of t in all pages.
* <Br> assume that all n> = 1

**/

Class numbercount {private int N; private int length; private int [] result; Private Static void confirm (int n, int [] result) {// method used for verification, calculate the number of each integer one by one while (n> 0) {result [n % 10] ++; N/= 10 ;}} public static void test () {// test the function int [] n = {9, 23, 301,3423, 34830,000000, 83049839}; For (Int J: N) {numbercount NC = new numbercount (j ); int [] result = NC. getcount (); system. out. println ("n =" + J); For (int t = 0; t <10; t ++) {system. out. println ("digit = "+ T +" number = "+ result [T]);} int [] con = new int [10]; for (int K = 1; k <= J; k ++) Confirm (K, con); For (int K = 0; k <10; k ++) if (CON [k]! = Result [k]) {system. Out. println ("error! "); For (int t: con) system. out. println (t); break ;}} public numbercount (int n) {This. N = N; length = lengthof ();}/** get the number of each 0-9 digit in all pages */Public int [] getcount () {If (result = NULL) {result = new int [10]; Result [0] = getzerocount (); For (Int J = 1; j <10; j ++) result [J] = getcount (j);} return arrays. copyof (result, result. length);} private int getzerocount () {// calculate the number of 0 without leading 0. Therefore, int Pos = 1, TK = 10, and zeronumber = 0 must be calculated separately; while (Pos <L Ength) {// 0 cannot be in the highest bit zeronumber + = (N/tk-1) * (tk/10); // No leading 0, so reduce 1, after the number of pos-1 bit composition is tk/10if (Pos> 1) {// consider the pos-1 bit int NA = n % TK; int second = na * 10/TK; // Number of pos-1 for if (second = 0) {// The pos-1 is 0, then, the number of N/tk * TK values is Na + 1 zeronumber + = Na + 1;} else {// if this bit is not 0, then the number of all the combinations that match the pos-1 bit, that is, TK/10 zeronumber + = tk/10;} else {zeronumber + = (n <10? 0: 1);} POS ++; TK * = 10;} return zeronumber;} private int getcount (int K) {// calculate the number of 1-9 int Pos = 1, TK = 10, number = 0; while (Pos <= length) {number + = N/tk * (tk/10); If (Pos> 1) {int NA = n % TK; int second = na * 10/TK; If (second = k) {number + = Na-K * tk/10 + 1 ;} else if (second> K) {number + = tk/10;} else {number + = n % 10> = K? 1:0;} POS ++; TK * = 10;} return number;} private int lengthof () {// calculate the decimal length of N, int r = 0, T = 1; while (T <= N) {r ++; T * = 10;} return r ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.