Input N in PHP and judge from 1 ~ Number of times 1 appears in the decimal number of N Sequence

Source: Internet
Author: User
1 <? PHP 2 # input n to judge from 1 ~ Number of occurrences of 1 in the decimal number of N sequence 3 # For example, input 12, numbers containing 1 are, here 1 appears 5 times in total 4 5 # Number of times to judge the occurrence of 1 in decimal number 6 function count1dec ($ X) {7 $ I = 1; 8 $ COUNT = 0; 9 10 # obtain each digit of X from the single digit, and determine if it is 111 while ($ x> = POW (10, $ I-1 )) {12 $ r = floor ($ X % POW (10, $ I)/POW (10, $ I-1); 13 if ($ r = 1) {14 $ count ++; 15} 16 $ I ++; 17} 18 19 return $ count; 20} 21 22 # determine the number of occurrences of the entire sequence 1 23 function count1dec_seq ($ n) {24 $ COUNT = 0; 25 for ($ I = 1; $ I <= $ N; $ I ++ ) {26 $ count + = count1dec ($ I); 27} 28 return $ count; 29} 30 31 echo count1dec_seq (12); 32?>

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.