java--Sword Point Offer (7)

Source: Internet
Author: User

31. Find out the number of occurrences of 1 in an integer of 1~13, and calculate the number of occurrences of 1 in 100~1300 integers? For that, he counted a few times. 1~13 contains 1 of the numbers are 1, 10, 11, 12, 13 so there is a total of 6, but for the latter problem he can not do. Acmer wants you to help him, and to make the problem more common, you can quickly find out the number of occurrences of 1 in any nonnegative integer interval.

 Public classSolution { Public intNumberof1between1andn_solution (intN) {intNumber = 0;  for(inti = 1; I <= N; i++) {String str=string.valueof (i); Char[] chars=Str.tochararray ();  for(intj = 0; J < Chars.length; J + +){                if(Chars[j] = = ' 1 ') number++; }        }        returnNumber ; }}

java--(7)

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.