Go: package main

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

var (
	sc = bufio.NewScanner(os.Stdin)
)

func main() {
	for sc.Scan() {
		if sc.Text() == "0 0" {
			break
		}
		chunks := strings.Split(sc.Text(), " ")
		n, _ := strconv.Atoi(chunks[0])
		p, _ := strconv.Atoi(chunks[1])
		cnts := make([]int, n, n)
		for i := 0; true; i++ {
			j := i % n
			if p == 0 {
				p, cnts[j] = cnts[j], 0
			} else {
				cnts[j]++
				p--
			}

			if p == 0 {
				noneCnt := 0
				winner := 0
				for j, c := range cnts {
					if c == 0 {
						noneCnt++
					} else {
						winner = j
					}
				}
				if noneCnt == n-1 {
					fmt.Println(winner)
					break
				}
			}
		}
	}
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
#define N 50
int i,n,p,tmp;
int ca[N];
bool sen(int tmp){
	int ans = 0;
	for(int i=0;i<n;i++){
		if(ca[i]==tmp) ans++;
	}
	if(ans) return false;
	else return true;
}

int main(){
	while(cin >> n >> p){
	//cin >> n >> p;	
		if(n==0&&p==0) break;
		tmp = p;
		for(i=0;i<n;i++) ca[i]=0;
		
		int k,j = 0;
		while(sen(tmp)){
			k = j%n;
			if(p==0){
				p += ca[k];
				ca[k] = 0;
			}else{
				p -= 1;
				ca[k]++;
			}
			j++;
		}
	    for(i=0;i<n;i++){
	    	if(ca[i]==tmp) cout << i <<endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h> 
using namespace std;
#define int long long
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define call(a) (a).cbegin(), (a).cend()
#define pb push_back
#define pf push_front
#define eb emplace_back
#define ef emplace_front
#define mp make_pair
#define mt make_tuple
#define fs first
#define se second
#define loop(i,a,b) for(int i=(a);i<ull(b);i++)
#define rloop(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define iter(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define riter(i,c) for(auto i=(c).rbegin(); i!=(c).rend(); ++i)
#define set(a,c) memset(a,c,sizeof a)
#define clr(a) memset(a,0,sizeof a)
#define debug(x) cout<<#x<<": "<<x<<endl
#define in scanf
#define out printf
#define f(i,j,k) fill(i[0],i[0]+j*j,k)
#define p(p) cout<<(p)<<endl;
#define exit(s,e) ((s).find(e)!=(s).end())
#define gcd(a,b) __gcd(a,b)
#define np(v) next_permutation(all(v))
#define pp(v) prev_permutation(all(v))
#define SORT(a) stable_sort(all(a))
#define RSORT(a) stable_sort(all(a),greater<int>())
#define MIN(a,b) (a>b?b:a)
#define MAX(a,b) (a>b?a:b)
#define INFILE() freopen("in0.txt","r",stdin)
#define OUTFILE() freopen("out0.txt","w",stdout)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> pii;
typedef pair<char, char> pcc;
typedef pair<char, int> pci;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-14;
const double pi  = 2*acos(0.0);
const double inf = INT_MAX/3;
int lcm(int a, int b) {return a*b/__gcd(a,b);}
bool is_leapyear(int y){return y%4==0&&(y%100!=0||y%400==0);}
int lis(int n,vi a) {
	int l[100000],length=1;l[0] = a[0];
	rep(i,n){
		if(l[length-1]<a[i])l[length++]=a[i];
		else *lower_bound(l,l+length, a[i])=a[i];
	}
	return length;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int dfs(int x, int y) {
    // (x, y) ????????????????????????????????????
    // ??¨???4??????????????????
    for(int r = 0; r < 4; r++) {
        int nx = x + dx[r];
        int ny = y + dy[r];
        dfs(nx, ny);
    }
}
//?´???°??????
int isPrime(int x){
    int i;
    if(x<2) return 0;
    else if (x == 2 ) return 1;
    if(x%2==0) return 0;
    for(i=3;i*i<=x;i+=2){
        if(x%i==0) return 0;
    }
    return 1;
}

//Union-find tree
class DisjointSet{
    public:
        vector<int> rank, p;
        DisjointSet(){}
        DisjointSet(int size){
            rank.resize(size,0);
            p.resize(size,0);
            for(int i=0; i<size; i++) makeSet(i);
        }
        void makeSet(int x){
            p[x] = x;
            rank[x] = 0;
        }
        bool same(int x, int y){
            return findSet(x) == findSet(y);
        }
        void unite(int x, int y){
            link(findSet(x), findSet(y));
        }
        void link(int x, int y){
            if(rank[x]>rank[y]){
                p[y] = x;
            } else {
                p[x] = y;
                if(rank[x]==rank[y]) {
                    rank[y]++;
                }
            }
        }
        int findSet(int x){
            if(x!=p[x]){
                p[x] = findSet(p[x]);
            }
            return p[x];
        }
};

//??????
class Point {
    public:
        double x,y;
        Point (double x=0, double y=0):x(x), y(y){}
        Point operator + (Point p) {return Point(x+ p.x,y+p.y);}
        Point operator - (Point p) {return Point(x- p.x,y-p.y);}
        Point operator * (double a) {return Point(a*x,a*y);}
        Point operator / (double a) {return Point(x/a, y/a);}
        double abs() {return sqrt(norm());}
        double norm() {return x*x+y*y;}

        bool operator < (const Point &p) const {
            return x != p.x ? x<p.x : y< p.y;
        }

        bool operator == (const Point &p) const{
            return fabs(x-p.x) < eps && fabs ( y-p.y) < eps; 
        }
};
typedef Point Vector;

// struct Point { double x, y; };
// typedef Point Vector;
// struct Segment { Point p1,p2;};
// typedef Segment Line;
// class Circle {
//     public:
//         Point c;
//         double r;
//         Circle(Point c = Point(), double r = 0.0): c(c), r(r){}
// };

// typedef vector<Point> Polygon;


double dot(Vector a, Vector b){
    return a.x* b.x + a.y * b.y;
}
double cross(Vector a, Vector b){
    return a.x* b.y - a.y * b.x;
}

signed main(){
    int n,q;
    while(cin >> n >> q&&n+q!=0){
        int m,ans=-1;
        vi a(n,0); m=q;
        while(ans==-1){
            rep(i,n){
                if(q!=0){
                    q--;
                    a[i]++;
                }
                else if(q==0){
                    q=a[i];
                    a[i]=0;
                }
                if(a[i]==m) ans=i;
            }
        }
        cout << ans << endl;
        }
}

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


int main(){
	int N,M,ans=0; cin >> N >> M;
	int d[N+10],v[N+10],m[101]={};
	for(int i=0; i<N; i++){
		 cin >> d[i] >> v[i];
		 if(m[d[i]]<=v[i]){
			 m[d[i]]=v[i];
		 }
	}
	for(int i=1; i<=M; i++){
			ans+=m[i];
	}
	cout << ans << endl;
}

C++: #include <stdio.h>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;
#define ll long long
#define pb push_back

void print(vector<int> a){
    for(int i=0;i<a.size();i++){
        cout << a[i] << " ";
    }
    cout << endl;
}

int main()
{
    int n, p,j=0;
    
    vector<int> x;
    while (1)
    {
        cin >> n >> p;
        if (n == 0)
        {
            break;
        }
        vector<int> a(n,0);
        int cup = p;
        int i=0;
        bool flag = true;
        while(flag){
            if(cup != 0){
                a[i]++;
                cup--;
                if(a[i]==p){
                    flag = false;
                    break;
                }
            } else {
                cup = a[i];
                a[i] = 0;
            }
            i++;
            if(i==n){
                i=0;
            }
           // print(a); // for debug
        }
        x.push_back(i);
        j++;
    }
    for (int i = 0; i < x.size(); i++)
    {
        cout << x[i] << endl;
    }
}