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