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 <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main (void) {

    vector<unsigned int> ns;
    vector<unsigned int> ps;

    unsigned int tn, tp;
    unsigned int dataset_size = 0;
    unsigned int count = 0;
    do {
        cin >> tn >> tp;
        ns.push_back(tn);
        ps.push_back(tp);
        dataset_size++;
    } while (tn != 0 || tp !=0);

    for (int x=0; x<dataset_size-1; x++) {
        count = 0;
        tn = ns[x];
        tp = ps[x];

        int has_p[tn];
        for (unsigned int i=0; i<tn; i++)
            has_p[i] = 0;

        unsigned int i = 0;
        do {
            if (tp == 0) {
                tp = has_p[i];
                has_p[i] = 0;
            } else {
                tp--;
                if ((++has_p[i]) == ps[x]) { break; }
            }
            i = (++i % tn);
            count++;
        } while (true);
        cout << i << endl;
    }

    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>
#include <iomanip>

using namespace std;
typedef long long ll;
typedef long long int llint;
#define MM 1000000000
#define MOD MM+7
#define pi pair<int,int>
#define pl pair<ll,ll>
#define chmax(a,b) (a<b ? a=b:0)
#define chmin(a,b) (a>b ? a=b:0)

const long double PI = acos(-1);
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};
int GCD(int x,int y){
    return y ? GCD(y,x%y) : x;
}
int n,p;
int a[50];

int main(){
    while(true){
        cin >> n >> p;
        if(n == 0 && p == 0) break;
        fill(a,a+n,0);
        int wan = p;
        int num = 0;
        while(true){
            if(wan > 0){
                a[num]++;
                wan--;
                if(a[num] == p){
                    //cout << "ans=";
                    cout << num << endl;
                    break;
                }
            } else if (wan == 0){
                wan += a[num];
                a[num] = 0;
            }
            num++;
            if(num == n) num = 0;
        }
    }
}

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

int main(){
    int n,p;
    while( cin>>n>>p ){
        int a[n]={};
        int max=p;
        int now;
        while(true){
            for(now = 0;now<n;now++){
                if(p>0){   
                    p--;
                    a[now]++;
                }else if(p==0){
                    p=a[now];
                    a[now]=0;
                }
                if(a[now]==max){
                    cout<<now<<endl;
                    break;
                }
            }
            if(a[now]==max){
                break;
            }
        }
        
    }
    return 0;
}

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-6;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

const int64 mod = 1e9+7;

int main(void){
	int32 n, r;
	while(cin >> n >> r && n && r){
		vector<int32> x(n, 0);
		int32 cup = r, cnt = 0;
		int32 now = 0;
		int32 res = 0;
		while(1){
			res++;
			if(cup){
				if(x[now] == 0) cnt++;
				x[now]++;
				cup--;
				if(cup == 0 && cnt == 1) break;
			}else{
				if(x[now]) cnt--;
				cup += x[now];
				x[now] = 0;
			}
			now = (now+1)%n;
		}
		cout << now << endl;
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

#define loop(n) for(int lp=0; lp<(n); lp++)
#define ascent(i, a, b)  for(int (i)=(a); (i)<(b); (i)++)
#define descent(i, a, b) for(int (i)=(a); (i)<(b); (i)--)

int main() {
  int n, _p;
  while(cin >> n >> _p, n) {
    int m[60];
    memset(m, 0, sizeof(m));

    int i = 0;
    int p = _p;
    while(true) {
      //cout << " " << p << endl;
      if(p > 0) {
	m[i%n] ++;
	p --;

	if(m[i%n] == _p) break;
      }
      else if(p == 0) {
	p = m[i%n];
	m[i%n] = 0;
      }
      i ++;
    }
    
    cout << i%n << endl;
  }
  return 0;
}

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

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int st[60];

int main() {
	int n,p,pp;
	while(n=in(),pp=in()){
		p=pp;
		memset(st,0,sizeof(st));
		int i=0;
		while(true){
			if(p){
				st[i]++;
				p--;
				if(st[i]==pp)break;
			}else{
				p+=st[i];
				st[i]=0;
			}
			i++;
			i%=n;
		}
		cout<<i<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int n,p;
  while(cin >> n >> p, n!=0 && p!=0){
    int s[n];
    int index = 0;
    int stone = p;
    for(int i=0;i<n;i++){
      s[i] = 0;
    }
    while(true){
      if(stone > 0){
    s[index]++;
    stone--;
      }else if(stone == 0){
    stone = s[index];
    s[index] = 0;
      }
     
      if(s[index] == p) break;
      if(index == n-1){
    index = 0;
      }else{
    index++;
      }
    }
    cout << index << endl;
  }
}

C++: #include <iostream>
using namespace std;
int main(){
	int n, p;
	int bowl;
	int x = 0;
	while (1){
		cin >> n >> p;
		if (n == 0 && p == 0) break;
		int C[50] = {0};
		bowl = p;
		x = 0;
		while (1){
			--bowl;
			++C[x];
			if (bowl == 0){
				if (C[x] == p){
					break;
				}
				else{
					bowl += C[x];
					C[x] = 0;
				}
			}
			if (x != n-1){
				x++;
			}
			else{
				x = 0;
			}
		}
		cout << x << endl;
	}
	return 0;
}