Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

type entry struct {
	id, score int
}

type lessFunc func(p1, p2 *entry) bool

type multiSorter struct {
	entries []entry
	less    []lessFunc
}

func (ms *multiSorter) sort(entries []entry) {
	ms.entries = entries
	sort.Sort(ms)
}

func OrderedBy(less ...lessFunc) *multiSorter {
	return &multiSorter{
		less: less,
	}
}

func (ms *multiSorter) Len() int {
	return len(ms.entries)
}

func (ms *multiSorter) Swap(i, j int) {
	ms.entries[i], ms.entries[j] = ms.entries[j], ms.entries[i]
}

func (ms *multiSorter) Less(i, j int) bool {
	p, q := &ms.entries[i], &ms.entries[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() {
	for scanner.Scan() {
		m, _ := strconv.Atoi(scanner.Text())
		if m == 0 {
			break
		}
		results := make([]entry, 0)
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			data := make([]int, len(buf))
			for i := range data {
				data[i], _ = strconv.Atoi(buf[i])
			}
			id, score := score(data)
			results = append(results, entry{id, score})
		}
		score_axis := func(e1, e2 *entry) bool {
			return e1.score > e2.score
		}
		id_axis := func(e1, e2 *entry) bool {
			return e1.id < e2.id
		}
		OrderedBy(score_axis, id_axis).sort(results)
		// log.Println(results)
		for _, e := range results {
			fmt.Printf("%d %d\n", e.id, e.score)
		}
	}
}

func score(input []int) (int, int) {
	id := input[0]
	data := input[1:]
	frame := 1
	scores := make([]int, 11)
	for frame <= 10 {
		if data[0] == 10 {
			scores[frame] = data[0] + data[1] + data[2]
			data = data[1:]
			frame += 1
		} else if data[0]+data[1] == 10 {
			scores[frame] = data[0] + data[1] + data[2]
			data = data[2:]
			frame += 1
		} else {
			scores[frame] = data[0] + data[1]
			data = data[2:]
			frame += 1
		}
	}
	sum := 0
	for i, v := range scores {
		if i != 0 {
			sum += v
		}
	}
	return id, sum
}

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

struct data
{
	int num,ptr;
	bool operator<(const data &c) const
	{
		return (ptr!=c.ptr ? ptr>c.ptr : num<c.num);
	}
};

int main()
{
	int t,i,j,n;
	data tmp;
	while(cin >> n, n)
	{
		vector<data> rank;
		for(int k=0; k<n; k++)
		{
			vector<int> score;

			cin >> tmp.num;
			while(cin.peek() != '\n')
			{
				cin >> t;
				score.push_back(t);
			}
			score.push_back(0);
			score.push_back(0);

			tmp.ptr=0;
			for(i=j=0; i<10; i++)
			{
				if(score[j]==10)
				{
					tmp.ptr += score[j]+score[j+1]+score[j+2];
					j+=1;
				}
				else if(score[j]+score[j+1]==10)
				{
					tmp.ptr += score[j]+score[j+1]+score[j+2];
					j+=2;
				}
				else
				{
					tmp.ptr += score[j]+score[j+1];
					j+=2;
				}
			}
			rank.push_back(tmp);
		}
		sort(rank.begin(), rank.end());
		for(int k=0; k<n; k++)
		{
			cout << rank[k].num << " " << rank[k].ptr << endl;
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

int m,id[40];
pair<int,int> p[40];

int main(){
	while(cin>>m&&m){
		rep(i,m){
			cin>>p[i].second;
			int f=1,n,b=0,b2=0,sc=0,sec=0,prv,thi=0;
			while(f<11){
				cin>>n;
				sc+=n;
				if(b2>0){sc+=n*2;b2--;b--;if(!b){b=b2;b2=0;}}
				else if(b>0){sc+=n;b--;}
				//cout<<f<<"-"<<sec+1<<" "<<n<<" "<<sc<<endl;
				if(f==10){
					if(sec==2)break;
					sec+=1;
					if(n==10||sec>1&&prv+n==10)thi=1;
					//cout<<sec<<thi<<endl;
					if(sec==2&&!thi)break;
				}
				else{
					if(!sec){
						if(n==10){f++;if(b>0)b2=2;else b=2;}
						else sec=1;
					}
					else{
						sec=0;f++;
						if(prv+n==10)b=1;
					}
				}
				prv=n;
			}
			p[i].first=-sc;
		}
		sort(p,p+m);
		rep(i,m)cout<<p[i].second<<" "<<-p[i].first<<endl;
	}
}

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

int calc_score(vector<int> pins)
{
	int score = 0;
	int frame = 1;
	int frame_cnt = 1;
	int now = 0;

	rep(i,pins.size())
	{
		if (frame != 10)
		{
			if (frame_cnt == 1)
			{
				if (pins[i] == 10)
				{
					//????????¬????????????????????§??????????????????????????????
					score += 10;
					for (int j=i+1; j<=i+2 && j<pins.size(); j++)
						score += pins[j];
					frame++;
					frame_cnt = 1;	
					continue;
				} else {
					score += pins[i];
					frame_cnt++;
					continue;
				}
			} else if (frame_cnt == 2) {
				if (pins[i-1] + pins[i] == 10)
				{
					//????????¢???????????????
					score += pins[i];
					score += pins[i+1];
					frame++;
					frame_cnt = 1;
					continue;
				} else {
					score += pins[i];
					frame++;
					frame_cnt = 1;
					continue;
				}
			}
		} else if (frame == 10) {
			if (frame_cnt == 1)
			{
				if (pins[i] == 10)
				{
					score += 10;
					//for (int j=i+1; j<=i+2 && j<pins.size(); j++)
					//	score += pins[j];
					frame_cnt = 2;
					continue;
				} else {
					score += pins[i];
					frame_cnt = 2;
					continue;
				}
			} else if (frame_cnt == 2) {
				if (pins[i] == 10)
				{
					score += 10;
					//for (int j=i+1; j<=i+2 && j<pins.size(); j++)
					//	score += pins[j];
					frame_cnt = 3;
					continue;
				} else if (pins[i-1] + pins[i] == 10) {
					score += pins[i];
					//score += pins[i+1];
					frame_cnt = 3;
					continue;
				} else {
					score += pins[i];
					break;
				}
			} else if (frame_cnt == 3) {
				score += pins[i];
			}
		}
	}

	return score;
}

int m;
vector<int> ps;
pair<int, int> lst[100];

signed main()
{
	while (true)
	{
		cin >> m;
		if (m == 0) break;
		cin.ignore();
		string str;
		rep(i,m)
		{
			int num;
			ps.clear();

			getline(cin, str);
			//cout << "getline = " << str << endl;
			stringstream ss(str);

			ss >> num;
			//printf("num = %d\n", num);
			for (int x; ss >> x;)
			{
				ps.push_back(x);
			}

			lst[i] = make_pair(-calc_score(ps), num);
			sort(lst, lst + m);
		}

		rep(i,m)
		{
			cout << lst[i].second << " " << -lst[i].first << endl;
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(void)
{
	int i,j,k,m,score[42][22],id[42],x,cnt,yoso[42],sum[42],flg[42][22],p,check[42];
	
	while(1) {
		cin>>m;
		if(m==0) break;
		for(i=0;i<m;i++) yoso[i]=sum[i]=check[i]=0;
		for(i=0;i<m;i++) {
			cnt=21;
			cin>>id[i];
			x=p=0;
			for(j=0;j<21;j++) flg[i][j]=-1;
			while(cnt>0) {
				cin>>score[i][x];
				if(cnt<=3) {
					if(score[i][x]==10) cnt--,flg[i][x]=2;
					else if(cnt==1&&score[i][x]+score[i][x-1]==10) flg[i][x]=flg[i][x-1]=1;
					else cnt-=2,flg[i][x]=0;
				}	
				else {
					if(score[i][x]==10) cnt-=2,flg[i][x]=2;
					else if(p==1&&score[i][x]+score[i][x-1]==10) flg[i][x]=flg[i][x-1]=1,p=0,cnt--;
					else if(p==1) cnt--,flg[i][x]=0,p=0;
					else cnt--,flg[i][x]=0,p=1;
				}
				x++;
			}
			yoso[i]=x;
		}
		int a;
		for(i=0;i<m;i++) {
			a=0;
			for(j=0;j<yoso[i];j++) {
				if(a!=9) {
					if(flg[i][j]!=2) j++,a++;
					else a++;
				}
				else check[i]++;
			}
		}
		for(i=0;i<m;i++) {
			for(j=0;j<yoso[i]-check[i];j++) {
				if(flg[i][j]==0) sum[i]+=score[i][j]+score[i][j+1],j++;
				else if(flg[i][j]==1) {
					sum[i]+=score[i][j]+score[i][j+1]+score[i][j+2];
					j++;
				}
				else sum[i]+=score[i][j]+score[i][j+1]+score[i][j+2];
			}
			if(check[i]==3) sum[i]+=score[i][j]+score[i][j+1]+score[i][j+2];
			else sum[i]+=score[i][j]+score[i][j+1];
		}
		for(i=0;i<m-1;i++) {
			for(j=i+1;j<m;j++) {
				if(sum[i]<sum[j]) swap(sum[i],sum[j]),swap(id[i],id[j]);
				else if(sum[i]==sum[j]&&id[i]>id[j]) swap(id[i],id[j]); 
			}
		}
		for(i=0;i<m;i++) cout<<id[i]<<" "<<sum[i]<<endl;
	}
}