Calculate the number of 1

Source: Internet
Author: User

_ Int64 countone (_ int64 N) {_ int64 COUNT = 0; If (n = 0) Count = 0; else if (n> 1 & n <10) Count = 1; else {_ int64 highest = N; _ int64 bit = 0; while (highest> = 10) {highest = highest/10; bit ++;} _ int64 Weight = (_ int64) Pow (10, bit); If (highest = 1) {COUNT = countone (weight-1) + countone (n-weight) + N-weight + 1;} else {COUNT = highest * countone (weight-1) + countone (n-highest * weigh T) + weight ;}} return count ;}< pre name = "code" class = "CPP"> publiclong countone2 (long n) {Long Count = 0; long I = 1; long current = 0, after = 0, before = 0; while (n/I )! = 0) {current = (N/I) % 10; before = N/(I * 10); after = N-(N/I) * I; if (current> 1) Count = count + (before + 1) * I; elseif (current = 0) Count = count + before * I; elseif (current = 1) count = count + before * I + after + 1; I = I * 10;} return count ;}


 
 

Calculate the number of 1

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.