#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <time.h> #include <limits.h> #define X 100 #define Y 100 #define Z 100 //MAXSIZE = (X+1)*(Y+1)*(Z+1) #define MAXSIZE 1030301 #define N 4 unsigned long global_Count; unsigned long temp_global_Count; typedef struct _coordinate { int x; int y; int z; }Coordinate; void DisplayCoordinate(Coordinate* c) { printf("x=%d, y=%d, z=%d/r/n",c->x, c->y, c->z); } Coordinate* NewCoordinate(Coordinate** c, int x, int y, int z) { *c = (Coordinate*)malloc(sizeof(Coordinate)); (*c)->x = x; (*c)->y = y; (*c)->z = z; return *c; } //Caculate C(m,n), M = m, b save the result cursor void combine(Coordinate* a[], int n, int m, int b[], const int M) { int i; for(i=n; i>=m; i--) { b[m-1] = i - 1; if (m > 1) { combine(a,i-1,m-1,b,M); } else { printf("==========Result==========/r/n"); for(int j=M-1; j>=0; j--) { DisplayCoordinate(a[b[j]]); } if(temp_global_Count==ULONG_MAX) global_Count++; else temp_global_Count++; } } } int _tmain(int argc, _TCHAR* argv[]) { int i,j,k; int count = 0; //Coordinate* coordinates[MAXSIZE]; Coordinate** coordinates; Coordinate* c; int result[N]; time_t curTime; tm* start = (tm*)malloc(sizeof(tm)); tm* end = (tm*)malloc(sizeof(tm)); coordinates = (Coordinate**)malloc(sizeof(Coordinate*)*MAXSIZE); global_Count = 0; time(&curTime); localtime_s(start, &curTime); for(i=0;i<=X;i++) { for(j=0;j<=Y;j++) { for(k=0;k<=Z;k++) { c=NewCoordinate(&c,i,j,k); coordinates[count++] = c; } } } combine(coordinates,MAXSIZE,N,result,N); time(&curTime); localtime_s(end, &curTime); printf("==================================================/r/n"); printf("Caculate C(%d,%d) = %u*%u+%u/r/n",N,MAXSIZE,ULONG_MAX, global_Count, temp_global_Count); printf("Start time: %s",asctime(start)); printf("End time: %s",asctime(end)); for(i=0;i<count;i++) { free(coordinates[i]); } return 0; }