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

int main(int argc, char const* argv[])
{
    int p_array[50];
    int p,n;
    int wan;
    while (1) 
    {
        int max_index=0;
        int max_p= 0;
        int k=0,l=0;
        cin >> n >> p;
        wan = p;
        if (n == 0 && p==0) 
        {
            break;
        }

        //init p_array
        for (int j = 0; j < n; j++) 
        {
            p_array[j] = 0;
        }
        while (max_p != p) 
        {
            if (wan != 0) 
            {
                wan--;
                p_array[k]++;
            }
            else if(wan == 0)
            {
                wan = p_array[k];
                p_array[k] = 0;
            }
            //search max_p
            for (l = 0; l < n; l++) 
            {
                if (max_p < p_array[l]) 
                {
                     max_p = p_array[l];
                     max_index = l;
                }
            }
            //for (int m = 0; m < n; m++) 
            //{
            //    cout << p_array[m] << " ";
            //}
            //cout << "max_index= " << max_index;
            //cout << endl;
            k++;
            k = k % n;
        }
        cout << max_index << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<stdio.h>
#include<limits.h>
#include<math.h>
#include<string>
#include<string.h>
#include<bitset>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<complex.h>
#include<map>

using namespace std;
const int INF = 1e9;


int main(){
  int n,p;
  while(1){
    cin>>n>>p;
    if(n==0){
      break;
    }
    int owan=p;
    vector<int> a(n,0);
    int i=0;
    while(1){
      //cerr<<"i="<<i<<endl;
      bool end_flg=false;
      //for(int x: a){
        //cerr<<x<<" ";
      //}
      //cerr<<endl;
      //cerr<<"owan="<<owan<<endl;
      if(owan==0){
        if(a[i]!=p){
          //tuduku
          owan+=a[i];
          a[i]=0;
        }else{
          //winner
          cout<<i<<endl;
          end_flg=true;
          break;
        }
      }else{
        a[i]++;
        owan--;
      }
      i++;
      if(i==n){
        i=0;
      }
      if(end_flg)break;
    }
  }
  return 0;
}

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

int main() {
	while(true){
		int n,p,b=0;
		cin>>n>>p;
		if(n==0&&p==0) return 0;
		vector<int>h(n);
		b=p;
		while(true){
			for(int i=0;i<n;i++){
				if(b==0){
					b=h[i];
					h[i]=0;
				}
				else{
					b--;
					h[i]++;
					if(h[i]==p){
						cout<<i<<endl;
						goto e;
					}
				}
			}
		}
		e:;
	}
	return 0;
}

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

int main()
{
	while (true) {
		int n, p;

		cin >> n >> p;

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

		int ans;
		int cup = p;
		vector<int> person(n, 0);
		int count = 0;

		while (true) {
			if (cup != 0) {
				person[count]++;
				cup--;

				if (cup == 0) {
					bool flag = true;
					for (int i = 0; i < n; i++) {
						if (i == count) {
							continue;
						}

						if (person[i] != 0) {
							flag = false;
							break;
						}
					}
					if (flag == true) {
						ans = count;
						break;
					}
				}
			}
			else {
				cup += person[count];
				person[count] = 0;
			}
			count = (count + 1) % n;
		}

		cout << ans << endl;
	}

	return 0;
}

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

int main(){
  int n, p;
  cin >> n >> p;
  while(n != 0 && p != 0){
    vector<int> v(n);
    for(int i = 0; i < n; i++){
      v[i] = 0;
    }

    int ans;
    int stone = p;
    bool ok = false;
    while(1){
      for(int i = 0; i < n; i++){
        if(stone > 0){
          v[i]++;
          stone--;
        }else{
          stone += v[i];
          v[i] = 0;
        }

        if(v[i] == p){
          ans = i;
          ok = true;
          break;
        }
      }
      if(ok)break;
    }
    cout << ans << endl;
    cin >> n >> p;
  }
}

C++: #include<iostream>

using namespace std;

int main(){
  int n,p;
  while(1){
    cin >> n >> p;
    if(n == 0 && p == 0)break;
    int pmax = p;
    int s[n];
    for(int i = 0; i < n; i++){
      s[i]=0;
    }
    int i = 0;
    while(1){
      if(p>0){
	s[i]++;
	p--;
      }else if(p==0){
	p = s[i];
	s[i]=0;
      }else if(s[i]==pmax){
	break;
      }

      if(i = n-1)i = 0;
      else i++;
    }
    cout << i << endl;
  }

  return 0;
}

C++: null

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

bool allZero(vector<int> s, int cur) {
	/*for (int i = 0; i < ns.size(); i++) {
		if (ns[i].size() > 0) return false;
	}*/
	for (int i = 0; i < s.size(); i++) {
		if (s[i] > 0 && i != cur) return false;
	}
	
	return true;
}
int main()
{
	int n, p, cur;
	//vector<int> s;
	//vector<vector<int>> ns;
	int curP;
	vector<int> s; 
	
	while (cin >> n >> p) {
		//cout << n << " " << p << endl;
		cur = 0;
		curP = p;
		s.clear();
		s.resize(n);
		/*ns.clear();
		ns.resize(0);
		for (int i = 0; i < n; i++) {
			ns[i].resize(0);
		}*/

		// 現在の候補者が碗から石を取り出す
		// 番号が降ってあるわけではない！
		//while (!(curP == 0 && isZero(s))) {
		while (true) {
			
			// 現在ターンである候補者が1つ取り出す
			//ns[cur].push_back(p[cur]);
			if (curP > 0) {
				s[cur]++;
				curP--;
			} else if (curP == 0 && s[cur] > 0) {// 手元に小石があれば全て碗の中に入れる
				/*for (int i = 0; i < ns[cur].size(); i++) {
					s.push_back(ns[cur][i]);
				}*/
				curP += s[cur];
				s[cur] = 0;
			}
			
			// 条件を満たして入れば終了
			if (curP == 0 && allZero(s, cur)) {
				cout << cur << endl;
				break;
			}

			// 次の候補者へ
			cur++;
			if (cur == n) cur = 0;
		}
	
		
	}

	return 0;
}

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

int main(){
	int n,p;
	while(cin>>n>>p,n,p){
		int is[60]={};
		int ap=p;
		int turn=0;
		int win;
		
		while(1){
			if(p!=0){
				is[turn%n]++;
				p--;
				}
			else{
				p=is[turn%n];
				is[turn%n]=0;
				}	
				
				int flg=0;
				for(int i=0;i<n;i++){
					if(is[i]==ap){
						flg=1;
						win=i;
						}
					}
					
					if(flg==1)break;
					turn++;
			}
			cout<<win<<endl;
		}
	return 0;
}

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

int main(){
	int people;
	int stone;
	int hand[50];

	while(1){
		for(int j=0; j < 50; j++) hand[j] = 0;

		int ans;
		cin >> people >> stone;
		if((people|stone) == 0) break;

		int box = stone;
		int i=0;
		while(1){
			if(box > 0){
				hand[i]++;
				box--;
			}
			else{
				box += hand[i];
				hand[i] = 0;
			}
			if(hand[i] == stone){
				ans = i;
				break;
			}
			i = (i + 1)%people;
		}
		cout << ans << endl;
	}
	return 0;
}