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;

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[j], vec[j+1]);
	}
      }
    }
    
    cout << cnt << 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>

int BubbleSort(int [],int n);

int main()
{
  int n;
  int i;
  int A[110];
  int cnt=0;

  while(scanf("%d",&n),n)
    {
      for(i=0;i < n;i++){
	scanf("%d",&A[i]);
      }
      
      cnt = BubbleSort(A,n);
      printf("%d\n",cnt);
    }
  return 0;
}

int BubbleSort(int A[],int n)
{
  int i,j;
  int cnt = 0;
  int temp;
  
  for(i=0;i < n;i++){
    for(j=n-1;j > i;j--){
      if(A[j-1] > A[j]){
	temp = A[j-1];
	A[j-1] = A[j];
	A[j] = temp;
	cnt++;
      }
    }
  }
  
  /*for(i=0;i < n;i++){
    if(i) printf(" ");
    printf("%d",A[i]);
  }
  printf("\n");
  */
  return cnt; 
}

C++: #include<iostream>

int main(){
	const int MAX_N = 100;
	int n, a[MAX_N];
	while(std::cin >> n, n){
		for(int i=0;i<n;i++){
			std::cin >> a[i];
		}

		int t = 0;
		while(true){
			bool update = false;
			for(int i=0;i<n-1;i++){
				if(a[i] > a[i+1]){
					std::swap(a[i], a[i+1]);
					t++;
					update = true;
				}
			}
			
			if(!update){
				break;
			}
		}

		std::cout << t << std::endl;
	}
}

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

using namespace std;

void solve()
{
	int n;
	while(cin >>n, n)
	{
		vector<int> Vec(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> Vec[i];
		}
		int count = 0;
		for(int i = 0; i < n - 1; ++i)
		{
			for(int j = n - 1; j > i; --j)
			{
				if(Vec[j - 1] > Vec[j])
				{
					int temp = Vec[j];
					Vec[j] = Vec[j - 1];
					Vec[j - 1] = temp;
					count++;
				}
			}
		}
		cout << count << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
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 = n-1; i >= 0; i--) {
      for(int j = 0; j < i; j++) {
	if(v[j] > v[j+1]) {
	  swap(v[j], v[j+1]);
	  ans++;
	}
      }
    }
    cout << ans << endl;
  }
}

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

using namespace std;

int main()
{
  int n;
  while(cin >> n,n)
    {
      vector<int> vec;
      vec.resize(n);
      for(int i=0;i<n;i++)
	cin >> vec[i];

      int ans = 0;

      for(int i=0;i<n;i++)
	for(int j=i+1;j<n;j++)
	  if(vec[i] > vec[j])
	    ans++,swap(vec[i],vec[j]);

      cout << ans << endl;

    }
  return 0;
}

C++: // 2011/01/12 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		int num[100];
		for(int i=0; i<n; i++)
			cin >> num[i];

		int cnt = 0;
		for(int i=n-1; i>=0; i--)
			for(int j=0; j<i; j++)
				if(num[j]>num[j+1]){
					int tmp = num[j];
					num[j] = num[j+1];
					num[j+1] = tmp;
					cnt++;
				}

		cout << cnt << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
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(){
	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[i]);

		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 <iostream>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main(){
	int n;
	while(cin >> n, n) {
		int a[110] = {}, cnt = 0;
		rep(i,n) {
			cin >> a[i];
		}
		rep(i,n-1) {
			rep(j,n-i-1) {
				if(a[j] > a[j+1]) {
					swap(a[j],a[j+1]);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
	}
    return 0;
}

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

using namespace std;

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

    int ans = 0;
    for (int i = 0; i < n - 1; i++) {
      for (int j = i + 1; j < n; j++) {
        if (a[j] < a[i]) {
          ans++;
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI=6*asin(0.5);
typedef pair<double,double> P;
typedef complex<double> CP;
typedef long long ll;

int a[110];

int main(){
	int n;
	while(cin>>n,n){
		rep(i,110) a[i]=0;
		rep(i,n){
			cin>>a[i];
		}
		ll res=0;
		for(int i=n;i>1;i--){
			for(int j=0;j<i-1;j++){
				if(a[j]>a[j+1]){
					int t;
					t=a[j+1];
					a[j+1]=a[j];
					a[j]=t;
					res++;
				}
			}
		}
		cout<<res<<endl;
	}
	return 0;
}