Pagini recente » Cod sursa (job #2670576) | Cod sursa (job #3245371) | Borderou de evaluare (job #1796793) | Cod sursa (job #3132253) | Cod sursa (job #3242067)
#include <stdio.h>
#include <stdlib.h>
// Check if long long is needed
int fractal(int order, int x, int y) {
/* If order = 0, there are no segments on the curve.
In fact, the curve is just a point in this case.
If order = 0, then x and y must be 1 for the function call to make sense.
*/
if (order == 0) return 0;
int pow2 = (1 << (order - 1));
// Segments needed to traverse a quarter: 4^(order-1) - 1.
// Recurrence is a_(n+1) = 4*a_n + 3.
int quarter = (1 << (2 * (order - 1))) - 1;
if (x <= pow2 && y <= pow2) return fractal(order - 1, y, x);
if (x <= pow2 && y > pow2)
return quarter + 1 + fractal(order - 1, x, y - pow2);
if (x > pow2 && y > pow2)
return 2 * quarter + 2 + fractal(order - 1, x - pow2, y - pow2);
if (x > pow2 && y <= pow2)
return 3 * quarter + 3 + fractal(order - 1, pow2 + 1 - y, 2 * pow2 + 1 - x);
}
int main() {
FILE *fp_in, *fp_out;
int k, x, y;
fp_in = fopen("fractal.in", "r");
fp_out = fopen("fractal.out", "w");
fscanf(fp_in, "%d %d %d", &k, &x, &y);
fprintf(fp_out, "%d", fractal(k, x, y));
fclose(fp_in);
fclose(fp_out);
return 0;
}