• API
• FAQ
• Tools
• Archive
daily pastebin goal
36%
SHARE
TWEET

# Untitled

a guest Jul 11th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <time.h>
4. #include <string.h>
5. #include <math.h>
6. #include <ctype.h>
7. #define DIM 50
8. #define NCOLOUR 4
9. typedef struct {
10.   int repColour; /* tile number, for each colour */
11. } colourID;
12. typedef struct {
13.   colourID colourV[NCOLOUR];
14.   int sizeColour; /* number of diferent colour in the colourV */
15. } combTile;
16.
17. /* this function makes the creation of the pascal triangle of size dim*dim,
18. and then returns 1 if all went ok or 0 if there was a problem in the function */
19. int pascalTriangleCreation (long long int **pascalV, int dim);
20.
21. /* this function makes the calculation of the maxTileRep per colour, and
22. then return maxSizeArray if all went ok or 0 if there was a problem in the
23. function */
24. int maxTileColourCalc (int *maxTileColour, int nColour, int dim);
25.
26. /* this function makes the fulling of the combV array in the position = realSize,
27. then it sorts in descending order of size, and returns 1 if all went ok, or 0 if
28. there was a problem in the function */
29. int fullingComV (combTile *combV, int realSize, int *indexColour, int nColour);
30.
31. /* this function sorts in descending order the array in the colourV, and then
32. return 1 if all went ok or 0 if there was a problem in the function */
33. int sortDescendingOrder (colourID *colourV, int sizeColour);
34.
35. /* this function makes the calculation of the answer using the array combV and
36. the array pascalV, and then returns the answer or -1 if there was a problem in
37. the function */
38. long long int answerCalc (combTile *combV, int realSize, long long int **pascalV);
39.
40. int main () {
41. clock_t beginning, end;
42. double tempo;
43. beginning = clock();
44. /* work to verify */
45. long long int **pascalV, answer=0;
46. int dim = DIM, i,j, booleano, indexColour[NCOLOUR], maxTileColour[NCOLOUR];
47. int nColour = NCOLOUR, maxSizeArrayCombV, realSizeCombV=0;
48. combTile *combV;
49. /* pascal array creation */
50. pascalV = (long long int**) calloc (dim, sizeof (long long int*));
51. if (pascalV == NULL) {
52.   printf("\nThere was an error in the memory allocation of pascalV.");
53. }
54. for (i = 0; i < dim; i++) {
55.   pascalV[i] = (long long int *) calloc (dim, sizeof (long long int));
56.   if (pascalV[i] == NULL) {
57.     printf("\nThere was an error in the memory allocation of pascalV[i].");
58.   }
59. }
60. /* pascal array fulling */
61. booleano = pascalTriangleCreation (pascalV, dim);
62. if (booleano == 0) {
63.   printf ("\nThere was a problem in the function pascalTriangleCreation.");
64.   return 0;
65. }
66. /* maxCalculation */
67. booleano = maxTileColourCalc (maxTileColour, nColour, dim);
68. if (booleano == 0) {
69.   printf("\nThere was a problem in the function maxTileColour.");
70.   return 0;
71. }
72. /* array creation */
73. maxSizeArrayCombV = booleano;
74. combV = (combTile*) calloc (maxSizeArrayCombV, sizeof (combTile));
75. if (combV == NULL) {
76.   printf("\nThere was a problem in the memory allocation of combV.");
77. }
78. /* cycle */
79. for (indexColour[0] = 0; indexColour[0] < maxTileColour[0]+1; indexColour[0]++) {
80.   for (indexColour[1] = 0; indexColour[1] < maxTileColour[1]+1; indexColour[1]++) {
81.     /* seed cycle 1 */
82.     booleano = indexColour[0]+indexColour[1]*2;
83.     if (booleano > dim) { break; }
84.     for (indexColour[2] = 0; indexColour[2] < maxTileColour[2]+1; indexColour[2]++) {
85.       /* seed cycle 2 */
86.       booleano = indexColour[0]+indexColour[1]*2+indexColour[2]*3;
87.       if (booleano > dim) { break; }
88.       for (indexColour[3] = 0; indexColour[3] < maxTileColour[3]+1; indexColour[3]++) {
89.           /* seed cycle 3 */
90.           booleano = indexColour[0]+indexColour[1]*2+indexColour[2]*3+indexColour[3]*4;
91.           if (booleano > dim) {
92.             break;
93.           } else if (booleano == dim) {
94.             /* fulling values in array comV */
95.               booleano = fullingComV (combV, realSizeCombV, indexColour, nColour);
96.               if (booleano == 0) {
97.                 printf("\nThere was a problem in the function fullingCombV.");
98.                 return 0;
99.               }
100.               realSizeCombV++;
101.           } else {
102.             continue;
103.           }
104.       }
105.     }
106.   }
107. }
108. /* reallocation of the extra memory */
109. combV = (combTile*) realloc (combV, realSizeCombV*sizeof (combTile));
110. if (combV == NULL) {
111.   printf("\nThere was an error in the reallocation of combV.");
112.   return 0;
113. }
116. if (answer == -1) {
117.   printf("\nThere was an erro in the function answerCalc.");
118.   return 0;
119. }
120. /* free memory */
121. for (i = 0; i < dim; i++) {
122.   free (pascalV[i]);
123. }
124. free (pascalV);
125. free (combV);
126. printf ("\n");
127. /* end of the work */
128. end = clock();
129. tempo = (double)(end - beginning) / CLOCKS_PER_SEC;
130. printf("\nTime in seconds: %lf", tempo);
131. printf("\nFor a row of %d tile there are %lld diferent combinations.", dim, answer);
132.
133. return 0;
134. }
135. /******************************************************************************/
136. int pascalTriangleCreation (long long int **pascalV, int dim){
137. /* this function makes the creation of the pascal triangle of size dim*dim,
138. and then returns 1 if all went ok or 0 if there was a problem in the function */
139. if (pascalV == NULL || dim < 1) {
140.   return 0;
141. }
142. int i, j;
143. for (i = 0; i < dim; i++) {
144.   /* starting value */
145.   if (i == 0) {
146.     pascalV[0][0] = 1;
147.     continue;
148.   }
149.   for (j = 0; j < i+1; j++) {
150.     if (j == 0) {
151.       pascalV[i][j] = 1;
152.     } else {
153.       pascalV[i][j] = pascalV[i-1][j-1]+pascalV[i-1][j];
154.     }
155.   }
156. }
157. return 1;
158. }
159. /******************************************************************************/
160. int maxTileColourCalc (int *maxTileColour, int nColour, int dim) {
161. /* this function makes the calculation of the maxTileRep per colour, and
162. then return maxSizeArray if all went ok or 0 if there was a problem in the
163. function */
164. if (maxTileColour == NULL || nColour < 1 || dim < 1) {
165.   return 0;
166. }
167. int i, size = 1, maxSize = 1;
168. for (i = 0; i < nColour; i++) {
169.   maxTileColour[i] = dim/size;
170.   size++;
171.   maxSize*=maxTileColour[i];
172. }
173. return maxSize;
174. }
175. /******************************************************************************/
176. int fullingComV (combTile *combV, int realSize, int *indexColour, int nColour) {
177. /* this function makes the fulling of the combV array in the position = realSize,
178. then it sorts in descending order of size, and returns 1 if all went ok, or 0 if
179. there was a problem in the function */
180. if (combV == NULL || indexColour == NULL || realSize < 0 || nColour < 0) {
181.   return 0;
182. }
183. int i,j, nColourSize = 0, booleano;
184. /* fulling */
185. for (i = 0; i < nColour; i++) {
186.   combV[realSize].colourV[i].repColour = indexColour[i];
187.   if (indexColour[i] != 0) {
188.     nColourSize++;
189.   }
190. }
191. combV[realSize].sizeColour = nColourSize;
192. /* sorting in descending order */
193. booleano = sortDescendingOrder (combV[realSize].colourV, nColour);
194. if (booleano == 0) {
195.   printf("\nThere was an error in the function sortDescendingOrder.");
196.   return 0;
197. }
198. return 1;
199. }
200. /******************************************************************************/
201. int sortDescendingOrder (colourID *colourV, int sizeColour) {
202. /* this function sorts in descending order the array in the colourV, and then
203. return 1 if all went ok or 0 if there was a problem in the function */
204. if (colourV == NULL || sizeColour < 0) {
205.   return 0;
206. }
207. int i, j;
208. colourID aux;
209. for (i = 0; i < sizeColour-1; i++) {
210.   for (j = i+1; j < sizeColour; j++) {
211.     if (colourV[i].repColour < colourV[j].repColour) {
212.       /* aux = i */
213.       aux.repColour = colourV[i].repColour;
214.       /* i = j */
215.       colourV[i].repColour = colourV[j].repColour;
216.       /* j = aux */
217.       colourV[j].repColour = aux.repColour;
218.     }
219.   }
220. }
221. return 1;
222. }
223. /******************************************************************************/
224. long long int answerCalc (combTile *combV, int realSize, long long int **pascalV) {
225. /* this function makes the calculation of the answer using the array combV and
226. the array pascalV, and then returns the answer or -1 if there was a problem in
227. the function */
228. if (combV == NULL || pascalV == NULL || realSize < 1) {
229.   return -1;
230. }
231. long long int answer = 0;
232. int i, p1, p2, p3, n1, n2, n3;
233. for (i = 0 ; i < realSize; i++) {
234.   if (combV[i].sizeColour == 1) {
235.     /* 1 colour */
237.   } else if (combV[i].sizeColour == 2) {
238.     /* 2 colour */
239.     p1 = combV[i].colourV[0].repColour+combV[i].colourV[1].repColour;
240.     n1 = combV[i].colourV[1].repColour;
242.   } else if (combV[i].sizeColour == 3) {
243.     /* 3 colour */
244.     p1 = combV[i].colourV[0].repColour+combV[i].colourV[1].repColour+combV[i].colourV[2].repColour;
245.     n1 = combV[i].colourV[1].repColour+combV[i].colourV[2].repColour;
246.     p2 = combV[i].colourV[1].repColour+combV[i].colourV[2].repColour;
247.     n2 = combV[i].colourV[1].repColour;
249.   } else {
250.     /* 4 colour */
251.     p1 = combV[i].colourV[0].repColour+combV[i].colourV[1].repColour+combV[i].colourV[2].repColour+combV[i].colourV[3].repColour;
252.     n1 = combV[i].colourV[1].repColour+combV[i].colourV[2].repColour+combV[i].colourV[3].repColour;
253.     p2 = combV[i].colourV[1].repColour+combV[i].colourV[2].repColour+combV[i].colourV[3].repColour;
254.     n2 = combV[i].colourV[2].repColour+combV[i].colourV[3].repColour;
255.     p3 = combV[i].colourV[2].repColour+combV[i].colourV[3].repColour;
256.     n3 = combV[i].colourV[2].repColour;