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 "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define ALL(c) c.begin(),c.end()
#define LOOP(i) while(i--)
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

ll MergeSort(vector<int>& A){
	int num = A.size();
	if(num<=1) return 0;

	ll cnt=0;
	vector<int> B(A.begin(),A.begin()+num/2);
	vector<int> C(A.begin()+num/2,A.end());

	cnt += MergeSort(B);
	cnt += MergeSort(C);

	int ai=0,bi=0,ci=0;
	int bnum=B.size(),cnum=C.size();
	while(ai<num){
		if(bi<bnum && (ci==cnum || B[bi]<=C[ci])){
			A[ai++] = B[bi++];
		} else {
			cnt += num/2-bi;
			A[ai++] = C[ci++];
		}
	}
	return cnt;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	while(true){
		ll N=in();
		if(!N) break;

		auto V=vecin(N);
		out(MergeSort(V));
	}
	
	return 0;
}

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

using namespace std;

int main() {
  int N;
  
  while(cin >> N && N) {
    
    vector<int> vec(N);
    for(int i=0; i<N; i++) cin >> vec[i];
    
    int cnt = 0;
    for(int i=0; i<N; i++) {
      for(int j=0; j<N-i-1; j++) {
	if(vec[j] > vec[j+1]) {
	  cnt ++;
	  swap(vec[j], vec[j+1]);
	}
      }
    }
    
    cout << cnt << 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++;
				}
			}
			N--;
		}
		
		cout << cnt << endl;
	}
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

static const double eps = 1e-8;
int n;
int cnt;
int main(){
	while(cin>>n&&n){
		int A[n];
		for (int i = 0; i < n; ++i) {
			cin>>A[i];
		}
		cnt=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]);
					cnt++;
				}
			}
		}
		cout<<cnt<<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 += floor(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;

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, a * 4);
		int c = 0;
		for (int b = 0; b < a; b++) {
			int d;
			scanf("%d", &d);
			c += b - BIT[d];
			add(d);
		}
		cout << c << endl;
	}
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int n;
  while (cin >> n, n) {
    vi a(n);
    rep(i, n) cin >> a[i];
    int totalSwapTime = 0;
    for (;;) {
      int st = 0;
      loop(i, 1, n) {
        if (a[i - 1] <= a[i]) continue;
        swap(a[i - 1], a[i]);
        st++;
      }
      totalSwapTime += st;
      if (!st) break;
    }
    cout << totalSwapTime << endl;
  }
}

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

void swap( int* a, int* b )
{
  int tmp;
  tmp = *a;
  *a = *b;
  *b = tmp;
}

int BubbleSort( int* lst, int length )
{
  int count = 0;
  for (int i=1; i<length; i++)
  {
    for (int j=0; j<length-i; j++)
    {
      if ( lst[j] > lst[j+1] )
      {
        swap( &lst[j], &lst[j+1] );
        count++;
      }
    }
  }
  return count;
}

int main()
{
  while(true)
  {
    int n;
    cin >> n;
    if ( n == 0 ) break;
    int* arr = new int[n];
    for (int i=0; i<n; i++)
    {
      cin >> arr[i];
    }

    int count = BubbleSort( arr, n );

    cout << count << endl;

    delete[] arr;
  }
}