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<bits//stdc++.h>
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p&&n!=0&&p!=0){
		int a[50];
		fill(a,a+n,0);
		int m=0,p2=p;
		while(true){
			if(p==0){
				p+=a[m];
				a[m]=0;
			}
			else{
				p--;
				a[m]++;
			}
			if(p==0&&a[m]==p2) break;
			m++;
			if(m==n) m=0;
		}
		cout<<m<<endl;
	}
}

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

int t[52];
main(){
	int n,p,c,i;
	while(cin>>n>>p,n){
		memset(t, 0, sizeof(t));
		for(c=0;;c=(c+1)%n){
			if(!p){
				p = t[c];
				t[c] = 0;
			}else{
				p--;
				t[c] ++;
				if(!p){
					for(i=0;i<n;i++) if(t[i] && i != c) break;
					if(i==n) break;
				}
			}
		}
		cout << c << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <string>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define endl '\n'
const int INF = (1 << 30);
using namespace std;
typedef long long int ll;

bool solve(){
    int n, p;
    cin >> n >> p;
    if(!n && !p) return false;

    vector<int> hand(n,0);
    int i=-1, wan=p;
    do{
        i = (i+1)%n;
        if(wan-- == 0) { wan = hand[i]; hand[i] = 0; }
        else{ hand[i]++; }
    }while(hand[i] != p);
    cout << i << endl;
    return true;
}

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

    while(solve());
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
// 18:45 -
using namespace std;

int main(void)
{
    while(1){
        int n, p;
        scanf("%d %d", &n, &p);
        if(n == 0 & p == 0)
            break;
        int mine[n] = {0};
        int all = 0;
        int count = 0;
        for(count = 0;; count++){
            all = 0;
            if(p > 0){
                p--;
                mine[count % n]++;
                for(int i = 0; i < n; i++) all += mine[i];
                all -= mine[count % n];
                if(all == 0 && p == 0){
                    break;
                }
            } else if(p == 0){
                p += mine[count % n];
                mine[count % n] = 0;
            }
        }
        printf("%d\n", count % n);
    }

    return 0;
}

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

using namespace std;
using i64 = long long int;
using ui64 = unsigned long long int;

////////////////////////////////

int main()
{
    int n,p;
    while(cin >> n >> p,n&&p){
        int cur = 0;
        int incup = p;
        vector<int> s(n,0);
        int ret = -1;
        while(true){
            if(incup > 0){
                incup--;s[cur]++;
                if(incup==0 && s[cur] == p){
                    ret = cur;break;
                }
            }else{
                incup+=s[cur];s[cur] = 0;
            }
            cur = (cur + 1) % n;
        }
        cout << ret << endl;
    }
}

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

using namespace std;

int main() {
	int n, p;
	while ( cin >> n >> p, n || p ) {
		vector<int> v(n, 0);
		int rest = p;
		int pos = 0;
		int ans = 0;
		int t = 0;
		while (++t < 1000001) {
			if (rest > 0) --rest, ++v[pos];
			else rest = v[pos], v[pos] = 0;

 			if (rest == 0 && v[pos] == p) {
				ans = pos;
				break;
			}
			pos = (pos + 1) % n;
		}
		cout << ans << endl;
	}
}

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

int main() {

    while(true){

        int n,p;

        cin >> n >> p;

        if(n == 0){
            break;
        }

        int maxp = p;

        list<pair<int,int> > l;

        for(int i=0; i<n; i++){
            l.push_back(make_pair(0,i));
        }

        list<pair<int,int> >::iterator itr = l.begin();

        while(true){

            if(p > 0){
                p--;
                itr->first++;
                if(itr->first == maxp){
                    cout << itr->second << endl;
                    break;
                }
            }
            else{
                p += itr->first;
                itr->first = 0;
            }

            itr++;

            if(itr == l.end()){
                itr = l.begin();
            }

        }

    }

    return 0;
}

C++: #include <iostream>
#include <cmath>
#include <algorithm>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i,k,n) for(int i = k; i < (int)(n); i++)
using namespace std;

int w,h;
int field[51][51];

void dfs(int y, int x){
        field[y][x]= 0;

        int dx[] = {-1,-1,-1,0,0,0,1,1,1};
        int dy[] = {-1,0,1,-1,0,1,-1,0,1,};
        rep(i,9){
                int nx = x+dx[i];
                int ny = y+dy[i];
                if(nx>=0 && nx<w && ny>=0 && ny<h && field[ny][nx] == 1) dfs(ny,nx);
        }
        return;
}

int main(){
        while(true) {
                cin >> w >> h;
                if(w==0) break;
                rep(y,h) rep(x,w){
                        cin >> field[y][x];
                }
                int ans = 0;
                rep(y,h) rep(x,w){
                        if(field[y][x] == 1) {
                                dfs(y,x);
                                ans++;
                        }
                }
                cout << ans << endl;
        }

        return 0;
}

C++: #include <stdio.h>

int main(void)
{
	int n = 1;
	int p = 1;
	int pt[64] = {0};
	int out[10000] = {0};

	int now = 0;
	int maxp = 0;

	int round = 0;
	int endflag = 1;

	int i;

	while(1)
	{

		scanf("%d",&n); scanf("%d",&p);

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

		maxp = p;

		while(1)
		{
	

			for( now = 0 ; now < n ; now++ )
			{
				if(pt[now] == maxp)
				{
				out[round] = now;
				endflag = 0;
				break;
				}
	
				else if(p)
				{	
				pt[now]++;
				p--;
				}

				else
				{
				p += pt[now];
				pt[now] = 0;
				}
			}


		now = 0;

		if(endflag)
			{continue;}

		else
			{
			endflag = 1;
			round++;
			for( i = 0 ; i < 64 ; i++)
			{pt[i] = 0;}
			break;
			}

		}
	}


	for( i = 0 ; i < round ; i++ )
	{
		printf("%d\n",out[i]);
	}

	return 0;

}

C++: #include<iostream>
using namespace std;
int n,p;

int main(){
	int i;
	while( cin >> n >> p ){
		if(!n)break;
		
		int table=p;
		int temoto[50] = {0};
		int cor=0;
		
		while(1){
			int cnt=0;
			if( table == 0 ){
				table = temoto[cor];
				temoto[cor] = 0;
			}
			else{
				table--;
				temoto[cor]++;
			}
			if( table == 0 ){
				for( i = 0; i < n; i++ ){
					if( temoto[i] ){
						cnt++;
					}
				}
				if( cnt <= 1 ){
					cout << cor << endl;
					break;
				}
			}
			cor++;
			if( cor == n )
				cor = 0;
		}
	}
	return 0;
}

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

int main(){
    int n,p;
    cin >> n >> p;
    int ans;
    int rem;
    bool flag;
    while (n!=0 || p!=0){
        vector<int> stone(n);
        for (int i = 0; i<n; i++) stone[i] = 0;
        flag = false;
        rem = p;
        while (1){
            for (int i = 0; i<n; i++){
                if (rem!=0){
                    stone[i]++;
                    rem--;
                }
                else{
                    rem += stone[i];
                    stone[i] = 0;
                }
                if (stone[i] == p){
                    ans = i;
                    flag = true;
                    break;
                }
                
            }
            if (flag) break;
        }
        cout << ans << endl;
        cin >> n >> p;
    }
}