From 1 to each number in parameter n, the number of occurrences of 1 on each bit and

Source: Internet
Author: User
/** * For each number from 1 to parameter n, 1 occurrences and *  * @param n * @return */public static int countone (int n) {int factor = 1;int Cur;int lo W;int High;int cnt = 0;while ((n/factor)! = 0) {low = n-(n/factor) * factor;//lower digit cur = (n/factor)% 10;//current bit digit h IgH = N/(factor * 10);//high-level digital switch (cur) {case 0:cnt + = high * factor;//up to higher level break;case 1:cnt + = higher * factor + low + 1;break;default:cnt + = (high + 1) * factor;} Factor *= 10;} return CNT;}

From 1 to each number in parameter n, the number of occurrences of 1 on each bit and

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.