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 _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define N_MAX 1000

using namespace std;

int cnt = 0;

/* aとbの値を入れ替える */
void swap(int *a, int *b)
{
	int tmp;

	tmp = *a;
	*a = *b;
	*b = tmp;
}

/* バブルソート */
void bubble_sort(int a[], int n)
{
	int i, j;

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

int main()
{
	int n;
	int a[105];

	while (1) {
		cin >> n;
		if (n == 0) break;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		cnt = 0;
		bubble_sort(a, n);
		cout << cnt << endl;
	}

	

	return (0);
}

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==g){
      //cout<<"aaaaaaaaa"<<endl;
  }
  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{
        ans+=m-p;
      t[r++]=a[q++];
    }
  }
  if(p<m){
    while(p<m){
      t[r++]=a[p++];
    }
  }else{
      while(q<g){
    t[r++]=a[q++];ans+=m-p;}
  }
  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);
  cout<<ans<<endl;
 // for(int i=0;i<n;i++){cout<<" "<<a[i];}
  //cout<<endl;
    }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int n;
    while(cin>>n,n){
        int a[100];
        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>
#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[j-1],a[j]);
        sum++;
      }
    }
    cout<<sum<<endl;
  }
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	for (int n; cin >> n&&n;) {
		vector<int> a(n);
		rep(i, 0, n)cin >> a[i];
		vector<int> s = a;
		sort(all(s));

		bool flag = true;
		int ans = 0;
		while (flag) {
			flag = false;
			rep(h, 0, n - 1) {
				rep(i, 0, n - 1 - h) {
					if (a[i] > a[i + 1]) {
						swap(a[i], a[i + 1]);
						ans++;
						flag = true;
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
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){
		vi a(n);
		rep(i,n)cin>>a[i];
		cout<<mergecount(a)<<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;j<n;j++)
	  if(vec[i] > vec[j])
	    ans++,swap(vec[i],vec[j]);

      cout << ans << endl;

    }
  return 0;
}

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

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

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


#define N (111)
int seg[N+1];

void add(int x,int v){
	for(int i = x ; i < N ; i+=i&-i){
		seg[i] += v;
	}
}

int get(int x){
	int ans = 0;
	for(int i = x ; i > 0 ; i -= i & -i){
		ans += seg[i];
	}
	return ans;
}

int a[101];

int main(){
	int n;
	while(cin >> n && n){
		vector<int> u;
		for(int i = 0 ; i < 101 ; i++) seg[i] = 0;
		
		for(int i = 0 ; i < n ; i++) cin >> a[i] , u.push_back(a[i]);
		sort(u.begin(),u.end());
		u.erase(unique(u.begin(),u.end()),u.end());
		for(int i = 0 ; i < n ; i++)
			a[i] = lower_bound(u.begin(),u.end(),a[i]) - u.begin() + 1;
		int ans = 0;
		for(int i = 0 ; i < n ; i++){
			add(a[i],1);
			ans += (i+1) - get(a[i]);  //a[i]より大きな値
		}
		cout << ans << endl;while(1){}
	}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
main(){int n,t,i;while(cin >> n && n){int a[n];int r=0;for(i=0;i<n;i++)cin >> a[i];for(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++: #define _USE_MATH_DEFINES
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<list>
#include<stack>
#include<queue>
#include<cctype>
#include<iomanip>
#include<functional>
#include<numeric>
#include<map>
#include<set>
#include<complex>
#define EPS 1e-10
using namespace std;
typedef long long llong;
int main() {
	int n;
	while (cin >> n, n) {
		vector<int>v(n);
		for (int i = 0; i < n; i++)cin >> v[i];
		int cnt = 0;
		for (int i = 0; i < n - 1; i++) {
			for (int j = n - 1; i < j; j--) {
				if (v[j - 1] > v[j]) {
					swap(v[j - 1], v[j]);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}