大佬帮帮我,这两种代码思路是一样的,但是一个10分一个100分
查看原帖
大佬帮帮我,这两种代码思路是一样的,但是一个10分一个100分
670533
spontenious楼主2023/4/17 17:10
#include<iostream>
#include<algorithm>
using namespace std;
int n, h[301];
int main()
{
	cin >> n;
	int right = n, left = 0;
	long long ans = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> h[i];
	}
	sort(h + 1, h + 1 + n);
	h[0] = 0;
	while (left < right)
	{
		ans += (long long)((h[right] - h[left]) * (h[right] - h[left]));
		left++;
		ans += (long long)((h[right] - h[left]) * (h[right] - h[left]));
		right--;
	}
	cout << ans << endl;
}
#include<iostream>
#include<algorithm>
using namespace std;
int n, h[301];
int main()
{
	cin >> n;
	int right = n, left = 0;
	long long ans = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> h[i];
	}
	sort(h + 1, h + 1 + n);
	h[0] = 0;
	while (left<right)
	{
		ans += (long long)((h[right] - h[left]) * (h[right] - h[left]));
		if (left % 2) right--;
		else left++;
	}
	cout << ans << endl;
}
2023/4/17 17:10
加载中...