Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"sort"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

type Team struct {
	Name string
	Win, Lose, Draw int
}
type lessFunc func(p1, p2 *Team) bool
type multiSorter struct {
	teams []Team
	less    []lessFunc
}
func (ms *multiSorter) Sort(teams []Team) {
	ms.teams = teams
	sort.Sort(ms)
}
func OrderedBy(less ...lessFunc) *multiSorter {
	return &multiSorter{
		less: less,
	}
}
func (ms *multiSorter) Len() int {
	return len(ms.teams)
}
func (ms *multiSorter) Swap(i, j int) {
	ms.teams[i], ms.teams[j] = ms.teams[j], ms.teams[i]
}
func (ms *multiSorter) Less(i, j int) bool {
	p, q := &ms.teams[i], &ms.teams[j]
	var k int
	for k = 0; k < len(ms.less)-1; k++ {
		less := ms.less[k]
		switch {
		case less(p, q):
			return true
		case less(q, p):
			return false
		}
	}
	return ms.less[k](p, q)
}

func main() {
	win := func(c1, c2 *Team) bool {
		return c1.Win > c2.Win
	}
	lose := func(c1, c2 *Team) bool {
		return c1.Lose < c2.Lose
	}
	draw := func(c1, c2 *Team) bool {
		return c1.Draw < c2.Draw
	}

	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		teams := make([]Team, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			name := buf[0]
			tmp := make([]int, n-1)
			for i := range tmp {
				tmp[i], _ = strconv.Atoi(buf[i+1])
			}
			cntwin, cntlose, cntdraw := 0, 0, 0
			for _, v := range tmp {
				switch v {
				case 1:
					cntlose++
				case 0:
					cntwin++
				case 2:
					cntdraw++
				}
			}
			teams[i] = Team{name, cntwin, cntlose, cntdraw}
		}
		OrderedBy(win, lose, draw).Sort(teams)
		for _, t := range teams {
			fmt.Println(t.Name)
		}
	}
}

C++: #include<stdio.h>
int main(void)
{
    int n,i,j,h,w[10],l[10],t,u[10];
    char q[11],g;
    while(1){
        for(i=0;i<10;i++){
            w[i]=0; l[i]=0;
        }   
        scanf("%d",&n);
        if(n==0) break;
            for(i=0;i<n;i++){
				u[i]=i;
                scanf(" %c",&q[i]);
                for(j=0;j<n-1;j++){
                    scanf("%d",&h);
                    if(h==0) w[i]++;
                    if(h==1) l[i]++;
                }
            }
            for(i=0;i<n-1;i++){
                for(j=i+1;j<n;j++){
                    if(w[i]<w[j]||(w[i]==w[j]&&l[i]>l[j])||(w[i]==w[j]&&l[i]==l[j]&&u[i]>u[j])){
                        t=w[i]; w[i]=w[j]; w[j]=t;
                         
                        t=l[i]; l[i]=l[j]; l[j]=t;
                         
                        t=u[i]; u[i]=u[j]; u[j]=t;
                     
                        g=q[i]; q[i]=q[j]; q[j]=g;
                    }               
                }   
            }       
            for(i=0;i<n;i++) printf("%c\n",q[i]);
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int,int> P;
typedef pair<P,string> PP;
int n,m;
PP t[10];
int main(){
  while(cin>>n&&n){
    for(int i=0;i<n;i++){
      cin>>t[i].second;
      t[i].first.first=t[i].first.second=0;
      for(int j=0;j<n;j++){
	if(i==j)continue;
	cin>>m;
	if(m==0)t[i].first.first--;
	else if(m==1)t[i].first.second++;
      }
    }
    sort(t,t+n);
    for(int i=0;i<n;i++){cout<<t[i].second<<endl; 
    }
    return 0;
  }
}

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

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

struct Team {
  char name;
  int win,lose,id;
  Team(int i):win(0),lose(0),id(i){}
  bool operator>(const Team& a) const {
    if (win == a.win && lose == a.lose) return id < a.id;
    if (win != a.win) return win > a.win;
    return lose < a.lose;
  }
};

int n;

void solve() {
  set<Team, greater<Team> > teams;
  rep(i,n) {
    Team team(i);
    cin>>team.name;
    rep(j,n-1) {
      int t; cin>>t;
      if (t == 0) team.win++;
      else if (t == 1) team.lose++;
    }
    teams.insert(team);
  }
  
  set<Team>::iterator it = teams.begin();
  while (it != teams.end()) {
    cout<<it->name<<endl;
    it++;
  }
}

int main (int argc, char const* argv[]) {
  while (cin>>n, n) {
    solve();
  }
  return 0;
}

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

#define	mp	make_pair

using namespace std;

int main(){
	for(int n;cin>>n,n;){
		pair< pair<int,int>,pair<int,char> > data[10];
		for(int i=0;i<n;i++){
			char nm;	cin>>nm;
			int res[3]={0};
			for(int j=0,t;j<n-1;j++)	cin>>t,res[t]++;
			data[i]=mp(mp(-res[0],res[1]),mp(i,nm));
		}
		sort(data,data+n);
		for(int i=0;i<n;i++)	cout<<data[i].second.second<<endl;
	}
	return 0;
}

C++: #include<iostream>

using namespace std;

struct sct
{
	char t;
	int r1[11];
	int win;
	int lose;
	int drow;
	int input;
};

int main()
{
	while(true)
	{
		int n = 0;
		sct team[11];
		sct cl = {0, 0, 0, 0, 0, 0}; 

		cin >> n;

		if(n == 0)
		{
			return false;
		}

		for(int i= 0; i < n; i++)
		{
			team[i] = cl;
			team[i].input = i;
			
			cin >> team[i].t;

			for(int j = 0; j < n - 1; j++)
			{
				cin >> team[i].r1[j];
			}
		}

		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < n - 1; j++)
			{
				if(team[i].r1[j] == 0)
				{
					team[i].win++;	
				}
				
				else if(team[i].r1[j] == 1)
				{
					team[i].lose++;
				}
				else if (team[i].r1[j] == 2)
				{
					team[i].drow++;
				}				
			}
		}

		for(int i = 0; i < n; i++)
		{
			for(int j = i; j < n; j++)
			{
				if(team[i].win < team[j].win)
				{
					sct tmp = team[i];
					team[i] = team[j];
					team[j] = tmp;
				}
				else if(team[i].win == team[j].win)
				{
					if(team[i].lose > team[j].lose)
					{
						sct tmp = team[i];
						team[i] = team[j];
						team[j] = tmp;
					}
					else if(team[i].lose == team[j].lose)
					{
						if(team[i].input > team[j].input)
						{
							sct tmp = team[i];
							team[i] = team[j];
							team[j] = tmp;
						} 	
					}
				}
			}
		}

		for(int i = 0; i < n; i++)
		{
			cout << team[i].t << endl;
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
 
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FFOR(i,a,b) for(int i=(a);i<=(b);i++)
#define REP(i,b) FOR(i,0,b)
#define RREP(i,b) FFOR(i,1,b)
#define PB push_back
#define F first
#define S second
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef LL ut;
typedef long double ld;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut> VI;
typedef pair<ut,pr> ppr;
typedef priority_queue<pr,Vpr, greater<pr> > PQ;
const int SIZE=5+3*1e+4;
const ut INF=1<<30;
const ld eps=1e-6;
const LL p=7+1e+9;

int main(){
	int n;
	while(cin >> n && n){
		char teams[16], c;
		ppr res[16];
		REP(i,16){
			res[i] = make_pair(-64, make_pair(64, 64));
		}
		int r;
		REP(i,n){
			cin >> c;
			teams[i] = c;
			res[i].S.S = -i;
			REP(j,n-1){
				cin >> r;
				if(r == 0){
					res[i].F++;
				}
				else if(r == 1){
					res[i].S.F--;
				}
			}
		}
		sort(res, res + n, greater<ppr>());
		REP(i,n){
			cout << teams[-res[i].S.S] << endl;
		}
	}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(void){

	int n;
	char t[10];
	int r;
	int win[10] = { 0 }, lose[10] = { 0 };

	while (cin >> n && n != 0){

		for (int i = 0; i < n; i++){
			cin >> t[i];
			for (int j = 0; j < n - 1; j++){
				cin >> r;
				if (r == 0) win[i]++;
				if (r == 1) lose[i]++;
			}
		}

		for (int i = 0; i < n; i++){
			for (int j = i + 1; j < n; j++){
				if (win[i] < win[j]){
					swap(win[i], win[j]);
					swap(lose[i], lose[j]);
					swap(t[i], t[j]);
				}
				else if (win[i] == win[j]){
					if (lose[i] > lose[j]){
						swap(win[i], win[j]);
						swap(lose[i], lose[j]);
						swap(t[i], t[j]);
					}
				}
			}

			cout << t[i] << endl;
		}
	}
}