Evaluate 0 ~ The total number of 1 in all num

Source: Internet
Author: User
  # Include  <  Stdio. h  >  
# Include < Math. h >
Int Fun ( Int Num)
{
Int K = 0 ;
Int Sum = 0 ;

Int Pw_k0 = ( Int ) Pow ( 10 , K ); // The value of pw_k0 is 10 ^ K.
Int Pw_k1 = ( Int ) Pow ( 10 , K + 1 ); // Pw_k1 is 10 ^ (k + 1)

While (Num - Pw_k0) > = 0 ) // If num is less than 10 ^ K, it indicates that there is no value on the K bit, that is, output.
{
Int X = (Num % Pw_k1 - Num % Pw_k0) / Pw_k0; // Take the number on the K-bit,
If ( 0 = X)
{
Sum = Sum + Num / Pw_k1 * Pw_k0; // When the number on K bit is 0, the number of all numbers on this bit is 1.
}
Else If ( 1 = X)
{
Sum = Sum + Num / Pw_k1 * Pw_k0 + Num % Pw_k0 + 1 ; // When the number on the K bit is 1, the number of all numbers on this bit is 1.
}
Else
{
Sum = Sum + Num / Pw_k1 * Pw_k0 + Pw_k0; // When the number in the K bit is greater than 1, the number of all numbers in this bit is 1.
}
K ++ ;
Pw_k0 = ( Int ) Pow ( 10 , K );
Pw_k1 = ( Int ) Pow ( 10 , K + 1 );
}
Return SUM;
}

Int Main ()
{
Printf ( " % D \ n " , Fun ( 11 ));
Return 0 ;
}

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.