2013年4月30日

UVa 598 - Bundling Newspapers


#include <stdio.h>

int n;
char word[99][99];

void find(int now, int need, int already, int * used) {
  int i, j;
  if (already == need) {
    for (i = 0, j = 0; i < n; i++)
      if (used[i]) printf("%s%s", word[i], ++j == need ? "\n" : ", ");
    return;
  }
  for (i = now; i < n; i++) {
    used[i] = 1;
    find(i + 1, need, already + 1, used);
    used[i] = 0;
  }
}

int main() {
  char s[99];
  gets(s);
  int T;
  sscanf(s, "%d", &T);
  gets(s);
  while (T--) {
    gets(s);
    n = 0;
    while (gets(word[n]) && word[n][0])
      n++;
    int l, r;
    if (s[0] == '*') {
      l = 1, r = n;
    } else if (!s[1] || !s[2]) {
      sscanf(s, "%d", &l);
      r = l;
    } else {
      sscanf(s, "%d%d", &l, &r);
    }
    while (l <= r) {
      int used[99] = {};
      printf("Size %d\n", l);
      find(0, l++, 0, used);
      puts("");
    }
    if (T) puts("");
  }
  return 0;
}

沒有留言:

張貼留言