Enter an integer n to calculate the number of times 1 appears in the decimal representation of the N integers from 1 to n.

Source: Internet
Author: User

For example, input 12, integers from 1 to 12 contain numbers 1, 10, 11, and 12, and 1 appear 5 times in total.

Public class test {// Number of all numbers from 1 to n public static int gettotal (int n) {If (n = 0) {return 0 ;} else {return gettotal (n-1) + Get (n) ;}// the first public static int get (int I) {string S = I + ""; int num = 0; For (Int J = 0; j <S. length (); j ++) {If (S. charat (j) = '1') {num ++;} return num;} public static void main (string [] ARGs) {// todo auto-generated method stubsystem. out. print (gettotal (12 ));}}

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.