开发工具:
文件大小: 4kb
下载次数: 0
上传时间: 2009-06-17
详细说明: #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; } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.