题解 CF347A 【Difference Row】
仔细观察,其实这个价格是可以抵消的
看:
价格 = (x1 - x2) + (x2 - x3) + (x3 - x4) + (x(n - 1) - xn)
所以会发现,x2和x2抵消,x3和x3抵消……其实最后就是x1 - xn
所以具体代码如下,用的是STL的vector,一会会讲到vector的使用:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector <int> vec;
bool cmp(int x, int y)
{
return x > y;
}
int main()
{
int n;
cin >> n;
vec.resize(n);
vector <int>::iterator it;
for(it = vec.begin(); it != vec.end(); it++)
{
cin >> *it;
}
sort(vec.begin(), vec.end(), cmp);
cout << vec.at(0) << " ";
for(it = vec.end() - 2; it != vec.begin(); it--)
{
cout << *it << " ";
}
cout << vec.at(n - 1);
cout << endl;
return 0;
}
vector是c++STL库的一种容器,常被称为动态数组,可以动态赋值,出示定义可以写大小,也可以不写,定义方式如下:
vector <数据类型> 名称;
vector还有很多函数等,需要包含头文件 #include

浙公网安备 33010602011771号