I. Contents of the topic
Given a positive integer in decimal, write down all integers starting with 1, to N, and then count the number of "1" that appears.
Requirements:
Write a function f (n) that returns the number of "1" that occurs between 1 and N. For example F (12) = 5.
Within a 32-bit integer range, the maximum n of the "F (n) =n" that satisfies the condition is what.
Second, design ideas
Each bit to calculate the number of 1, each adjacent to the three-digit link, ABC, first count C, plus B, and finally plus a
Third, the experimental code
Findone.java Package Com.minirisoft;import Java.util.scanner;public class FindOne {public static int Sum (int n) { int Count = 0; int Factor = 1; int Lower = 0; int Curr = 0; int higher = 0; while (N/factor! = 0) {Lower = n-(n/factor) * Factor; Curr = (n/factor)% 10; Higher = N/(Factor *10); Switch (Curr) {case 0:count + = higher * Factor; Break Case 1:count + = higher * Factor + Lower + 1; Break Default:count + = (higher + 1) * Factor; Break } Factor *= 10; } return Count; } public static void Main (string[] args) {Scanner input=new Scanner (system.in); System.out.print ("Please enter a positive integer:"); int N=input.nextint (); System.out.print ("1 to" +n+ "1 numbers: "+sum (N) +" x "); } }
Four, the experiment
Five, experience
General design procedures, to find the law of the problem, according to the law in the process of making
Java implementation to find a number within a range of all