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<utility>
#include<cmath>
using namespace std;

int kaijyou(int x){
int b=1;
for(int i=1;i<x;i++){
b*=i;
}
return b;
}

int main(){
int n,t;
int ans;
double count;
while(cin>>n){
if(n==0)return 0;
int a[n];
t=0;
ans=0;
count=0;
for(int i=0;i<n;i++){
cin >> a[i];
}
for(int j=0;t==0;j++){
if(j+1==n){
j=0;
count+=1;
}
if(a[j]>a[j+1]){
swap(a[j],a[j+1]);
ans+=1;
}
if(count>kaijyou(n)){
cout << ans << endl;
t=1;
}
}
}

return 0;
}

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

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;

ll a[111];
int n;

int main(){
	while (cin >> n&&n){
		REP(i, 100)a[i] = 0;
		ll co = 0;
		REP(i,n){
			cin >> a[i];
		}
		for (int i = n; i > 0; i--){
			for (int j = 0; j < i - 1; j++){
				if (a[j] > a[j + 1]){
					ll k = a[j];
					a[j] = a[j + 1];
					a[j + 1] = k;
					co++;
				}
			}
		}
		//cout << "!";
		cout<< co << endl;

	}
}

C++: #include <iostream>
#include <string.h>
using namespace std;
//BIT
long bit[1000001],n;
long MAX;
long sum(int i){
	long s=0;
	while(i>0){
		s+=bit[i];
		i-=i&-i;
	}
	return s;
}
void add(long i,long x){
	while(i<=MAX){
		bit[i]+=x;
		i+=i&-i;
	}
}
long a[100];
bool input(){
	cin >> n; if(!n) return false;
	for(int i=0;i<n;i++) cin >> a[i];
	MAX=a[0];
	for(int i=0;i<n;i++) MAX=max(MAX,a[i]);
	return true;
}
int main() {
	while(input()) {
		long long ans=0;
		for(int i=0;i<n;i++) {
			ans+=i-sum(a[i]);
			add(a[i],1);
		}
		cout << ans << '\n' ;
		memset(bit,0,sizeof(bit)); memset(a,0,sizeof(a));
	}
}

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)return;
  if(s+2==g){
    if(a[s]>a[s+1]){
      swap(a[s],a[s+1]);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++];
    }else{
      t[r++]=a[q++];
      ans+=m-p;
    }
  }
  if(p<m){
    while(p<m)t[r++]=a[p++];
  }else{
    while(q<g){
      t[r++]=a[q++];
    }
  }
  if(r!=g)cout<<"error"<<endl;
  for(int i=s;i<g;i++){
    a[i]=t[i];
  }
}
int n;
int main(){
    while(cin>>n&&n){ans=0;
  for(int i=0;i<n;i++)cin>>a[i];
  marge_count(0,n);
//  for(int i=0;i<n;i++)cout<<" "<<a[i];
 // cout<<endl;
  cout<<ans<<endl;}
  return 0;
}

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= vec.size()-1;i>0;i--)
	for(int j=0;j<i;j++)
	  if(vec[j+1] < vec[j])
	    ans++,swap(vec[j+1],vec[j]);

      cout << ans << endl;

    }
  return 0;
}

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

using namespace std;

int main(){
	
	while(true){
		
		int n;
		
		int a[200];
		
		cin >> n;
		
		if(n == 0){
			break;
		}
		
		int ans = 0;
		
		for(int i = 0; i < n; i++){
			cin >> a[i];
		}
		
		for(int i = 0; i < n - 1; i++){
			for(int j = i + 1; j < n; j++){
				if(a[i] > a[j]){
					ans++;
				}
			}
		}
		
		cout << ans << endl;
		
	}
	
	return 0;
}

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

using namespace std;


int main(){
	int size, A[100], k;
	
	while( cin >> size ) {
		
	if( size == 0)
	break;
	
	k = 0;
	
	for(int i=0;i<size;i++)
	cin >> A[i];

	for(int j = size-1; j > 0; j--) 
		for(int i = 0; i < j; i++)
			if(A[i] > A[i + 1]) {
				
				swap(A[i], A[i + 1]);
				k++;
				
		}
			 
	cout << k << "\n";
	
}
	return 0;
	
}

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

constexpr int kaijyou(const int &n) {
  return n == 0 ? 1 : n * kaijyou(n - 1);
}

int main() {
  int n, t;
  int ans;
  double count;
  while (cin >> n) {
    if (n == 0) return 0;
    int a[n];
    t     = 0;
    ans   = 0;
    count = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int j = 0; t == 0; j++) {
      if (j + 1 == n) {
        j = 0;
        count += 1;
      }
      if (a[j] > a[j + 1]) {
        swap(a[j], a[j + 1]);
        ans += 1;
      }
      if (count > kaijyou(n)) {
        cout << ans << endl;
        t = 1;
      }
    }
  }
  return 0;
}

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

int main(){
  int n,i,j,k,c,a[1000000];

  while(cin>>n&&n!=0){
    for(i=0;i<n;i++){
      cin>>a[i];
    }
    
    for(k=0,i=0;i<n-1;i++){
      for(j=i;j<n;j++){
	if(a[i]>a[j]){
	  c=a[i];
	  a[i]=a[j];
	  a[j]=c;
	  k++;
	} 
      }
    }
    cout<<k<<endl;
  }
return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> P;

vector<int> ans;

void solve(int n) {
    vector<int> a(n);
    rep(i, n) cin >> a[i];

    int res = 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]);
                res++;
            }
        }
    }
    ans.push_back(res);
    return ;
}

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

    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << endl;
    }

    return 0;
}

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

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

C++: #include<bits/stdc++.h>
using namespace std;

int BIT[500];
int sum(int a) {
	int b = 0;
	while (a > 0) {
		b += BIT[a];
		a -= a&-a;
	}
	return b;
}
void add(int a) {
	while (a <= 300) {
		BIT[a] ++;
		a += a&-a;
	}
}
int main() {
	int a;
	while (cin >> a, a) {
		fill(BIT,BIT+101, 0);
		int c = 0;
		for (int b = 0; b < a; b++) {
			int d;
			scanf("%d", &d);
			c += b - sum(d);
			add(d);
		}
		cout << c << endl;
	}
}

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

int a[102],n;

int main(void){
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		int ans=0;
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		for(int i=0;i<n;i++){
			for(int j=i;j<n-1;j++){
				if(a[j]>a[j+1]){
					ans++;
					swap(a[j],a[j+1]);
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}