Topic 2664: The real questions of the 13th Provincial Competition of the Lanqiao Cup in 2022 - Summation

You can’t get full marks with violence, so I optimized it and extracted the common factor.

#include <iostream>
using namespace std;
int main(){
	int n;
	int nums[200000];
	long long sum = 0;
	long long num = 0;//记录
	cin>>n;
	for(int i = 0;i < n; i++){
		cin>>nums[i];
		num += nums[i];
	}
	for(int i = 0;i < n; i++){
		num -= nums[i];//每次减掉前面的
		sum += (nums[i]*num);
	}
	cout<<sum;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_63499305/article/details/129972208