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++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;


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

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define INF 1<<30

int main() {
  int n;
  while(cin >> n,n) {
    int v[n];
    REP(i,n)
      cin >> v[i];
    int res = 0;
    REP(i,n) {
      REP(j,n-i-1) {
        if (v[j] > v[j+1]) {
          //  printf("%d,%d, %d,%d\n",i,j,v[j],v[j+1]);
          swap(v[j],v[j+1]);
          res++;
        }
      }
    }
    cout << res << endl;
  }
}

C++: #include <stdio.h>
void swap(int &a,int &b){
    int temp=a;
    a=b;
    b=temp;
    return ;
}
const int N=1e5+10;
int main(){
    int n,a[N];
    while(true){
        scanf("%d",&n);
        if(n==0)return 0;
        for(int i=0;i<n;i++)scanf("%d",&a[i]);
        int ans=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]);
            ans++;
        }
        printf("%d\n",ans);
    }
}

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

using namespace std;

#define MAX 100
 
int array[MAX];
 
int bubble_sort(int n, int array[])
{
    int tmp, cnt = 0;
     
    for (int i = n - 1; i > 0; i--) {
        for (int j = 0; j < i; j++) {
            if (array[j+1] < array[j]) {
                tmp = array[j];
                array[j] = array[j+1];
                array[j+1] = tmp;
                cnt++;
            }
        }    
    }
    return cnt;
}
 
 
int main()
{
    int n, array[MAX];
     
    while (cin >> n, n) {
        for (int i = 0; i < n; i++) {
            cin >> array[i];
        }
        int ans = 0;
        ans = bubble_sort(n, array);
        cout << ans << endl;
    }
    return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0} , dy[] = {0,1,0,-1};
#define INF 999999
int main(){
    int n;
    while(scanf("%d",&n),n){
        int so[128]={0};
        for(int i = 0; i < n; i++)scanf("%d",&so[i]);

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

C++: #include<functional>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<string>
#include<math.h>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
using namespace std;
const int INF=100000000;
int main(){
  int n;
  while(cin>>n,n!=0){
    int a[20];
    int c=0;
    rep(i,n)cin>>a[i];
    for(int i=0;i<n-1;i++){
      for(int j=i+1;j<n;j++){
	if(a[i]>a[j])c++;
      }
    }
    cout<<c<<endl;
  }
  return 0;
}

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

using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  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; i++) {
      for (int j = 0; j < i; j++) {
        ans += a[j] > a[i];
      }
    }
    cout << ans << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;
int ans;

void bubblesort(vector<int>& a){
    REP(i,a.size()){
	for(int j=0;j<a.size()-1;j++){
	    if(a[j] > a[j+1]){
		swap(a[j],a[j+1]);
		ans++;
	    }
	}
    }
}

int main(){
    while(true){
	int n;
	vector<int> a;
	ans = 0;
	cin >> n;
	if(n == 0) break;
	REP(i,n){
	    int temp;
	    cin >> temp;
	    a.PB(temp);
	}
	bubblesort(a);
	cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

int N, k, sum, A[100];

int bubble_sort_count(int r){
	sum = 0;
	for (int i = 0; i < r - 1;i++){
		for (int j = 0; j < r - i - 1;j++){
			if (A[j] > A[j + 1]){
				k = A[j + 1];
				A[j + 1] = A[j];
				A[j] = k;
				sum += 1;
			}
		}
	}
	return sum;
}

int main()
{
	cin >> N;
	while (N != 0){
		for (int n = 0; n < N; n++){
			cin >> A[n];
		}
		cout << bubble_sort_count(N) << endl;
		cin >> N;
	}
}

C++: #include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>
#include <climits>
#include <functional>

#define REP(i,n) for(int i = 0;i < n;i++)

using namespace std;
typedef long long ll;
const int INF = INT_MAX / 4;

const int max_a = 1e6;


class BIT
{
public:
  vector<int> bit;
  int size;

  BIT(int size)
    : bit(vector<int>(size + 1, 0)), size(size)
  {}

  int sum(int i) {
    int r = 0;
    while (i > 0) {
      r += bit[i];
      i -= (i & -i);
    }
    return r;
  }

  void add(int i, int x) {
    while (i <= size) {
      bit[i] += x;
      i += (i & -i);
    }
  }
};


int main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(1) {
    int N;
    cin >> N;
    if (N == 0) return 0;

    int a[N];
    REP(i, N) cin >> a[i];
    BIT bit(max_a);
    int ans = 0;
    REP(i, N) {
      ans += i - bit.sum(a[i]);
      bit.add(a[i], 1);
    }
    cout << ans << "\n";
  }
}