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<algorithm>
#include<string>
#include<cstring>
using namespace std;
int main(){
	int n;
	while(cin >> n,n){
        int array[120] = {0};
        for(int i = 0; i < n; i++){
            cin >> array[i];
        }
        int cnt = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n-1; j++){
                if(array[j] > array[j+1]){
                    array[j] ^= array[j+1];
                    array[j+1] ^= array[j];
                    array[j] ^= array[j+1];
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
	}
	return 0;
}

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

const int N = 100;
int bit[N+10];

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;
	}
}

vector<int> compress(vector<int> v){
	map<int, int> mp;
	for(int i=0;i<v.size();i++)mp[i] = 0;

	int id = 1;
	for(map<int,int>::iterator it=mp.begin(); it != mp.end(); it++){
		it->second = id++;
	}

	for(int i=0;i<v.size();i++)v[i] = mp[v[i]];

	return v;
}


int main(){
	int n;
	while(cin >> n && n){
		memset(bit, 0, sizeof(bit));

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

		v = compress(v);

		int ans = 0;
		for(int i=n-1;i>=0;i--){
			ans += sum(v[i] - 1);
			add(v[i], 1);
		}

		cout << ans << endl;
	}

	return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int a[120];
int n;
int cnt =0;
void bubblesort(){
	for(int i =0;i < n;i++){
		int pos =0;
		while(a[pos]>a[pos+1]&&pos<(n-i-1)){
			swap(a[pos],a[pos+1]);
			pos++;
			cnt++;
		}
	}
}

int main(){

	while(cin>>n,n){
		cnt = 0;
		for(int i =0;i <120;i++){
			a[i]=0;
		}
		for(int i =0;i < n;i++){
			cin>>a[i];
		}
		/*rep(i,n){
			cout <<a[i];
			if(i!=n-1)cout <<" ";
		}cout <<endl;*/
		bubblesort();
		/*rep(i,n){
			cout <<a[i];
			if(i!=n-1)cout <<" ";
		}
		cout <<endl;*/
		cout <<cnt<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int a[100];
int main(){
	int n;
	while(true){
	cin>>n;
	if(n==0)
		break;
	for(int i=0;i<n;i++)
		cin>>a[i];
	int ans=0;
	for(int i=0;i<n-1;i++){
		for(int j=n-1;j>i;j--){
			if(a[j]<a[j-1]){
				int tmp=a[j];
				a[j]=a[j-1];
				a[j-1]=tmp;
				ans++;
			}
		}
	}
	cout<<ans<<endl;
	}
	return 0;
}

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==g){
      cout<<"aaaaaaaaa"<<endl;
  }
  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;
 // for(int i=0;i<n;i++){cout<<" "<<a[i];}
  //cout<<endl;
    }
  return 0;
}

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

using namespace std;

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

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

using namespace std;

int main(void){
	int n,t;
	while(cin >> n && n){
		int a[n];
		int r=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]){
					swap(a[i],a[j]);
					r++;
				}
			}
		}
		cout << r << endl;
	}
}

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

int n, a[100];
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(false);

  while (1) {
		cin >> n;
		if (n == 0) break;

		int ans = 0;
		for (int i = 0; i < n; i++) 
			cin >> a[i];
		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]);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;

ll a[111];
int n;

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

	}
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int main(void) {
  int i,j;
  int n;
  while(1){
    cin>>n;
    if(n==0)break;
    vi v(n);
    rep(i,n)
      cin>>v[i];
    int ans=0;
    rep(i,n)rep(j,n-i-1)
      if(v[j]>v[j+1]){
        ans++;
        swap(v[j],v[j+1]);
      }
    cout<<ans<<endl;

  }
}

C++: #include<stdio.h>
#include<string.h>

int ele[1000000],i,j,n,c,t;

int main(){
	while(0<=scanf("%d",&n)){
		if(n==0)break;
		c=0;
		
		for(i=0;i<n;i++){
			scanf("%d",&ele[i]);
		}
		
		
		for(i=0;i<n;i++){
			for(j=0;j<n-1;j++){
				if(ele[j]>ele[j+1]){
					++c;
					t=ele[j];
					ele[j]=ele[j+1];
					ele[j+1]=t;
				}
			}
		}
		
		printf("%d\n",c);
	}
	return 0;
}