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;
struct team {
  string name;
  int win;
  int lose;
};

class winner {
public:
  bool operator() (const team& a, const team& b) {
    if(a.win != b.win) {
    return a.win > b.win;
    }
    else {
      return a.lose > b.lose;
    }
  }
};

vector<team> T;

int main() {
  int n,res;
  while(cin >> n) {
    if(n==0) break;
    for(int i=0;i<n;i++) {
      team temp;
      temp.win = 0;temp.lose = 0;
      cin >> temp.name;
      for(int j=0;j<n-1;j++) {
        cin >> res;
        if(res == 0) temp.win++;
        if(res == 1) temp.lose--;
        if(res == 2) continue;
      }
      //      cout << temp.name << " " << temp.win << " " << temp.lose << endl;
      T.push_back(temp);
    }

    sort(T.begin(),T.end(), winner());
    for(int i=0;i<n;i++) {
      cout << T[i].name << endl;
      }
    T.clear();
  }
}

C++: #include<stdio.h>
int main(void)
{
	int n,p[100];
	int i,j,temp;
	int w[100],l[100],a[100];
	char t[100],m;
	scanf("%d",&n);
	while(n!=0){
		for(i=0;i<n;i++){
			w[i]=0;
			l[i]=0;
			scanf(" %c",&t[i]);
			a[i]=i;
			for(j=0;j<n-1;j++){
				scanf("%d",&p[j]);
				if(p[j]==0){
					w[i]++;
				}
				if(p[j]==1){
					l[i]++;
				}
			}
		}
		for(i=0;i<n-1;i++){
			for(j=i+1;j<n;j++){
				if(w[i]<w[j]){
					temp=w[i];
					w[i]=w[j];
					w[j]=temp;
					m=t[i];
					t[i]=t[j];
					t[j]=m;
					temp=a[i];
					a[i]=a[j];
					a[j]=temp;
				}
				else if(w[i]==w[j] && l[i]<l[j]){
					temp=w[i];
					w[i]=w[j];
					w[j]=temp;
					m=t[i];
					t[i]=t[j];
					t[j]=m;
					temp=a[i];
					a[i]=a[j];
					a[j]=temp;
				}
				else if(w[i]==w[j] && a[i]>a[j]){
					temp=w[i];
					w[i]=w[j];
					w[j]=temp;
					m=t[i];
					t[i]=t[j];
					t[j]=m;
					temp=a[i];
					a[i]=a[j];
					a[j]=temp;				
				}
			}
		}
		for(i=0;i<n;i++){
			printf("%c\n",t[i]);
		}
		scanf("%d",&n);
	}
	return 0;
}

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

int main(){
  int n;

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

    char t[n];
    int id[n], win[n], lose[n];

    for(int i = 0; i < n; i++){
      id[i] = i;
      cin >> t[i];

      win[i] = 0;
      lose[i] = 0;

      for(int j = 0; j < n - 1; j++){
        int x;
        cin >> x;

        if(x == 0) win[i]++;
        else if(x == 1) lose[i]++;
      }
    }

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

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

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

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

		char t[10];
		int r;
		int win[10] = { 0 }, lose[10] = { 0 };
		memset(win, 0, sizeof(win)); memset(lose, 0, sizeof(lose));

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

		bool flg = true;
		int i = 0;
		while (flg){
			flg = false;
			for (int j = n - 1; j >= i + 1; j--){
				if (win[j] > win[j - 1]){
					swap(win[j], win[j - 1]); swap(lose[j], lose[j - 1]); swap(t[j], t[j - 1]);
					flg = true;
				}
				else if (win[j] == win[j - 1]){
					if (lose[j] < lose[j - 1]){
						swap(win[j], win[j - 1]); swap(lose[j], lose[j - 1]); swap(t[j], t[j - 1]);
						flg = true;
					}
				}
			}
			i++;
		}
		for (i = 0; i < n;i++){
			cout << t[i] << endl;
		}
	}
}

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 %d\n",t[i],x[i]);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<char,pair<int,int> > p;
int main() {
	int n;
	while(cin>>n,n){
		vector<p>a;
		for(int i=0;i<n;i++){
			p data;cin>>data.first;
			data.second.first=0;
			data.second.second=0;
			for(int j=0;j<n-1;j++){
				int p;cin>>p;
				if(p==0)data.second.first++;
				else if(p==1)data.second.second++;
			}
			a.push_back(data);
		}
		for(int i=0;i<n;i++){
			int f=0;
			for(int j=1;j<a.size();j++){
				if(a[j].second.first>a[f].second.first)f=j;
				else if(a[j].second.first==a[f].second.first&&a[j].second.first<a[f].second.first)f=j;
			}
			cout<<a[f].first<<endl;
			a.erase(a.begin()+f);
		}
	}
	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
pair<int, string>a[100000];
int n, sum, p;
int main() {
	while (true) {
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> a[i].second;
			sum = 0;
			for (int j = 0; j < n - 1; j++) {
				cin >> p;
				if (p == 0) { sum += 10000; }
				if (p == 2) { sum += 1; }
			}
			a[i].first = sum;
		}
		sort(a, a + n);
		for (int i = n - 1; i >= 0; i++) {
			cout << a[i].second << endl;
		}
	}
}

C++: #include<iostream>
using namespace std;
int main()
{
	int n,score;
	int tokuten[10]={0};
	char name[10];
	while(1){
		cin >> n;
		if(n == 0)
			break;
		for(int i=0;i<n;i++){
			cin >> name[i];
			for(int j=0;j<n-1;j++){
				cin >> score;
				if(score ==0)
					tokuten[i] += 2;
				else if(score == 2)
					tokuten[i] += 1;
			}
		}
		for(int i=0;i<n-1;i++){
			for(int j=i+1;j<n;j++){
				if(tokuten[i] < tokuten[j]){
					int T = tokuten[i];
					tokuten[i] = tokuten[j];
					tokuten[j] = T;
					char TT = name[i];
					name[i] = name[j];
					name[j] = TT;
				}
			}
		}
		for(int i=0;i<n;i++)
			cout << name[i] << endl;
	}
	return 0;
}