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 <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef complex<double> P;
typedef long long ll;

const int MAX_SIZE = 10000;

int main(){

	int n;
	while(cin>>n&&n!=0){

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

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

	return 0;
}

C++: #include<complex>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<map>
#include<sstream>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<deque>
#include<queue>
#include<climits>
#include<list>
#include<stack>
#include<ctime>
#define all(n) n.begin(),n.end()
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define insert(a,b,c,d) PP(P(a,b),P(c,d))
#define F first
#define S second
#define Iter(c) __typeof__((c).begin())
#define foreach(i,c) for(Iter(c) i = (c).begin();i != (c).end();++i)
#define pb push_back
#define INF (1<<27)
#define EPS 1e-10
#define equals(a,b) (fabs((a)-(b)) < EPS)

using namespace std;
typedef pair<int,int> P;
typedef pair<P,P> PP;
typedef unsigned long long ull;
typedef long long ll;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0};

int n,a[10000000];

int bit[10000001];

int sum(int i)
{
  int s = 0;
  while(i > 0)
    {
      s += bit[i];
      i -= i & -i;
    }
  return s;
}

void add(int i,int x)
{
  while(i <= n)
    {
      bit[i] += x;
      i += i & -i;
    }
}

void solve()
{
  ll ans = 0;
  rep(j,n)
    {
      ans += j-sum(a[j]);
      add(a[j],1);
    }
  cout << ans << endl;
}

int main(){
  
  while(cin >> n && n)
    {
      rep(i,n)
	{
	  cin >> a[i];
	}
      solve();
    }
  return 0;
}

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

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

int n, a[102];

int solve() {
  rep (i,n) cin >> a[i];
  //rep (i,n) printf("%2d", a[i]); puts("");

  int ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = 0; j < n - (i+1); ++j) {
      if (a[j] > a[j+1]) { swap(a[j], a[j+1]); ans++; }
    }
  }
  //rep (i,n) printf("%2d", a[i]); puts("");
  return ans;
}

int main() {
  while (cin >> n, n) cout << solve() << 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) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> A[i];
    cout << merge_and_count(0, n) << endl;
    return 0;
}

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

int main(){
  int n,sort[1111111],cou,tmp;
  while(1){
    cou=0;
    cin >> n;
    if(n == 0) break;
    for(int i=0;i<n;i++){
      cin >> sort[i];
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n-i-1;j++){
	if(sort[j] > sort[j+1]){
	  swap(sort[j],sort[j+1]);
	  cou++;
	}
      }
    }
    cout << cou << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;

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

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
int main(){
    int n;
    while(cin>>n,n){
        vi v(n,0);rep(i,n)cin>>v[i];int ans=0;
        rep(j,n-1)rep(i,n-1)if(v[i]>v[i+1]){swap(v[i],v[i+1]);ans++;}
        cout<<ans<<endl;
    }
}

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

int main(){
  int n,res;
  int a[100];

  while(1){
    cin >> n;
    if(!n)break;

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

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

    cout << res << endl;
  }
}

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;
  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++];
      ans+=q-m;
    }else{
      t[r++]=a[q++];
    }
  }
  if(p<m){
    while(p<m){
      t[r++]=a[p++];ans+=q-m;
    }
  }else{
    t[r++]=a[q++];
  }
  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);
  cout<<ans<<endl;}
  return 0;
}

C++: #include<iostream>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(void){
  int n;
  while(cin>>n,n){
    int a[n],sum=0;
    r(i,n)cin>>a[i];
    r(i,n)for(int j=1;j<n;j++){
      if(a[i]>a[j]){
        swap(a[i],a[j]);
        sum++;
      }
    }
    cout<<sum<<endl;
  }
}