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 <utility>
using namespace std;

int main(void){
  while(1){
    int stone[50];
    for(int k = 0 ; k < 50 ; k++){stone[k] = 0;}

    int bowl;
    int n, p;  // n???????£?????????°???p????°????????????°
    cin >> n;
    cin >> p;

    if(n == 0 && p == 0){return 0;}

    bowl = p;

    bool flg = true;
    int i;
    int ans;

    for(i = 0 ; flg ; i = (i+1) % n){

      if(bowl == 0){
        bowl = bowl + stone[i];
        stone[i] = 0;
      }else{
        stone[i] ++;
        bowl--;
      }

      if(bowl == 0){
        for(int j = 0 ; j < n ; j++){
          if(stone[j] == p){
            flg = false;
            ans = j;
          }
        }
      }

    }

    cout << ans << endl;
  }
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    int r;
    while(cin>>n>>r,n)
    {
        int isi;
        int num[50]={0};
        int t=0;
        isi=r;
        while(true)
        {
            if(isi==0)
            {
                isi=num[t];
                num[t]=0;
                t++;
            }
            else if(isi>0)
            {
                num[t]++;
                isi--;
                t++;
            }
            if(t==n)t=0;
            if(isi==0)
            {
                int cnt=0;
                for(int i=0;i<n;i++)
                {
                    if(num[i]==0)cnt++;
                }
                if(cnt==n-1)break;
            }
        }
        for(int i=0;i<n;i++)
        {
            if(num[i]!=0)
            {
                cout<<i<<endl;
            }
        }
    }
    return 0;
}

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

typedef struct{
  int no;
  int KOMA;
}P;

int main(){

  int n,p;
  P mem[50];
  while(1){
    for(int i=0;i<50;i++)mem[i].no=i,mem[i].KOMA=0;
    int ban=0;

    cin >> n >> p;
    int WAN = p;

    if(n==0 && WAN==0) break;

    while(1){
      if(ban==n)ban=0;
      if(WAN>0){
	mem[ban].KOMA++,WAN--;
      }else{
	WAN=mem[ban].KOMA;
	mem[ban].KOMA=0;
      }
      if(p==WAN){  //勝判定
	cout << ban << endl;   //
	break;                 //
      }
      ban++;
    }
  }
}

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

int main( void )
{
    int n, p;
    while ( cin >> n >> p && n && p ) {
        int C = p;
        int P[51];
        memset( P, 0, sizeof(P) );
        int ind = 0;
        int answer;
        for ( int s = 0; s < 1000000; s++ ) {
            if ( C > 0 ) {
                P[ind]++;
                C--;
                if ( P[ind] == p ) {
                    answer = ind;
                    break;
                }
            } else {
                C += P[ind];
                P[ind] = 0;
            }
            ind = ( ind + 1 ) % n;
        }
        cout << answer << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <numeric>
#include <vector>

using namespace std;

int main()
{
    int t = 0;
    while (true) {
        // if (t == 4) {
        //    break;
        //}
        int n;
        int p;
        scanf("%d %d", &n, &p);

        if (n == 0 && p == 0) {
            break;
        }

        vector<int> v;
        v.resize(n);
        for (auto&& i : v) {
            i = 0;
        }
        int c = p;
        int nn = 0;
        while (true) {
            // printf("c: %d nn: %d\n", c, nn);
            // printf("v %d: ", v.size());
            // for (auto&& i : v) {
            //    printf("%d ", i);
            //}
            // putchar('\n');
            if (c != 0) {
                c -= 1;
                v[nn] += 1;
                if (c == 0) {
                    auto s = accumulate(begin(v), end(v), 0) - v[nn];
                    if (s == 0) {
                        printf("%d\n", nn);
                        break;
                    }
                }
            }
            else {
                if (v[nn] != 0) {
                    c += v[nn];
                    v[nn] = 0;
                }
            }
            nn = (nn + 1) % n;
        }
        ++t;
    }
}

C++: #include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
  
int main()
{
	int n,m;
	int p[50]={0};
	int i,j;
	int ans=-1;

	while(1){
	scanf("%d %d",&n,&m);
	if((n==0)&&(m==0))
		break;
	//ここまでテンプレ
	j=m;
	while(1){
		for(i=0;i<n;i++){
			if(j>0){
				p[i]++;
				j--;
				if(p[i]==m){
					ans=i;
					break;
				}
			}else if(j==0){
				j+=p[i];
				p[i]=0;
			}

		}
		if(ans>-1)
			break;
	}

	printf("%d\n",ans);


	//ここからテンプレ
	}
	return 0;
}

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

#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;
typedef complex<double> P;

int main(){
  int n,p;
  while(cin>>n>>p, n+p){
    vector<int> v(n);
    int st = p;
    int ans;
    while(true){
      REP(i,n){
	if(st == 0){
	  st += v[i];
	  v[i] = 0;
	}else{
	  st--;
	  v[i]++;
	  if(v[i] == p){
	    ans = i;
	    goto end;
	  }
	}
      }
    }
  end:
    cout << ans << endl;
  }
  return 0;
}

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


int main(){
	for(int n,p;cin>>n>>p,p;){
		int have[50]={0};
		int cup=p;
		int turn=0;
		for(int i=0;1;){
			if(cup==0)swap(cup,have[i]);
			else{
				cup--;
				have[i]++;
			}
			if(have[i]==p){
				cout<<i<<endl;
				break;
			}
			i=(i+1)%n;
		}
		
		
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

bool isallzero(int n[], int m, int num) {
	for (int i = 0; i < num; i++) {
		if (i == m) {
			continue;
		} else if (n[i] != 0) {
			return false;
		}
	}

	return true;
}

int main() {
	while (true) {
		int n, p;
		cin >> n >> p;
		if (n == 0) {
			break;
		}
		
		int bowl = p;
		int candidates[n];
		int current = 0;
		for (int i = 0; i < n; i++) {
			candidates[i] = 0;
		}

		while (true) {
			if (bowl > 0) {
				bowl--;
				candidates[current]++;
				if (bowl == 0 && isallzero(candidates, current, n)) {
					cout << current << endl;
					break;
				}
			} else {
				bowl = candidates[current];
				candidates[current] = 0;
			}
			current = (current + 1) % n;
		}
	}
	
	return 0;
}

C++: #include <iostream>
#include <map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  int n, p, all;
  vector<int> r;
  while (cin >> n >> p && n && p) {
    bool flag = false;
    all = p;
    map<int, int> m;
    rep(i, n) m[i] = 0;
    while (!flag) {
      // cout << n << ' ' << p << endl;
      rep(i, m.size()) {
        if (p > 0) {
          p--;
          m[i]++;
        } else {
          p += m[i];
          m[i] = 0;
        }
      }
      rep(i, m.size()) {
        if (m[i] == all) {
          r.push_back(i);
          flag = true;
        }
      }
    }
  }
  for (auto i : r) {
    cout << i << endl;
  }
  return 0;
}