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<vector>
#include<cstdio>
#include<string>
#include<numeric>
#include<algorithm>
#include<complex>
#include<queue>
#include<stdlib.h>
#include<map>
#include<cmath>
#include<stdio.h>
#include<stdlib.h>
#include<set>

#define ll long long int
#define MOD 1000000007
#define pb push_back
#define ub upper_bound
#define lb lower_bound

using namespace std;

int main(){
    int n,p;
    int j=0;
    int p_cnt[51];
    int table=0;
    while(1){
        for(int i=0;i<51;i++){
            p_cnt[i]=0;
        }
        cin >> n >> p;
        table=p;
        if(n==0||p==0){
            break;
        }
        j=0;
        while(1){
            if(table==0&&p_cnt[j]==p){
                cout << j << endl;
                break;
            }
            if(table!=0){
                table--;
                p_cnt[j]++;
            }
            else{
                table=p_cnt[j];
                p_cnt[j]=0;
            }
            if(j<n-1){
                j++;
            }
            else if(j==n-1){
                j=0;
            }
        }
    }
    return 0;
}

C++: 
// Next Mayor
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1159&lang=jp

#include<iostream>
#include<vector>

using namespace std;

const int NONE = -1;

int N, P;
vector<int> player;
int bowl;
int winner;

bool is_win(int i) {
    for (int nth = 0; nth < N; ++nth) {
        if (nth == i) continue;
        if (player[nth] != 0) return false;
    }
    return true;
}

void pick_up(int i) {
    if (bowl == 0) {
        bowl += player[i];
        player[i] = 0;
        return;
    }
    else if (bowl == 1) {
        bowl--;
        player[i]++;
        if (is_win(i)) winner = i;
        return;
    }
    bowl--;
    player[i]++;
    return;
}

int main() {
    while(cin >> N >> P && N) {
        player.assign(N, 0); 
        bowl = P;
        winner = NONE;
        int i = 0;
        while (true) {
            if (winner != NONE) break;
            pick_up(i);
            i = (i + 1) % N;
        }
        cout << winner << endl;
    }
    return 0;
}

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

int main(void){
    while(true){
        int n, kouho[55], stone;
        for(int i=0;i<55;++i) kouho[i] = 0;
        cin >> n >> stone;
        if(n==0 && stone==0) break;
        int i=0;
        while(true){
            i %= n;
            if(stone!=0){
                kouho[i] += 1;
                --stone;
                ++i;
                int cnt=0, ans=0;
                if(stone==0){
                    for(int j=0;j<n;++j){
                        if(kouho[j]==0) ++cnt;
                        else ans = j;
                    }
                    if(cnt==(n-1)){
                        cout << ans << endl;
                        break;
                    }
                }
            }else{
                stone += kouho[i];
                kouho[i] = 0;
                ++i;
            }
        }
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)

using namespace std;

int main(){

	int n,p;
	while(1){
		cin>>n>>p;
		if(n==0&&p==0) break;
		bool t = false;
		int wan=p,a[50]={},i=0;
		while(1){
			if(wan>0){
				wan--;
				a[i]++;
				bool flag = false;
				if(wan==0){	
					for(int j=0; j<n; j++){
						if(j==i) continue;
						if(a[j]!=0 || t==false) flag = true;
					}
					if(!flag){
						cout<<i<<endl;
						break;
					}
				}

			}else{
				if(a[i]==0){

				}else{
					wan += a[i];
					a[i] = 0;
				}
			}

			t = true;
			
			i++;
			if(i==n) i=0;
		}
	}

	return 0;
}

C++: #include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <stack>
#include <set>
#include <vector>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef pair<int,int> pii;

const int INF = 1 << 29;
const double EPS = 1e-10;


int main() {
  for (int n, p; cin >> n >> p, (n | p ) != 0; ) {
    int stone[51];
    rep(i, 51) stone[i] = 0;
    int cup = p;
    int cur = 0;
    for(; ; ) {
      if (cup != 0) {
        stone[cur]++;
        cup--;
      }
      else {
        cup = stone[cur];
        stone[cur] = 0;
      }

      if (stone[cur] == p) break;

      cur = (cur + 1 ) % n;
    }
    
    cout << cur << endl;


  }
  
  return 0;
}

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

using std::cout;
using std::cin;
using std::endl;
using std::vector;

vector<int> initial(int n, int p) {
    vector<int> status(n + 1);
    status[n] = p;
    return status;
}

void operation(vector<int> &status, int player, int n) {
    if (status[n] == 0) {
        status[n] += status[player];
        status[player] = 0;
    } else {
        status[n] --;
        status[player] ++;
    }
}

int nextPlayer(int player, int n) {
    if (player == n - 1) {
        return 0;
    }
    return player + 1;
}

int check(vector<int> status, int n, int p) {
    for (int i = 0; i < n; i ++) {
        if (status[i] == p) {
            return 1;
        }
    }
    return 0;
}

void game(vector<int> status, int n, int p) {
    int player = -1;
    while (check(status, n, p) != 1) { // ??°????????????????????????
        player = nextPlayer(player, n);// player?????´??°
        operation(status, player, n);
    }
    cout << player << endl;
}

int main(void) {
    int n; // ????£?????????°
    int p; // ?????????????????\???????°?????????°
    cin >> n >> p;
    while (n != 0 && p != 0) {
        vector<int> status = initial(n, p);
        game(status, n, p);
        cin >> n >> p;
    }

    return 0; 
}

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


using namespace std;

int main(){
	int n,r;
	while(cin >> n >> r,n){
		int p[n];
		fill(p,p+n,0);
		int s = n;
		for(int i = 0;;i++,i %= n){
			if(p[i] == r){
				cout << i << endl;
				break;
			} else {
				if(s == 0){
					s += p[i];
					p[i] = 0;
				} else {
					p[i]++;
					s--;
				}
			}
		}
	}
}

C++: import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);

		while(true){
			int n=sc.nextInt();
			int p=sc.nextInt();
			if(n==0) break;

			int i=0;
			int[] a=new int[n];
			while(true){
				if(p>0){ p--; a[i]++; }
				else{ p+=a[i]; a[i]=0; }

				if(p==0){
					boolean end=true;
					for(int j=0;j<n;j++) if(i!=j && a[j]!=0) end=false;
					if(end) break;
				}

				i=(i+1)%n;
			}
			System.out.println(i);
		}

		return;
	}
}