问题 3629 --读入优化(程序填空)

3629: 读入优化(程序填空)★★

时间限制: 1 Sec  内存限制: 128 MB
提交: 659  解决: 467
[提交][状态][命题人:]

题目描述

有时候我们需要输入大量的数据,如果使用cin或者scanf会因为效率较低导致程序运行超时,这时候就需要使用读入优化了。完善下面的程序,使得程序能快速读入数字。

#include<bits/stdc++.h>
using namespace std;
int a[1000007];
void read(int &x)
{
    int f=1;
    x=0;
    char s=getchar();
    while(____(1)______)
    {
        if(s=='-')
        _____(2)_____
        s=getchar();
    }
    while(s>='0'&&s<='9')
    {
        ____(3)____
        s=getchar();
    }
    _____(4)_____
}
int main()
{
    int ans=0;
    int n;
    read(n);
    for(int i=0;i<n;i++)
    {
        ____(5)______
        ans+=a[i];
    }
    printf("%d\n",ans);
}

输入

一个整数n<1000000

下面一行是n个整数,每个整数大于-1000,并且小于1000

输出

输出上述的n个整数的累加和

样例输入
Copy
10
1 2 3 4 5 6 7 8 9 10
样例输出
Copy
55

提示

void read(int &x),这里的x前面加了&,表示引用,子函数中x的值可以返回到主函数中

来源

[提交][状态]