wzoi.cc读入优化程序填空AC代码

题目描述:

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

输入格式:

一个整数n < 1000000

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

输出格式:

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

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

请完善程序。

#include<bits/stdc++.h>
using namespace std;
int a[ 1000007 ];

int read( ){
	int x=0, f=1;
	char ch= ____(1)_____;
	while ( ( ch <'0') || ( ch>'9') ) {
		if (ch=='-') ___(2)____;
		ch = getchar();
	}
	while (  ch>='0' && ch<='9' ) {
		x = (x<<1) + (x<<3) + ____(3)______;
		ch = getchar();
	}
	return ___(4)_____;       //f=-1时,表示读取的是负数
}

void write( int x ) {
	if( x<0 ) {
		putchar( '-' );
		x = -x;
	}
	if( x>9 ) write( ____(5)_____ );
	putchar( x % 10 + '0' );
}

int main() {
    int ans=0;
    int n;
    n = read( );
    for(int i=0;i<n;i++)
    {
        ____(6)______
        ans += a[i];
    }
    write(  ans );
}

时间限制: 1000ms
空间限制: 256MB

解析:

不用程序填空,我们直接写一个加法程序即可!

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	long long ans=0;
	for(int d=0;d<n;d++)
	{
		ans=ans+a[d];
	}
	cout<<ans;
	return 0;
}

有点啰嗦,可以自己改一下! 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值