1 to an integer 1 of the number

Source: Internet
Author: User

First, design ideas

From the beginning to the N to traverse, ask each one on the number of 1, and then the number of each bit added is the result, such as 12 of the number of 1 is 5, of which there are 1, 10 bits on 4, total five.

Second, the Code

 Packagefind 1;ImportJava.util.Scanner; Public classFindOne {/**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner A=NewScanner (system.in); System.out.println ("Please enter a positive integer n in decimal:"); intn=A.nextint (); intI,k=0;  for(i=1;i<=n;i=i*10)        {            intM=i*10; intp=n%m,q=n/m; if(p/i==0) {k=k+q*i; }            Else if(p/i==1) {k=k+q*i; K=k+n%i+1; }            Else{k=k+ (q+1) *i; }} System.out.println (The number of 1 between "1 to" +n+ "is:" +k); }}

Three

Iv. Analysis of results

Decomposition of the problem, step by step to solve, which is the class mentioned in the big problem into a small problem, a process to slowly solve a process, the end will be together, the problem is solved.

1 to an integer 1 of the number

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.