Speed of printf and cout

Source: Internet
Author: User

Today, the fart king asked me to do hdu1128, and his last time was 78 Ms. My time was 500 + Ms. I wrote something simpler than him and the size was smaller than him, he is faster than me, so after discussion, cout is a function and is called every time, while printf is a light output. No wonder he is faster than me ~~~~ I can't.

My: # include <iostream>
Using namespace STD;
Int A [1000010] = {0 };
Int main (void)
{
Int I, J, K;
For (I = 1, K = 0; I <1000001; I ++)
{
K = I; j = I * 10;
While (J/= 10)
K + = J % 10;
A [k] = 1;
If (! A [I])
Cout <I <Endl;
}
Return 0;
}

Her:

# Include <stdio. h>
# Include <string. h>

Int hash [1000005];

Int fun (int K)
{
Int sum = 0;
While (k)
{
Int r = K % 10;
Sum + = R;
K/= 10;
}
Return sum;
}
Int main ()
{
Int I;
Memset (hash, 1, sizeof (hash ));
For (I = 1; I <= 1000000; I ++)
{
Int sum = I;
Int K;
Sum + = fun (I );
Hash [Sum] = 0;
}
For (I = 1; I <= 1000000; I ++)
{
If (hash [I])
Printf ("% d \ n", I );
}

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.