/* Solution to ball toss by bob roos */ #include int n, k, count; char temp[2]; int t[32]; int used[32]; int total; int tosser, next; int add(int a, int b) { if (a+b < 0) return a+b+n; return (a+b)%n; } void toss() { int i,hold; /* pre: tosser is completely processed; next receives the ball */ while (total < n) { count++; if (!used[next]) { total++; used[next] = 1; } /* figure out new value of "next" */ if (tosser == add(next,1) && t[next] == -1) { t[next] = -t[next]; tosser = next; next = add(next,-1); } else if (tosser == add(next,-1) && t[next] == 1) { t[next] = -t[next]; tosser = next; next = add(next,1); } else { t[next] = -t[next]; hold = tosser; tosser = next; next = add(hold,-t[next]); } } printf("Classmate %d got the ball last after %d tosses.\n",tosser+1,count); } int main(void) { int i; while (scanf("%d %d",&n, &k) && n > 0) { for (i = 0; i < n; i++) { scanf("%s",&temp); used[i] = 0; total = 0; count = 0; t[i] = 1; if (temp[0] == 'L') t[i] = -1; } next = k-1; tosser = 0; toss(); } }