tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
39
Task/Pascals-triangle-Puzzle/C/pascals-triangle-puzzle-1.c
Normal file
39
Task/Pascals-triangle-Puzzle/C/pascals-triangle-puzzle-1.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/* Pascal's pyramid solver
|
||||
*
|
||||
* [top]
|
||||
* [ ] [ ]
|
||||
* [mid] [ ] [ ]
|
||||
* [ ] [ ] [ ] [ ]
|
||||
* [ x ] [ a ] [ y ] [ b ] [ z ]
|
||||
* x + z = y
|
||||
*
|
||||
* This solution makes use of a little bit of mathematical observation,
|
||||
* such as the fact that top = 4(a+b) + 7(x+z) and mid = 2x + 2a + z.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
void pascal(int a, int b, int mid, int top, int* x, int* y, int* z)
|
||||
{
|
||||
double ytemp = (top - 4 * (a + b)) / 7.;
|
||||
if(fmod(ytemp, 1.) >= 0.0001)
|
||||
{
|
||||
x = 0;
|
||||
return;
|
||||
}
|
||||
*y = ytemp;
|
||||
*x = mid - 2 * a - *y;
|
||||
*z = *y - *x;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int a = 11, b = 4, mid = 40, top = 151;
|
||||
int x, y, z;
|
||||
pascal(a, b, mid, top, &x, &y, &z);
|
||||
if(x != 0)
|
||||
printf("x: %d, y: %d, z: %d\n", x, y, z);
|
||||
else printf("No solution\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
73
Task/Pascals-triangle-Puzzle/C/pascals-triangle-puzzle-2.c
Normal file
73
Task/Pascals-triangle-Puzzle/C/pascals-triangle-puzzle-2.c
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void show(int *x) {
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < 5; i++)
|
||||
for (j = 0; j <= i; j++)
|
||||
printf("%4d%c", *(x++), j < i ? ' ' : '\n');
|
||||
}
|
||||
|
||||
inline int sign(int i)
|
||||
{
|
||||
return i < 0 ? -1 : i > 0;
|
||||
}
|
||||
|
||||
int iter(int *v, int *diff) {
|
||||
int sum, i, j, e = 0;
|
||||
|
||||
# define E(x, row, col) x[(row) * ((row) + 1) / 2 + (col)]
|
||||
/* enforce boundary conditions */
|
||||
E(v, 0, 0) = 151;
|
||||
E(v, 2, 0) = 40;
|
||||
E(v, 4, 1) = 11;
|
||||
E(v, 4, 3) = 4;
|
||||
|
||||
/* calculate difference from equilibrium */
|
||||
for (i = 1; i < 5; i++) {
|
||||
for (j = 0; j <= i; j++) {
|
||||
E(diff, i, j) = 0;
|
||||
if (j < i)
|
||||
E(diff, i, j) += E(v, i - 1, j) -
|
||||
E(v, i, j + 1) -
|
||||
E(v, i, j);
|
||||
if (j)
|
||||
E(diff, i, j) += E(v, i - 1, j - 1) -
|
||||
E(v, i, j - 1) -
|
||||
E(v, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
for (j = 0; j < i; j++)
|
||||
E(diff, i, j) += E(v, i + 1, j) +
|
||||
E(v, i + 1, j + 1) -
|
||||
E(v, i, j);
|
||||
|
||||
E(diff, 4, 2) += E(v, 4, 0) + E(v, 4, 4) - E(v, 4, 2);
|
||||
# undef E
|
||||
|
||||
/* Do feedback, check if we are done. */
|
||||
for (i = sum = 0; i < 15; i++) {
|
||||
sum += !!sign(e = diff[i]);
|
||||
|
||||
/* 1/5-ish feedback strength on average. These numbers are highly
|
||||
magical, depending on nodes' connectivities. */
|
||||
if (e >= 4 || e <= -4) v[i] += e/5;
|
||||
else if (rand() < RAND_MAX/4) v[i] += sign(e);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int v[15] = { 0 }, diff[15] = { 0 }, i, s;
|
||||
|
||||
for (i = s = 1; s; i++) {
|
||||
s = iter(v, diff);
|
||||
printf("pass %d: %d\n", i, s);
|
||||
}
|
||||
show(v);
|
||||
|
||||
return 0;
|
||||
}
|
||||
13
Task/Pascals-triangle-Puzzle/C/pascals-triangle-puzzle-3.c
Normal file
13
Task/Pascals-triangle-Puzzle/C/pascals-triangle-puzzle-3.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
pass 1: 12
|
||||
pass 2: 12
|
||||
pass 3: 14
|
||||
pass 4: 14
|
||||
...
|
||||
pass 113: 4
|
||||
pass 114: 7
|
||||
pass 115: 0
|
||||
151
|
||||
81 70
|
||||
40 41 29
|
||||
16 24 17 12
|
||||
5 11 13 4 8
|
||||
Loading…
Add table
Add a link
Reference in a new issue