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++: #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 = i; j+1 < n; 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 bubbleSort(int A[], int N) {
    int c = 0;
    bool flag = true;

    for (int i = 0; flag; i++) {
        flag = false;
        for (int j = N-1; j >= i+1; j--) {
            if (A[j] < A[j-1]) {
                swap(A[j], A[j-1]);
                flag = true;
                c++;
            }
        }
    }
    return c;
}

int main()
{
    int A[101];
    int N;

    while (cin >> N) {

        if (N == 0)
            break;

        for (int i = 0; i < N; i++)
            cin >> A[i];

        cout << bubbleSort(A, N) << endl;
    }

    return 0;
}

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() {
  goto yarudake;
yarudake:
  int n;
  while (cin >> n, n) {
    vi a(n);
    rep(i, n) cin >> a[i];
    int totalSwapTime = 0;
    for (;;) {
      int partialSwapTime = 0;
      loop(i, 1, n) {
        if (a[i - 1] <= a[i]) continue;
        swap(a[i - 1], a[i]);
        partialSwapTime++;
      }
      totalSwapTime += partialSwapTime;
      if (!partialSwapTime) break;
    }
    cout << totalSwapTime << endl;
  }
}

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

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

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100;

int a[MAX_N];
int k[MAX_N];
int bit[MAX_N+1],n;
map<int,int> mp;

//i???????????§??????????¨??????????(i???1????????????)
int sum(int i)
{
	int s = 0;
	while(i>0){
		s += bit[i];
		i -= i & -i;
	}
	return s;
}
//i???????????????x????????????(i???1????????????)
void add(int i,int x)
{
	while(i <= n){
		bit[i] += x;
		i += i & -i;
	}
}

int main()
{
	while(1){
	cin >> n;
	if(n==0){
		break;
	}
	rep(i,n){
		scanf("%d",&a[i]);
		k[i] = a[i];
		mp[a[i]] = i;
	}
	sort(a,a+n);
	rep(i,n){
		k[mp[a[i]]] = i+1;
	}
	fill(bit,bit+n+1,0);
	ll ans = 0;
	rep(j,n){
		ans += j - sum(k[j]);
		add(k[j],1);
	}
	printf("%lld\n",ans);
	}
	return 0;
}

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 ; i ++)
			for(int j = 1 ; 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<stdio.h>
 
int main(){
	int a, b[100], i, c, count = 0, j;
	while(scanf("%d", &a) != 0){
		if(a == 0) break;
		for(i = 0; i < a; i++) scanf("%d", &b[i]);
		for(j = a - 1; j >= 1; j--){
			for(i = 0; i < j; i++){
				if(b[i + 1] < b[i]){
						c = b[i];
						b[i] = b[i + 1];
						b[i + 1] = c;
						count++;
				}
			}
		}
		printf("%d\n", count);
		count = 0;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
const int INF = 2000000;

int main() {
  int n;
  while (cin >> n and n != 0) {
    int* arr = new int[n];

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

    int val = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < i; j++)
        if (arr[j] > arr[i]) val++;

    cout << val << endl;
  }
}

C++: #include <iostream>
#include <string.h>
using namespace std;
//BIT
long bit[1000001],n;
long sum(int i){
	long s=0;
	while(i>0){
		s+=bit[i];
		i-=i&-i;
	}
	return s;
}
void add(long i,long x){
	while(i<=n){
		bit[i]+=x;
		i+=i&-i;
	}
}
long a[100];
bool input(){
	cin >> n; if(!n) return false;
	for(int i=0;i<n;i++) cin >> a[i];
	return true;
}
int main() {
	while(input()) {
		long long ans=0;
		for(int i=0;i<n;i++) {
			ans+=i-sum(a[i]);
			add(a[i],1);
		}
		cout << ans << '\n' ;
		memset(bit,0,sizeof(bit));
	}
}