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 <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define DEC(i, s) for (int i = (s); i >= 0; i--)

#define SIZE(v) (int)((v).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define FIND(m, w) ((m).find(w) != (m).end())

int n, p;
int stone[100];

int main() {
  while (scanf("%d %d", &n, &p), n|p) {
    MEMSET(stone, 0);
    int turn = 0;
    while (true) {
      if (p > 0) {
        stone[turn]++;
        p--;
        if (p == 0) {
          int have = 0;
          REP(i, n) {
            if (stone[i] > 0) { have++; }
          }
          if (have == 1) {
            printf("%d\n", turn);
            break;
          }
        }
      } else {
        p += stone[turn];
        stone[turn] = 0;
      }
      turn = (turn + 1) % n;
    }
  }
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<memory>
#include<functional>

using namespace std;

#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define F(i,j,k) fill(i[0],i[0]+j*j,k)
#define P(p) cout<<(p)<<endl;
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define INF 1<<25
#define pb push_back

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

int main()
{
    int n, p;
    while (cin >> n >> p, n|p) {
        int cup = p;
        int i = 0;
        vi v(n, 0);
        while(1){
            if (cup == 0) {
                cup += v[i];
                v[i] = 0;
                i = (i+1) %n;
            } else {
                cup--;
                v[i]++;
                if (p == v[i])
                    break;
                i = (i+1) % n;
            }
        }
        cout << i << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a); i<(b); i++)
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define sort(v,n) sort(v,v+n);
#define vsort(v) sort(v.begin(),v.end());
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
typedef pair<int,int> p;
typedef pair<ll,ll> lp;
bool is_uruu(int y) {
        return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
}
const ll MOD=1e9+7;
const double PI=acos(-1.0);
//---------------------------------------------------------------------------//
int n,b;
int main(){
        while(1) {
                cin>>n>>b;
                if(n==0) return 0;
                int index=0;
                int s[n];
                for(int i=0; i<n; i++) {
                        s[n]=0;
                }
                int stone=b;
                while(1) {
                        if(stone!=0) {
                                stone--;
                                s[index]+=1;
                        }else if(stone==0) {
                                stone+=s[index];
                                s[index]=0;
                        }
                        if(s[index]==b) {
                                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, pebbles[50];
    int winner;
    cin >> n >> p;
    while(n || p){
        for(int i=0;i<n;++i){
            pebbles[i] = 0;
        }
        bowl = p;
        winner = 1000;
        while(true){
            for(int i=0;i<n;++i){
                if(bowl){
                    ++pebbles[i];
                    --bowl;
                }else{
                    bowl = pebbles[i];
                    pebbles[i] = 0;
                }
                if(pebbles[i] == p){
                    winner = i;
                    break;
                }
            }
            if(winner < 50) break;
        }
        cout << winner << endl;
        cin >> n >> p;
    }
}

C++: #include<iostream>

using namespace std;

int main(){
  int P,N,ndata[50],wan,lop;
  while(1){
    cin>>N;
    cin>>P;
    if(N==0&&P==0){
      break;
    }
    for(lop=0;lop<N;lop++){
      ndata[lop]=0;
    }
    wan=P;
    while(1){
      for(lop=0;lop<N;lop++){
        if(wan!=0){
          wan--;
          ndata[lop]++;
        }else{
          wan=wan+ndata[lop];
          ndata[lop]=0;
        }
        if(ndata[lop]==P&&wan==0){
          break;
        }
      }
      if(ndata[lop]==P&&wan==0){
        cout<<lop<<endl;
        break;
      }
    }

  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
int array[51];
int main(){
  int n,p;
  vector<int> v;
  while(cin >>n >>p){
     if(n==0&&p==0){
       break;
     }
     int i=0,gen=p;
     while(true){
       /*
    for(int s=0;s<n;s++){
	 cout << "人" <<s <<" " <<array[s]  <<endl;
       }
       cout <<"p " << p << endl;
       */

       if(p!=0){
       array[i]+=1;
       p--;
        if(array[i]==gen&&p==0){
	  cout   << i <<endl;
	  array[i]=0;
	  break;
	} 
       }else{
	   p+=array[i];
	   array[i]=0;
       }
   
       i++;
    
       if(i==n){
	 i=0;
       }
     }
  }
 
  return 0;
}

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

int solve (int n, int p);

int main(void) {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, p, mayor;
	while (cin >> n >> p, n | p) {
		mayor = solve(n, p);
		cout << mayor << endl;
	}

	return 0;
}

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

int solve (int n, int p) {
	int a[n], bowl = p, m_num;

	for (int j = 0; j < n; j++) a[j] = 0;

	while (1) {
		for (int i = 0; i < n; i++) {
			if (bowl == 0) {
				bowl = a[i]; 
				a[i] = 0;
			} else {
				bowl--; 
				a[i]++;
			}	
			if (bowl == 0 && (m_num = check(a, n, p)) != -1) goto SKIP;
		}
	}
	SKIP:
	return m_num;
}