注册 登录
编程论坛 C++教室

超级诡异的超时。。求解释。

聪儿 发布于 2012-11-02 09:02, 277 次点击
题目在此处: http://acm.xmu.

第一种解法:
#include <iostream>
#include <math.h>
#define MAXSIZE 1000000
using namespace std;

int main()
{
    long int n;
    int temp=0,num;
    cin>>n;
    if(n<1||n>1000000) return -1;
    for(int i=0;i<2*n-1;i++)
    {
        cin>>num;
        temp=temp^num;
    }
    cout<<temp<<endl;
    return 0;
}


第二种解法
#include <iostream>
using namespace std;


int main()
{
    int n,i,x,s=0;
   
    scanf("%d",&n);
    n=n*2-1;
   
    for(i=1;i<=n;i++)
    {
      scanf("%d",&x);
      s=s^x;
    }
    printf("%d\n",s);
   
    return 0;
}

诡异的问题出现了,第一种解法 超时,用了5004MS,而第二种解法是AC的,用时2024MS。
 
诡异的地方在于两种解法除了输入用了两种表达方式以外没有任何区别。。。

想不明白了,请大神解释!!
4 回复
#2
rjsp2012-11-02 09:21
cin 比 scanf 慢
#3
zxd5432012-11-03 09:38
Huge Input, scanf is Recommended(推荐) if your are using C++.
这道题的下边写的
#4
天剑山2012-11-03 14:50
明明白白的写有scanf比cin快....
#5
聪儿2012-11-03 16:19
明白了。。对 c++太不懂了。。。多谢!
1