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;
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define rrep(i,n) for(int i=(int)(n)-1; i>=0; i--)
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin(); it!=(c).end(); it++)
#define pb(a) push_back(a)
#define pr(a) cout << (a) << endl
#define PR(a,b) cout << (a) << " " << (b) << endl
#define F first
#define S second
typedef long long ll;
typedef pair<int,int> P;
const int MAX=1000000001;
const ll MAXL=1000000000000000001LL;
const ll mod=1000000007;
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int main() {
  int n;
  while(cin >> n && n) {
    int a[n];
    rep(i,n) cin >> a[i];
    if(n==1) pr(0);
    else {
      int ans=0;
      for(int i=1; i<n; i++) {
	rrep(j,i) {
	  if(a[j]>a[j+1]) swap(a[j],a[j+1]);
	  else break;
	  ans++;
	}
      }
      pr(ans);
    }
  }
  return 0;
}

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

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

using namespace std;

#define MAX_N 101

int a[MAX_N];

int main() {
    int n;
    while (cin >> n) {
        if (n == 0) break;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        int count = 0;
        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]);
                    count++;
                }
            }
        }
        cout << count << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <cstdio>
#include <algorithm>
using namespace std;
int arr[101];

int main() {
    int sw, N;
    for (; ;) {
        scanf("%d", &N);
        if (N == 0) break;
        for (int i = 0; i < N; i++) scanf("%d", &arr[i]);
        bool flag = true; sw = 0;
        for (int i = 0; flag; i++) {
            flag = false;
            for (int j = N-1; j >= i+1; j--) {
                if (arr[j] < arr[j-1]) {
                    swap(arr[j], arr[j-1]);
                    sw++;
                    flag = true;
                }
            }
        }
        std::printf("%d\n", sw);
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#define MAX_N 1000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 1e-8;

int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		int* a = new int[n];
		for(int i=0;i<n;i++){
			scanf("%d",a+i);
		}

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

		printf("%d\n",res);

		delete[] a;
	}
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int h,i;
  int a[100],n,c;
  for(;;){
    cin>>n;
    if(n==0)
      break;
    for(i=0;i<n;i++)
      cin>>a[i];
    c=0;
    do{
      h=0;
      for(i=1;i<n;i++){
	if(a[i-1]>a[i]){
	  swap(a[i-1],a[i]);
	  h=1;
	  c++;
	}
      }
    }while(h);
    cout<<c<<endl;
  }
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <memory>
#include <memory.h>
#include <fstream>
#include <cmath>
#include <math.h>
#include <numeric>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <map>
#include <iomanip>
#include <list>
#include <cctype>
#include <algorithm>
#include <complex>

#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

int nums[200];

int solve(int n){
	int cnt = 0;
	while(n > 1){
		rep(i, n-1){
			if(nums[i] > nums[i+1]) {
				cnt++;
				swap(nums[i], nums[i+1]);
			}
		}
		n--;
	}
	return cnt;
}

int main(){
	int n;
	while(cin >> n && n){
		rep(i, n) cin >> nums[i];
		cout << solve(n) << endl;
	}
	return 0;
}

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

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

long long int a[500],n;

int main(void){
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		long long int ans=0;
		for(int i=0;i<n;i++){
			cin >> a[i];
		}
		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++;
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

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

int data[101];
int main()
{
	int n;
	while(cin >> n){
		if(n==0)
			break;
		for(int i=0;i<n;i++){
			cin >> data[i];
		}
		int cnt = 0;
		for(int i=0;i<n;i++){
			for(int j=n-1;j>i;j--){
				if(data[j] < data[j-1]){
					int tmp = data[j];
					data[j] = data[j-1];
					data[j-1] = tmp;
					cnt++;
				}
			}
		}
		
		cout << cnt << endl;
	}
	return 0;
}

C++: #include<iostream>
#define MAX 100
using namespace std;
int ans;
void BubbleSort(int *a, int n){
	for(int i = 0; i < n; i++)for(int j = n - 1; j > i; 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;
}

C++: #include <cstdio>

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

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

int bubblesort(vector<int> a){
    int cnt = 0;
    for(int i=0; i<a.size()-1; i++){
        for(int j=a.size()-1; j>i; j--){
            if(a[j]<a[j-1]){
                int tmp = a[j];
                a[j] = a[j-1];
                a[j-1] = tmp;
                cnt++;
            }
        }
    }

    return cnt;
}

int main(){
    int n;

    while(cin >> n, n != 0){
        vector<int> a(n);
        for(int i=0; i<n; i++){
            cin >> a[i];
        }

        int result = bubblesort(a);

        cout << result << endl;
    }
    return 0;
}