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 n, p, index, owan, answer;
	int person[1000];

	while(1){
		//ÏÌú»
		index = 0;
		for(int i=0 ; i<1000 ; i++){
			person[i] = 0;
		}
   
		//lüÍ  n:óâÒÌl  p:¬ÎÌ
		cin >> n >> p;
		if(n==0 && p==0) break;
		owan = p;//ßÍ¨oÉ¬Îª·×ÄüÁÄ¢é

		while(1){
			if(owan>0){//¨oÉÎª êÎÎð1ÂÆé
				owan--;
				person[index]++;
			}
			else{//ÎðSüêé
				owan += person[index];
				person[index] = 0;
			}

			// élª·×ÄÌ¬ÎðàÁÄ¢½ç»Ìlª¬·ÆÈé
			if(owan==0 && person[index]==p){
				answer = index;
				break;
			}
			index++;//ÐÆÂÆÈèÌÐÆÖ
			if(index>=n)index = 0;
		}

		//¦ÌoÍ
		cout << answer << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>

using namespace std;
int main(){
	int n, p;
	while(1){
		cin >> n >> p;
		if(n == 0 && p == 0) break;
		int i, count = 0;
		int a[n], P;
		for(i = 0;i < n;i++){
			a[i] = 0;
		}
		P = p;
		while(1){
			if(count == n){
				count = 0;
			}
			//cout << "count = " << count << endl;
			if(P > 0){
				a[count]++;
				P--;
				if(a[count] == p){
					break;
				}
				count++;
			}
			else if(P == 0 && a[count-1] != p){
				P = a[count];
				a[count] = 0;
				count++;
			}

			//printf("a[%d] = %d, count = %d, P = %d\n",count-1,a[count-1],count,P);
		}
		/*if(count == 0){
			count = n-1;
		}
		else if(count != 0){
			count = count - 1;
		}*/
		cout << count << endl;
	}
	return 0;
}