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
	}

	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).Sort(teams)
		for _, t := range teams {
			fmt.Println(t.Name)
		}
	}
}

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

using namespace std;

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

    if(n == 0){
      break;
    }
    
    vector<pair<int,char>> total_result;
    int result;

    for(int i = 0;i < n;++i){
      int point = 0;
      char name;
      cin>>name;

      for(int j = 0;j < n - 1;++j){
        cin>>result;
        if(result == 0){
          point += 100;
        }
        else if(result == 2){
          point += 1;
        }
      }

      total_result.push_back(make_pair(point,name));
    }

    sort(total_result.begin(),total_result.end());
    reverse(total_result.begin(),total_result.end());

    for(int i = 0;i < n;++i){
      cout<<total_result[i].second<<endl;
    }
  }
  return 0;
}

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

typedef pair<int, int> P;
typedef pair<P, int> T;
int main(){
  int n, x;
  char c;
  while(cin >> n, n){
    map<T, char> mem;
    for(int i = 0;i < n;i++){
      T tmp = T(P(0, 0), i);
      cin >> c;
      for(int j = 1;j < n;j++){
	cin >> x;
	if(x == 0)tmp.first.first--;
	if(x == 1)tmp.first.second++;
      }
      mem[tmp] = c;
    }
    map<T, char>::iterator it = mem.begin();
    while(it != mem.end()){
      cout << it->second << endl;
      it++;
    }
  }
  return 0;
}

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

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

#define f first
#define s second
#define mp make_pair

#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()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int main(){
  int n;
  while(scanf("%d",&n), n){
    vector<pair<int,pair<int,pair<int,char> > > > v(n);
    REP(i,n){
      char c;
      scanf(" %c ",&c);
      int w=0, l=0;
      REP(j,n-1){
	int a; scanf("%d",&a);
	if(a == 0) w++;
	else if(a==1) l++;
      }
      v[i] = mp(-w, mp(l, mp(i, c)));
    }
    sort(v.begin(), v.end());
    REP(i,n)
      printf("%c\n",v[i].s.s.s);
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define INF (1<<21)
int main(){
	int n;
	while(cin >> n,n){
		vector< pair<int,char> > data(n);
		rep(i,n){
			int sum = 0;
			cin >> data[i].second;
			rep(j,n-1){
				int t;
				cin >> t;
				sum += (t==0?10:t==2?1:0);
			}
			data[i].first = sum;
		}
		stable_sort(data.begin(),data.end(),greater< pair<int,char> >());
		rep(i,n){
			cout << data[i].second << endl;
		}
	}
}

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;
}