Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			arr[i], _ = strconv.Atoi(scanner.Text())
		}
		cnt := 0
		for i := n-1; i  > 0; i-- {
			for j := 0; j < i; j++ {
				if arr[j] > arr[j+1] {
					// log.Println(arr)
					cnt++
					arr[j], arr[j+1] = arr[j+1], arr[j]
				}
			}
		}
		// log.Println(arr)
		fmt.Println(cnt)
	}
}

C++: #include<iostream>
#include<vector>
using namespace std;

int main(void){
	int n;
	while(cin>>n,n){
		vector<int> sort;
		int inp , cnt = 0;
		for(int i = 0 ; i < n ; i ++){
			cin>>inp;
			sort.push_back(inp);
		}
		for(int i = 0 ; i < n - 1 ; i ++)
			for(int j = i ; j < n ; j ++){
				if(sort[i] > sort[j]){
					int work = sort[i];
					sort[i] = sort[j];
					sort[j] = work;
					cnt ++;
				}
			}
			
		cout<< cnt <<endl;
	}
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	for(int n;cin>>n,n;){
		vi a(n);
		rep(i,n)
			cin>>a[i];
		int res=0;
		for(int i=n;i--;)
			for(int j=0;j<i;j++)
				if(a[j]>a[j+1]){
					swap(a[j],a[j+1]);
					res++;
				}
		cout<<res<<endl;
	}
	
	return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cstdio>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

bool solve() {
    int n, arr[100000];
    cin >> n;
    if (n == 0) { return false; }
    for (int j = 0; j < n; ++j) {
        cin >> arr[j];
    }
    int ret = 0;
    for (int j = 0; j < n; ++j) {
        for (int k = j+1; k < n; ++k) {
            if (arr[j] > arr[k]) {
                ++ret;
            }
        }
    }
    cout << ret << endl;
    return true;
}

int main() {
    while(solve());
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n,a[100];

int main(){
	while(cin>>n){
		if(!n)return 0;
		int ans=0;
		for(int i=0;i<n;i++)cin>>a[i];
		for(int i=0;i<n-1;i++){
			for(int j=0;j<n-1-i;j++){
				if(a[j]>a[j+1]){
					ans++;
					swap(a[j],a[j+1]);
				}
			}
		}
		cout<<ans<<endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include <cassert>

using namespace std;

int N, A[100];
int W[100];

int merge_and_count(int l,int r){
	if (l + 1 >= r)return 0;
	if (l + 2 == r){
		if (A[l] <= A[l + 1])return 0;
		swap(A[l], A[l + 1]);
		return 1;
	}
	int m = (l + r) / 2;
	int cl = merge_and_count(l, m);
	int cr = merge_and_count(m, r);
	int c = 0;
	int i = 1, j = m;
	int k = 1;
	while (i < m && j < r){
		if (A[i] <= A[j])W[k++] = A[i++];
		else{
			W[k++] = A[j++];
			c += m-i-1;
		}
	}
	while (i < m)W[k++] = A[i++];
	while (j < r)W[k++] = A[j++];
	assert(k == r);
	return cl + cr + c;
}

int main()
{
	cin >> N;
	for (int n = 0; n < N; n++){
		cin >> A[n];
	}
	cout << merge_and_count(0, N) << endl;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ELEM(array) (sizeof (array)/sizeof *(array))
#define MAX_N 100
#define SAFE_FREE(ptr) if( ptr != NULL ){ free(ptr); ptr = NULL; }
typedef unsigned int UINT;
typedef long long ll;
typedef queue<pair<int, int> > que;

int main()
{
	int n, a[1005];
	while (1)
	{
		cin >> n; if (n == 0)return 0;
		REP(i, n)cin >> a[i];
		int cnt = 0;
		REP(i, n - 1)REP(j, n - 1)
		{
			if (a[j] > a[j + 1])
			{
				swap(a[j], a[j + 1]);
				cnt++;
			}
		}
		cout << cnt << endl;

		
	}return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    int sort[1000000];

    while (cin >> n, n) {
        int cnt = 0;

        for (int i = 0; i < n; i++)
            cin >> sort[i];

        for (int i = n - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (sort[j] > sort[j + 1]) {
                    swap(sort[j], sort[j + 1]);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

C++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;


int main(){
	int n;
	while(cin>>n,n){
		vector<int> v(n); FOR(i,n) cin>>v[i];
		int ans = 0;
		FOR(i,n) for(int j = 0; j+1 < n-i; j++){
			if(v[j] > v[j+1]){ 
				swap(v[j],v[j+1]); 
				ans++;
			}
		}
		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>

using namespace std;

int main()
{
	int n,i,j,ans,array[1000];
	
	for(;;)
	{
		cin >> n;
		if(n==0)
			break;
		ans=0;
		for(i=0;i<n;i++)
			cin >> array[i];
		
		for(;n>1;)
		{
			for(i=0;i<n-1;i++)
			{
				if(array[i]>array[i+1])
				{
					int a;
					a=array[i];
					array[i]=array[i+1];
					array[i+1]=a;
					ans++;
				}
			}
			n--;
		}
		cout << ans << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include <cassert>

using namespace std;

int N, A[100];
int W[100];

int merge_and_count(int l,int r){
	if (l + 1 >= r)return 0;
	if (l + 2 == r){
		if (A[l] <= A[l + 1])return 0;
		swap(A[l], A[l + 1]);
		return 1;
	}
	int m = (l + r) / 2;
	int cl = merge_and_count(l, m);
	int cr = merge_and_count(m, r);
	int c = 0;
	int i = 1, j = m;
	int k = 1;
	while (i < m && j < r){
		if (A[i] <= A[j])W[k++] = A[i++];
		else{
			W[k++] = A[j++];
			c += 1;
		}
	}
	while (i < m)W[k++] = A[i++];
	while (j < r)W[k++] = A[j++];
	assert(k == r);
	return cl + cr + c;
}

int main()
{
	cin >> N;
	for (int n = 0; n < N; n++){
		cin >> A[n];
	}
	cout << merge_and_count(0, N) << endl;
}