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>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define REP(i,a,n) for(int i = a ; i < n ; i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int mergecount(vector<int> &a){
  int count = 0;
  int n = a.size();
  if(n > 1){
    vector<int> b(a.begin(),a.begin() + n/2);
    vector<int> c(a.begin() + n/2, a.end());
    count += mergecount(b);
    count += mergecount(c);
    for(int i=0,j=0,k=0;i<n;i++){
      if(k == c.size()) a[i] = b[j++];
      else if(j == b.size()) a[i] = c[k++];
      else if(b[j] <= c[k]) a[i] = b[j++];
      else {a[i] = c[k++]; count += n/2 - j;}
    }
  }
  return count;
}

int main(){
  int n;
  while(cin>>n,n){
    vector<int> v(n);
    rep(i,n)cin>>v[i];
    cout<<mergecount(v)<<endl;
  }
}

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

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

int main(void){
  int n;
  while(cin >> n, n != 0){
    int *arr = new int[n];
    for(int i = 0; i < n; i++){
      cin >> arr[i];
    }
    int cnt = bubblesort(arr, n);
    cout << cnt << endl;
  }
  return 0;
}

C++: #include<cstdio>
int main(){
	int a[102],i,j,k;
	for(;scanf("%d",a),*a;printf("%d\n",k)){
		for(i=1;i<=n; i++)scanf("%d",*(a+i++));
		k=0;
		for(j=n;j>1;j--)
			for(i=2;i<=j;i++)
				if(a[i-1]>a[i])
					a[i-1]^=a[i],a[i]^a[i-1],a[i-1]^=a[i],k++;
	}
}

C++: #include<iostream>

using namespace std;

int main(){

  int n;

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

    int data[n];
    int sum = 0;

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

    cout << sum << endl;
  }
  return 0;
}

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

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

int BIT[1000001];

int sum(int idx)
{
	int ret = 0;
	while (idx != 0)
	{
		ret += BIT[idx];
		idx -= idx & -idx;
	}
	return ret;
}

int n;
void add(int idx, int num)
{
	while (idx <= n)
	{
		BIT[idx] += num;
		idx += idx & -idx;
	}
}

signed main()
{
	while (cin >> n && n)
	{
		memset(BIT, 0, sizeof(BIT));
		int ans = 0;
		vector<int> arr(n);
		rep(i, n) cin >> arr[i];
		rep(i, n)
		{
			ans += sum(arr[n - i - 1]);
			add(arr[n - i - 1], 1);
		}
		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<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=1;j<n;j++){
      if(a[j-1]>a[j]){
        swap(a[i],a[j]);
        sum++;
      }
    }
    cout<<sum<<endl;
  }
}

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

int main(){
    while(1){
        int n;
        cin >> n;

        if(n == 0)  break;

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

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

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

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


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

int BIT[1000001];

int sum(int idx)
{
	int ret = 0;
	while (idx != 0)
	{
		ret += BIT[idx];
		idx -= idx & -idx;
	}
	return ret;
}

int n;
void add(int idx, int num)
{
	while (idx <= n)
	{
		BIT[idx] += num;
		idx += idx & -idx;
	}
}

signed main()
{
	while (cin >> n && n)
	{
		memset(BIT, 0, sizeof(BIT));
		int ans = 0;
		vector<int> arr(n);
		rep(i, n) cin >> arr[i];
		rep(i, n)
		{
			ans += sum(arr[n - i - 1]);
			add(arr[n - i - 1], 1);
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
using namespace std;
int main(){

	int n;
	
	while(cin>>n,n!=0){
		int c=0;
		vector<int> s(n);
		for(int i=0;i<n;i++)
			cin>>s[i];
		
		for(int i=0;i<n;i++){
			for(int j=0;j<n-1;j++){
				if(s[j]>s[j+1]){
				swap(s[j],s[j+1]);
					c++;
				}
			}
			
		}
		cout<<c<<endl;
	}
	
	
}

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

using namespace std;

#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;




int main() {
	int n;
	int ans, t;
	vi v(110, 0);
	while (cin >> n) {
		if (n == 0)break;
		ans = 0;
		rep(i, n) {
			cin >> v[i];
		}
		rep(i, n) {
			for (int j = 0; j < n - i - 1; j++) {
				if (v[j] > v[j + 1]) {
					t = v[j];
					v[j] = v[j + 1];
					v[j + 1] = t;
					ans++;
				}
			}
		}
		/*rep(i, n) {
			cout << v[i] << " ";
		}
		cout << endl;*/
		cout << ans << endl;
	}
}

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

using namespace std;

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

C++: #include<iostream>
#include<cstdio>
#include<time.h>
using namespace std;
#define N 314159
int a[N],t[N];
long long ans=0;
void marge_count(int s,int g){
  if(s+1==g){
    if(a[s]>a[g]){
      swap(a[s],a[g]);ans++;
    }return;
  }
  int m= (s+g)/2;
  marge_count(s,m);
  marge_count(m,g);
  int p=s,q=m,r=s;
  while(p<m&&q<g){
    if(a[p]<a[q]){
      t[r++]=a[p++];
      ans+=q;
    }else{
      t[r++]=a[q++];
    }
  }
  if(p<m){
    while(p<m){
      t[r++]=a[p++];ans+=q;
    }
  }else{
    t[r++]=a[q++];
  }
  for(int i=s;i<g;i++){
    a[i]=t[i];
  }
}
int main(){
　while(cin>>n&&n!=0){
    ans=0;
    for(int i=0;i<n;i++)cin>>a[i];
    marge_count(0,n);
    cout<<(int)ans<<endl;
  }
  return 0;
}