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

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

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)

const int MAX_N=100;

int N;
int a[MAX_N];

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	do{
		cin>>N;
		if (N){
			REP(i,N){
				cin>>a[i];
			}
			int ans=0;
			REP(i,N){
				REP(j,N-i-1){
					if (a[j]>a[j+1]){
						ans++;
						swap(a[j],a[j+1]);
					}
				}
			}
			cout<<ans<<endl;
		}
	}while (N);
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <stack>


using namespace std;

int main(int, char**){
    int x;
    while(cin >> x,x){ 
	int vec[128];
	int cnt(0);
	for(int i(0);i<x;++i){
	    cin >> vec[i];
	}
	for(int i(x-1);i>0;--i){
	    for(int j(0);j<i;++j)
		if(vec[j] > vec[j+1]){ swap(vec[j],vec[j+1]); ++cnt; }
	}
	cout << cnt << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,tmp;
	while(cin>>n,n){
		int a[100],r=n-1,cnt=0;
		for(int i=0;i<n;i++)cin>>a[i];
		for(int i=0;i<n-1;i++){
			for(int j=0;j<r;j++){
				if(a[j]>a[j+1]){
					tmp=a[j];a[j]=a[j+1];a[j+1]=tmp;
					cnt++;
				}
			}
			r--;
		}
		cout<<cnt<<endl;
	}
}

C++: #include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int main()
{
    for(int n; cin >> n && n;)
	{
		int res = 0;
		vint v(n);
		
		rep(i, n) cin >> v[i];
		
		for(int j = n - 1; 0 < j; j--) for(int i = 0; i < j; i++) 
		{
			if(v[i] > v[i + 1])
			{
				swap(v[i], v[i - 1]);
				res++;
			}
		}
		
		cout << res << endl;
	}		
}

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

using namespace std;
typedef long long ll;

#define rep(i,a) for(int i = 0 ; i < a ; i ++)

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

		cout<<cnt<<endl;
	}
	return 0;
}

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

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

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
#define INF 1000000000;
using namespace std;

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

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

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
using namespace std;

#define gcu getchar_unlocked
#define pcu putchar_unlocked
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _in _il int in
#define _sc _il bool scan
_T T in(int c){T n=0;bool m=false;while(isspace(c)){c=gcu();}if(c=='-')m=true,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m?-n:n;}
_in() {return in<int>(gcu());}
_T T scan(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _vo _il void out
#define _vl _il void outl
_vo(bool b) {pcu('0'+b);}
_vo(const char *s){while(*s)pcu(*s++);}
_vo(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_vo(string s){for(char c:s)pcu(c);}
#endif
_T _vo(T n){static char buf[20];char *p=buf;
	if(n<0)pcu('-'),n*=-1;if(!n)*p++='0';else while(n)*p++=n%10+'0',n/=10;
	while (p!=buf)pcu(*--p);}
_vl(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _vo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _vo(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _vl(T&&... t){out(move(t)...);outl();}

int main() {
	for (int n; (n = in());) {
		vector<int> A(n);
		for (int &a: A)
			a = in();
		int c = 0;
		for (int i = n - 1; i; i--)
			for (int j = 1; j <= i; j++)
				if (A[j - 1] > A[j]) {
					c++;
					swap(A[j - 1], A[j]);
				}
		outl(c);
	}
}

C++: #include<iostream>
#define MAX 100
using namespace std;
int ans;
void BubbleSort(int *a, int n){
	if(n==1)return;
	for(int i=0;i<n;i++){
		for(int j=0;j<n-i-1;j++){
			if(a[j]>a[j+1]){
				swap(a[j], a[j+1]);
				++ans;
			}
		}
	}
}
int main(){
	while(true){
		int n;
		int a[MAX];
		scanf("%d", &n);
		if( n==0 ) break;
		for(int i=0;i<n;++i){
			int t;
			scanf("%d", &t);
			a[i]=t;
		}
		ans=0;
		BubbleSort(a,n);
		printf("%d\n", ans);
	}
	return 0;
}