【文件属性】:
文件名称:北大acm第1014题源代码
文件大小:4KB
文件格式:C
更新时间:2012-07-07 08:45:37
北大acm第1014题源代码
#include
#include
typedef int status;
#define OK 1
#define ERROR 0
#define OVER_FLOW -2
#define TRUE 1
#define FALSE 0
#define LIST_INIT_SIZE 2
#define LIST_INCREASEMENT 2
#define N 6
typedef struct
{
int value;
int quantity;
int functionCount;
} marbleType;
typedef struct
{
marbleType *marble;
int length;
int listSize;
} sqListMarble;
typedef struct
{
status *state;
int length;
int listSize;
} resultList;
FILE *fp;//
status initList_sq(sqListMarble *list);
status initResultList(resultList *list);
status sqListInsertM(sqListMarble *list,int quantity,int value);
status insertResultList(resultList *list,status state);
status calculate(int totalValue,sqListMarble *marbleList);
int getCurMarbleValue(sqListMarble *marbleList,int marbleNumber);
void printResult(resultList *list);
main()
{
resultList result;
int i,quantity,totalValue;
sqListMarble marbleList;
fp = fopen("data.txt","r");//
initResultList(&result);
while(1)
{
initList_sq(&marbleList);
totalValue = 0;
for(i = 0;i < N;i++)
{
fscanf(fp," %d",&quantity);//
//scanf(" %d",&quantity);
if(quantity)
{
sqListInsertM(&marbleList,quantity,i + 1);
totalValue += quantity * (i + 1);
}
}
if(!totalValue)
break;
insertResultList(&result,calculate(totalValue, &marbleList));
}
printResult(&result);
fclose(fp);
}
status initList_sq(sqListMarble *list)
{
list->marble = (marbleType *)calloc(LIST_INIT_SIZE,sizeof(marbleType));
if(!list->marble)
exit(OVER_FLOW);
list->length = 0;
list->listSize = LIST_INIT_SIZE;
return OK;
}
status sqListInsertM(sqListMarble *list,int quantity,int value)
{
marbleType *newBase;
if(list->length >= list->listSize)
{
newBase = (marbleType *)realloc(list->marble,(list->listSize + LIST_INCREASEMENT) * sizeof(marbleType));
if(!newBase)
exit(OVER_FLOW);
list->marble = newBase;
list->listSize += LIST_INCREASEMENT;
}
(list->marble + list->length)->quantity = quantity;
(list->marble + list->length)->value = value;
(list->marble + list->length)->functionCount = 0;
list->length++;
return OK;
}
status calculate(int totalValue,sqListMarble *marbleList)
{
int halfValue ,i,marbleNumber;
int functionAmount = 1,value = 0;
if(totalValue % 2)
return FALSE;
halfValue = totalValue / 2;
for(i = 0; i < marbleList->length;i++)
functionAmount *= (marbleList->marble + i)->quantity + 1;
marbleList->marble->functionCount = -1;
for(i = 0;i < functionAmount;i++)
{
value = 0;
for(marbleNumber = 0;marbleNumber < marbleList->length;marbleNumber++)
value += getCurMarbleValue(marbleList,marbleNumber);
//putchar('\n');//
if(value == halfValue)
return TRUE;
}
return FALSE;
}
int getCurMarbleValue(sqListMarble *marbleList,int marbleNumber)
{
marbleType *marble = marbleList->marble + marbleNumber;
if(marble == marbleList->marble)
marble->functionCount++;
if(marble->functionCount == marble->quantity + 1)
{
if(marbleList->marble + marbleList->length - 1 != marble )
(marble + 1)->functionCount++;
marble->functionCount = 0;
}
//printf(" %d ",curCount);//
return marble->functionCount * marble->value;
}
void printResult(resultList *list)
{
int i;
for(i = 0;i < list->length;i++)
{
printf("Collection #%d:\n",i + 1);
if(*(list->state + i) == 1)
{
printf("Can be divided.\n");
}
else
{
printf("Can't be divided.\n");
}
printf("\n");
}
return;
}
status initResultList(resultList *list)
{
list->length = 0;
list->listSize = LIST_INIT_SIZE;
list->state = (status *)calloc(LIST_INIT_SIZE,sizeof(status));
if(!list->state)
exit(OVER_FLOW);
return OK;
}
status insertResultList(resultList *list,status state)
{
if(list->length >= list->listSize)
{
list->state = (status *)realloc(list->state,(list->listSize + LIST_INCREASEMENT) * sizeof(status));
if(!list->state)
exit(OVER_FLOW);
list->listSize += LIST_INCREASEMENT;
}
*(list->state + list->length) = state;
list->length++;
return OK;
}