View Code of Problem 18

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;


int main()
{
	int n;

	while (cin >> n) {

		int half = n / 2;	//分成两个直链
		int sum = (half * (half - 1)) / 2;	//两个直链分别逆序的交换次数

		if (n % 2 == 0)
			sum *= 2;	//偶数链
		else
			sum += sum + half;	//奇数链,剩下的一个元素还需交换 half = n/2次

		cout << sum << endl;
	}
}

Double click to view unformatted code.


Back to problem 18