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++: /*
	Writing Segment Tree is Fun !
*/
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;


struct SegTree {

	vector<int> dat;
	int s;

	void init(int size)
	{
		s = 1;
		while(s < size)
			s *= 2;
		dat.resize(s * 2, 0);
	}

	void add(int pos, int x)
	{
		pos += s;

		while(pos > 0) {
			dat[pos] += x;
			pos = pos / 2;
		}
	}

	int sum(int a, int b, int k, int l, int r)
	{
		if(b <= l || a >= r)
			return 0;
		if(a <= l && r <= b)
			return dat[k];

		int mid = (l + r) / 2;
		int lv = sum(a, b, k * 2 + 0, l, mid);
		int rv = sum(a, b, k * 2 + 1, mid, r);

		return lv + rv;
	}

	int sum(int a, int b)
	{
		return sum(a, b, 1, 0, s);
	}
};


int main()
{
	while(true) {

		int n;
		vector<pair<int, int> > x;

		scanf("%d", &n);
		if(n == 0)
			break;
		x.resize(n);
		for(int i = 0; i < n; ++i) {
			int v;
			scanf("%d", &v);
			x[i] = make_pair(v, i);
		}

		SegTree st;

		st.init(n);
		sort(x.begin(), x.end());

		int ans = 0;

		for(int i = 0; i < n; ++i) {
			int p = x[i].second;
			ans += p - st.sum(0, p);
			st.add(p, 1);
		}

		printf("%d\n", ans);
	}

	return 0;
}

C++: <iostream>
using namespace std;

int main()
{
  int n;
  for(;cin>>n,n;)
    {
      int data[n];
      long long int step=0;
      for(int i=0;i<n;i++)
	cin>>data[i];
      
      for(int i=0;i<n;i++)
	for(int j=0;j<n-i-1;j++)
	  {
	    if(data[j]>data[j+1])
	      {
		swap(data[j],data[j+1]);
		step++;
	      }
	  }
      cout<<step<<endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

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

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

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

C++: #include<iostream>
using namespace std;
int main(){
	int n;
	while (cin >> n, n){
		int sort[100], hoge, cnt = 0;
		for (int i = 0; i < n; i++){
			cin >> sort[i];
		}
		for (int i = 0; i < n - 2; i++){
			for (int j = 0; j < n - 1 - i; j++){
				if (sort[j] > sort[j + 1]){
					hoge = sort[j];
					sort[j] = sort[j + 1];
					sort[j + 1] = hoge;
					cnt++;
				}
			}
		}
		cout << cnt << "\n";
	}
	return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<string.h>
#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)

using namespace std;

int main(){
	int n,v[100],cnt;
	for(;;){
		scanf("%d",&n);
		if(n==0)break;
		rep(i,n)scanf("%d",v+i);
		cnt=0;
		for(int i=n-1;i>0;i--)reps(j,1,i)if(v[j]<v[j-1]){
			swap(v[j],v[j-1]);
			cnt++;
		}
		printf("%d\n",cnt);
	}
	return 0;
}

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


using namespace std;
#define rep(i, n) for(int i = 0; i < int(n); i++)

int BIT[1000001];

int sum(int idx)
{
	int ret = 0;
	while (idx > 0)
	{
		ret += BIT[idx];
		idx -= idx & -idx;
	}
	return ret;
}

const int N = 1000000;
void add(int idx, int num)
{
	while (idx <= N)
	{
		BIT[idx] += num;
		idx += idx & -idx;
	}
}

signed main()
{
	int n;
	while (cin >> n && n)
	{
		memset(BIT, 0, sizeof(BIT));
		int ans = 0;
		vector<int> arr(n);
		rep(i, n) cin >> arr[i];
		rep(i, n)
		{
			ans += i - sum(arr[i]);
			add(arr[i], 1);
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp make_pair
#define ERASE(v,i) (v).erase(remove(all(v),i),(v).end())
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define debug(x) cerr<<#x<<" = "<<(x)<<endl;
#define LINE cerr<<"LINE: "<<__LINE__<<endl;
inline int toInt(string s){int v;istringstream i(s);i>>v;return v;}
template<class T> inline string toString(T x){ostringstream o;o<<x;return o.str();}
template<class T> void pv(T a,T b){for(T it=a;it!=b;++it)cerr<<*it<<" ";cerr<<endl;}
const int INF = 1000000000;
const double PI = acos(-1.0), EPS = 1e-10;

int main() {
	for(int N; cin >> N, N;) {
		vector<int> v(N);
		rep(i,N) cin >> v[i];
		
		int cnt = 0;
		for(int i = 0; i < N; i++) {
			for(int j = 0; j < N - 1; j++) {
				if(v[j] > v[j+1]) {
					swap(v[j], v[j+1]);
					cnt++;
				}
			}
		}
		
		cout << cnt << endl;
	}
}

C++: #include<iostream>
using namespace std;
int n,a[100];
main()
{
    while(cin>>n,n)
    {
        for(int i=0;i<n;i++)cin>>a[i];
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            for(int j=i;j<n;j++)cnt+=a[i]>a[j];
        }
        cout<<cnt<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
#define PP(m) REP(i, m.size()) cout << m[i] << endl;

int main() {
  int N;

  while (cin >> N && N) {
    int c=0;
    vector<int> arr(N);
    REP(i, N) cin >> arr[i];
    REP(i, N-1) {
      REP(j, N-i-1) {
        if (arr[j] > arr[j+1] && arr[j] != arr[j+1]) {
          swap(arr[j], arr[j+1]);
          c++;
        }
      }
    }
    cout << c << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI=6*asin(0.5);
typedef pair<double,double> P;
typedef complex<double> CP;
typedef long long ll;

int a[110];

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

C++: #include<bits/stdc++.h>
using namespace std;

int BIT[110];
int sum(int a) {
	int b = 0;
	while (a > 0) {
		b += BIT[a];
		a -= a&-a;
	}
	return b;
}
void add(int a) {
	while (a < 110) {
		BIT[a] ++;
		a += a&-a;
	}
}
int main() {
	int a;
	while (cin >> a, a) {
		memset(BIT, 0, sizeof(BIT));
		int c = 0;
		for (int b = 0; b < a; b++) {
			int d;
			scanf("%d", &d);
			c += b - sum(d);
			add(d);
		}
		cout << c << endl;
	}
}

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

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