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"
using namespace std;
using ll = long long; using ull = unsigned long long;
//#define int ll
using vb = vector<bool>; using vvb = vector<vb>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
template<class T> using V = vector<T>;
template<class T> using VV = vector<V<T>>;
#define fi first
#define se second
#define maxs(x,y) (x=max(x,y))
#define mins(x,y) (x=min(x,y))
using pii = pair<int,int>; using pll = pair<ll,ll>;
#define FOR(i,a,b) for(ll i = (a); i < (ll)(b); ++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(ll i = (ll)(b)-1;i >= a;--i)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define eb(val) emplace_back(val)
const double PI = acos(-1),EPS = 1e-10;
const ll MOD = 1e9+7;
void cioacc(){//accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
ll w;
int bubble_sort(vector<int> &vec){
  int res = 0;
  for(int i=0;i<(int)vec.size();++i){
    for(int j=0;j<(int)vec.size()-i-1;++j){
      if(vec.at(j)>vec.at(j+1))  swap(vec.at(j),vec.at(j+1)),++res;
    }
  }  
  return res;
}
int main(){
  int n;
  while(cin >> n,n){
    vector<int> vec(n);
    REP(i,n){
      cin >> vec.at(i);
    }
    //REP(i,n){
    //  cout << vec.at(i) << endl;
    //}
    cout << bubble_sort(vec) << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>	// require sort next_permutation etc.
//#include <fstream>	// require freopen

using namespace std;

int main()
{
//	cut here before submit 
//	freopen ("testcase.bb", "r", stdin );
	int n, m;

	while (cin >> n && n ){
		vector <int> a(n);
		int res = 0;
		int i,j;

		for (i = 0; i < n; ++i ) {
			cin >> a[i];
		} // /end for
		for (i = 0; i < n; ++i ) {
			for (j = 0; j < n - i - 1; ++j ) {
				if (a[j] > a[j+1] ) {
					swap (a[j], a[j+1] );
					++res;
				} // end for
 			} // end for
		} // end for
		cout << res << endl;
	} // end loop

	return 0;
}

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


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

int main(){
	int a[100],n;
	while(cin>>n&&n){
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		cout<<inversion(a,n)<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cassert>
#include <algorithm>
using namespace std;
int a[201],w[201],n;
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;
    else{
      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 ret=cl+cr;
  int i=l,j=m,k=l;
  while(i<m&&j<r&&k<r){
    if(a[i]<=a[j])w[k++]=a[i++];
    else{
      w[k++]=a[j++];
      ret+=m-i;
    }
  }
  while(i<m)w[k++]=a[i++];
  while(j<r)w[k++]=a[j++];
  assert(k==r);
  for(i=l;i<r;i++){
    a[i]=w[i];
  }
  //cout<<"marge"<<l<<" "<<r<<" "<<ret<<endl;
  return ret;
}
int main(){
  while(cin>>n&&n){
    for(int i=0;i<n;i++){
      cin>>a[i];
    }
    cout<<merge_and_count(0,n)<<endl;
  }
}

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<i; j++) {
	if(vec[j] > vec[i]) {
	  cnt ++;
	  swap(vec[i], vec[j]);
	}
      }
    }
    
    cout << cnt << 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=i+1;j<n;j++){
      if(a[i]>a[j]){
        swap(a[i],a[j]);
        sum++;
      }
    }
    cout<<sum<<endl;
  }
}

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

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

	return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n;
	int a[101];
	int cnt;
	int i,j,temp;
	
	scanf("%d",&n);
	while(n!=0){
		cnt=0;
		for(i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		for(i=0;i<n;i++){
			for(j=0;j<n-1;j++){
				if(a[j]>a[j+1]){
					cnt++;
					temp=a[j];
					a[j]=a[j+1];
					a[j+1]=temp;
				}
			}
		}
		printf("%d\n",cnt);
		scanf("%d",&n);
	}
	return 0;
}

C++: #include<stdio.h>
int main()
{
	int n,a[100],i,j,c;
	while(scanf("%d",&n),n)
	{
		for(i=0; i<n; ++i) scanf("%d",&a[i]);
		for(c=i=0; i<n; ++i) for(j=1; j<n; ++j) if(a[j-1]>a[j]) a[j-1]^=a[j],a[j]^=a[j-1],a[j-1]^=a[j],++c;
		printf("%d\n",c);
	}
	return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  while(cin>>n&&n){
    vary(int,v,n+1,0);
    rep(i,n){
      cin >> v[i];
    }
    v[n]=inf;
    int ans = 0;
    int len = n;
    while(len != 1){
      int i = 0;
      while(i < len){
        if(v[i] > v[i+1]){
          swap(v[i],v[i+1]);
          ++ans;
        }
        ++i;
      }
      --len;
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

int n, a[100], ct = 0;


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