參考Lucky Cat。
想法:
這題比較麻煩的部分在於輸入,輸入完成後就用backtracking把每個Size的答案找出來,詳細看底下code。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <string> | |
#include <map> | |
#include <vector> | |
using namespace std; | |
int N; | |
char News[15][40]; | |
int ans[20]; | |
bool choosed[20] = {0}; | |
int Input1(int &, int &); | |
int Input2(); | |
void Combination(int &Size, int Len, int pos); | |
int main() | |
{ | |
int Case; | |
scanf("%d ", &Case); | |
while (Case--) { | |
int a = 0, b = 0; | |
int Mode = Input1(a, b); | |
N = Input2(); | |
if (Mode == 1) b = a; | |
else if (Mode == 3) a = 1, b = N; | |
for (int i = a; i <= b; ++i) { | |
printf("Size %d\n", i); | |
Combination(i, 0, 0); | |
putchar('\n'); | |
} | |
if (Case) putchar('\n'); | |
} | |
} | |
int Input1(int &a, int &b) | |
{ | |
char line[20]; | |
int Mode = 1; | |
gets(line); | |
if (line[0] == '*') Mode = 3; | |
else { | |
int i = 0; | |
while (line[i] != '\0' && line[i] != ' ') | |
a = a * 10 + (line[i++] - '0'); | |
if (line[i] == ' ') { | |
Mode = 2, ++i; | |
while (line[i]) | |
b = b * 10 + (line[i++] - '0'); | |
} | |
} | |
return Mode; | |
} | |
int Input2() | |
{ | |
int n = 0; | |
while (gets(News[n]) && News[n][0] != '\0') ++n; | |
return n; | |
} | |
void Combination(int &Size, int Len, int pos) | |
{ | |
if (Len == Size) { | |
printf("%s", News[ans[0]]); | |
for (int i = 1; i < Size; ++i) | |
printf(", %s", News[ans[i]]); | |
putchar('\n'); | |
return; | |
} | |
for (int i = pos; i < N; ++i) { | |
ans[Len] = i; | |
Combination(Size, Len + 1, i + 1); | |
} | |
} |
沒有留言:
張貼留言