Skip to content

Instantly share code, notes, and snippets.

@hirosof
Created August 17, 2014 17:05
Show Gist options
  • Save hirosof/471950287230e25c799c to your computer and use it in GitHub Desktop.
Save hirosof/471950287230e25c799c to your computer and use it in GitHub Desktop.
yukicoder No.15
/*
yukicoder No.15
作成者:ヒロソフ
*/
#include <stdio.h>
unsigned int NumberOfCatalogItems; //カタログの商品数
unsigned int NumberOfBoughtPrice; //購入した価格の数
unsigned int *pPriceForItems; //商品の価格
void Process(unsigned int Current,unsigned int root , unsigned int Price);
void PrintCurrentRoot(unsigned int root , unsigned int Current);
int main(void) {
//入力
scanf("%u %u", &NumberOfCatalogItems, &NumberOfBoughtPrice);
//各商品の価格の入力
pPriceForItems = new unsigned int[NumberOfBoughtPrice];
for (unsigned int i = 0; i < NumberOfCatalogItems; i++) {
scanf("%u", pPriceForItems + i);
}
//処理実行
for (unsigned int i = 0; i < (NumberOfCatalogItems - 1); i++) {
Process(i, 0, 0);
}
//最後のデータのみここで判定
if (*(pPriceForItems + NumberOfCatalogItems - 1) == NumberOfBoughtPrice)printf("%d\n", NumberOfCatalogItems);
//メモリ解放
delete[] pPriceForItems;
return 0;
}
void Process(unsigned int Current, unsigned int root, unsigned int Price){
Price += *(pPriceForItems + Current);
root |= 1 << Current;
if (Price == NumberOfBoughtPrice) {
PrintCurrentRoot(root , Current);
} else if (Price <= NumberOfBoughtPrice) {
for (unsigned int i = Current + 1; i < NumberOfCatalogItems; i++) {
Process(i, root, Price);
}
} else {
return;
}
}
void PrintCurrentRoot(unsigned int root, unsigned int Current){
for (unsigned int i = 0; i <= Current; i++) {
if (root & (1 << i))printf("%d ", i + 1);
}
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment