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<cstdio>
#include<time.h>
using namespace std;
#define N 314159
int a[N],t[N];
long long ans=0;
void marge_count(int s,int g){
  if(s+1>=g)return;
  if(s+2==g){
    if(a[s]>a[s+1]){
      swap(a[s],a[s+1]);ans++;
    }
    return;
  }
  int m= (s+g)/2;
  marge_count(s,m);
  marge_count(m,g);
  int p=s,q=m,r=s;
  while(p<m&&q<g){
    if(a[p]<a[q]){
      t[r++]=a[p++];
    }else{
      t[r++]=a[q++];
      ans+=m-p;
    }
  }
  if(p<m){
    while(p<m)t[r++]=a[p++];
  }else{
    while(q<g){
      t[r++]=a[q++];
    }
  }
  if(r!=g)cout<<"error"<<endl;
  for(int i=s;i<g;i++){
    a[i]=t[i];
  }
}
int n;
int main(){
    while(cin>>n&&n){ans=0;
  for(int i=0;i<n;i++)cin>>a[i];
  marge_count(0,n);
//  for(int i=0;i<n;i++)cout<<" "<<a[i];
 // cout<<endl;
  cout<<ans<<endl;}
  return 0;
}

C++: #include <stdio.h>
void swap(int &a,int &b){
    int temp=a;
    a=b;
    b=temp;
    return ;
}
const int N=1e5+10;
int main(){
    int n,a[N];
    while(true){
        scanf("%d",&n);
        if(n==0)return 0;
        for(int i=0;i<n;i++)scanf("%d",&a[i]);
        int ans=0;
        for(int i=0;i<n;i++)for(int j=0;j<n-1;j++)if(a[j]<a[j+1]){
            swap(a[j],a[j+1]);
            ans++;
        }
        printf("%d\n",ans);
    }
}

C++: #include <iostream>
using namespace std;
int n,a[100],ans=0;

void solve() {
  int fin = n-1;
  while(fin != 0) {
    for(int i=0;i<fin;i++) {
      if(a[i] > a[i+1]) {
        swap(a[i],a[i+1]);
        ans++;
      }
    }
    fin--;
  }
  return;
}

int main() {
  while(cin >> n) {
    if(n==0) break;
    for(int i=0;i<n;i++) {
      cin >> a[i];
    }
    solve();
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
using namespace std;

int main()
{
	int n;
	int a[100];
	while (cin >> n)
	{
		if (n == 0)
		{
			return 0;
		}
		int cnt = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}

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

	sort(a, a + n);
	for (int i = 0; i < n; i++)
	{
		cout << a[i];
		if ( i != n - 1)
		{
			cout << " ";
		}
	}
	cout << endl;
	return 0;
}

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define MOD 1000000007
#define REP(i, N) for (int i = 0; i < N; ++i)
#define REP1(i, N) for (int i = 1; i <= N; ++i)
#define RREP(i, N) for (int i = N - 1; i >= 0; --i)
#define ALL(a) a.begin(), a.end()

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

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

int main() {
	int n;

	while(cin >> n, n) {
		int a[128];
		bool flag = true;
		int ans = 0;

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

		while(flag) {
			flag = false;
			for(int i = 0; i < n - 1; i++) {
				if(a[i] > a[i + 1]) {
					int tmp = a[i];
					a[i] = a[i + 1];
					a[i + 1] = tmp;
					ans++;
					flag = true;
				}
			}
		}
		cout << ans << endl;
	}

	return 0;
}

C++: #include <stdio.h>
#include<math.h>
#include <algorithm>
using namespace std;

int main(){
	int n,table[100],count;

	while(true){
		scanf("%d",&n);
		if(n == 0)break;

		for(int i=0; i < n; i++){
			scanf("%d",&table[i]);
		}

		count = 0;

		for(int i=n-2; i >= 0; i--){
			for(int k=0; k <= i; k++){
				if(table[k] > table[k+1]){
					swap(table[k],table[k+1]);
					count++;
				}
			}
		}

		printf("%d\n",count);
	}
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define FOR(i, m, n) for(int i=(m); i<(n); ++i)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define Cout(x) cout << (x) << endl
#define Cout2(x, y) cout << (x) << " " << (y) << endl
#define dump(x) cout << #x << " = " << (x) << endl;
#define forauto(i, a) for(auto i : a)cout << i << " "; puts("");
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
const int inf = 1e9;
const double pi = acos(-1.0);

int main() {
	int n;
	while (cin >> n && n) {
		VI a(n); rep(i, n)cin >> a[i];

		int ans = 0;
		for (int i = n - 1; i >= 0; i--) {
			for (int j = 0; j < i; j++) {
				if (a[j] > a[j + 1]) {
					swap(a[j], a[j + 1]);
					ans++;
				}
			}
		}
		Cout(ans);
	}
}

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

int main(void){
	int n;
	while(cin>>n,n){
		int t[n];
		for(int i=0;i<n;i++){
			cin>>t[i];
		}
		if(n == 1){
			cout<<0<<endl;
			continue;
		}
		int ans = 0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(t[j] > t[j+1]){
					ans++;
					swap(t[j],t[j+1]);
				}
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"


int main() {
	while (1) {
		int N;
		cin >> N;
		if (!N)break;
		vector<int>as(N);
		for (int i = 0; i < N; ++i) {
			cin >> as[i];
		}
		int sum = 0;
		for (int i = 0; i < N; ++i) {
			sum += count_if(as.begin() + i + 1, as.end(), [=](const int n) {return n < as[i]; });
		}
		cout << sum << endl;
	}
	return 0;
}

C++: #include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cassert>
using namespace std;

#define MAXN 1000000

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

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 = l, j = m;
    int k = l;
    while (i < m && j < r) {
        if (A[i] <= A[j]) W[k++] = A[i++];
        else {
            W[k++] = A[j++];
            c += m-i;
        }
    }
    while (i < m) {
        W[k++] = A[i++];
    }
    while (j < r) W[k++] = A[j++];
    assert(k == r);
    copy(W+l, W+r, A+l);
    return cl+cr+c;
}

int main(void) {
    while (1) {
        int n;
        cin >> n;
        if (n == 0) break;
        for (int i = 0; i < n; i++) cin >> A[i];
        cout << merge_and_count(0, n) << endl;
    }
    return 0;
}