Saturday, September 21, 2013

2011年04月 (2) 2010年05月 (4) 2010年03月 (2) 2010年01月 (23) 2009年12月 (17) 2009年11月 (5) 2009年06月 (1) 2009年05


//附上别人的队列解法 #include <iostream> #include <iomanip> using namespace std; int main() { int N,k,m; while (cin >> N >> k >> m) { bool flag[20] = {false}; int count = 0; if (!N && !k && !m) break; int i = 0,j = N-1; for ( ; count <= N; ) { int sum = 0; for (; ; i = (i + 1) % N) { if (!flag[i]) { sum++; if (sum == k) break; } } sum = 0; for (; ; j = (j-1 + N) % N) { if (!flag[j]) { sum++; if (sum == m) break; } } flag[i] = true; flag[j] = true; if (i != j) { count += 2; cout << setw(3) << i + 1 << watch part setw(3) << j + 1; } else { count += 1; cout << setw(3) << i+1; } if (count != N) cout << ","; else break; i = (i + 1) % N; while (flag[i]) i = (i + 1) % N; j = (j+N-1) % N; while (flag[j]) watch part j = (j + N -1) % N; } cout << endl; } return 0; } /* ACM Q133: The Dole Queue */ /* 2008/4/8 AC by cjw */ /* lang: C */ /* 用�� queue �模� */ #include <stdio.h> struct Queue { int no; /* No. */ struct Queue *next; struct Queue *last; }; typedef struct Queue Queue; watch part Queue *head = NULL; /* head */ Queue *tail = NULL; void buildQue(int watch part N) { Queue *new, *tmp; int i; head = malloc(sizeof(Queue)); /* the first one */ head->no = 1; head->next = NULL; head->last = NULL; tmp = head; for ( i = 2 ; i < N ; i++ ) { new = malloc(sizeof(Queue)); new->no = i; new->last = tmp; tmp->next = new; new->next = NULL; tmp = new; } if ( N > 1 ) { new = malloc(sizeof(Queue)); /* the last one */ new->no watch part = N; new->next = head; new->last = tmp; tmp->next = new; tail = new; head->last = tail; } else { tail = head; } } void delete(Queue *del) { /* delete node */ del->last->next = del->next; del->next->last = del->last; free(del); } void pickPeople(int k, int m, int N) { /* 官��人 */ Queue *pick1, *pick2, *tmp1, *tmp2; int count = N; int i; pick1 = head; pick2 = tail; while ( count > 0 ) { for ( i = 1 ; i < k ; i++ ) pick1 = pick1->next; /* �人 */ for ( i = 1 ; i < m ; i++ ) pick2 = pick2->last; /* output */ if ( pick1 != pick2 ) { printf("%3d%3d", pick1->no, pick2->no); if ( count > 2 ) printf(","); } else { printf("%3d", pick1->no); if ( count > 1 ) printf(","); watch part } if ( pick1 != pick2 ) { /* 挑到��不同的人 */ tmp1 = pick1; tmp2 = pick2; /* tmp 是要被 delete 的 */ pick1 = pick1->next; pick2 = pick2->last; /* 下一� */ /* 如果挑到的�好是另一�的下一�, 另一�要再往前�一� */ if ( tmp1 == pick2 ) pick2 = pick2->last; if ( tmp2 == pick1 ) pick1 = pick1->next; delete(tmp1); delete(tmp2); /* delete it */ count -= 2; } else { /* 挑到同一�人 */ tmp1 = pick1; pick1 = pick1->next; pick2 = pick2->last; delete(tmp1); count--; } } printf("/n"); } int main() { int N, k, m; while ( 1 ) { scanf("%d %d %d", &N, &k, watch part &m); /* input */ if ( N == 0 && k == 0 && m == 0 ) break; if ( N == 1 ) printf("%3d/n", 1); /* 如果只有一�人, 直接�出 */ else { buildQue(N); pickPeople(k, m, N); } } return 0; }
2011年04月 (2) 2010年05月 (4) 2010年03月 (2) 2010年01月 (23) 2009年12月 (17) 2009年11月 (5) 2009年06月 (1) 2009年05月 (7) 2009年04月 (5) 2009年03月 (18) 2009年02月 (2) 2009年01月 (13) 2008年12月 (1) 2008年11月 (1) 2008年10月 (2) 2008年09月 (4)
匿名用户 : #ifdef#else

No comments:

Post a Comment