Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

Student Grants

144.c

#include <stdio.h>

int N,k;
int students[26];

void
calc(void)
{
	int i;
	int out=0, order_stu=1, order_out=0;

	for(i=1; i<=N; i++) {
		students[i]=0;
	}
	while(1) {
		int done=1;
		for(i=1; i<=N; i++) {
			if (students[i]!=40) {
				done=0;
				break;
			}
		}
		if (done) {
			break;
		}
		if (out==0) {
			order_out++;
			if (order_out>k) {
				order_out=1;
			}
			out=order_out;
		}
		while (students[order_stu] == 40) {
			order_stu++;
			if (order_stu > N) {
				order_stu=1;
			}
		}
		if (students[order_stu]+out > 40) {
			out -= 40-students[order_stu];
			students[order_stu]=40;
		} else {
			students[order_stu] += out;
			out = 0;
		}
		if (students[order_stu] == 40) {
			printf("%3d", order_stu);
		}
		order_stu++;
		if (order_stu > N) {
			order_stu=1;
		}
	}
	printf("\n");
}

int
main(void)
{
	while(1) {
		if (scanf(" %d %d", &N, &k)!=2) {
			exit(0);
		}
		if (N==0) {
			exit(0);
		}
		calc();
	}

	exit(0);
}