Java implementation to find a number within a range of all

Source: Internet
Author: User
Tags findone

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

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.