Find the largest F (n) = N in a range, where F (n) is the sum of "1" in the calculation of a value less than or equal to n.

Source: Internet
Author: User

# include
using namespace STD;
pair mypair;
int count_1 (int n) // count the number of values containing "1"
{< br> int sum = 0;
while (n! = 0)
{< br> If (N % 10 = 1)
{< br> sum ++;
}< br> N/= 10;
}< br> return sum;

}
Int F (int n) // the sum of all the values in n that contain "1"
{
Int sum = 0;
While (N)
{
Sum + = count_1 (N );
N --;
}
Return sum;
}
Int main ()
{
Int maxcount = 0;
Int COUNT = 0;
Int Index = 0;
For (INT I = 1; I <4000000000; I ++) // find the largest F (n) = N
{
Count = f (I );
If (COUNT = I)
If (count> maxcount)
{
Maxcount = count;
Index = I;
}
}
If (maxcount = 0 & Index = 0)
Cout <"not met ";
Else
{
Mypair = make_pair (maxcount, index );
Cout <"the maximum number of 1 displayed is" <mypair. First <"this value appears in" <mypair. Second <Endl;
}
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.