Pagini recente » Cod sursa (job #1342299) | Cod sursa (job #1014570) | Cod sursa (job #3334583) | Cod sursa (job #1742780) | Cod sursa (job #3346679)
#include <stdio.h>
#include <stdlib.h>
struct punct {
int x, y;
};
void swap( int *x, int *y ) {
int cop = *x;
*x = *y;
*y = cop;
}
long long pas_fractal( int k, struct punct * punct ) {
if ( k == 0 )
return 0;
long long lungime_latura = 1LL << k, arie = ( 1LL << ( k - 1 ) ) * ( 1LL << ( k - 1 ) );
long long jumatate = 1LL << (k - 1);
if ( punct -> x < ( 1LL << ( k - 1 ) ) && punct -> y < ( 1LL << ( k - 1 ) ) ) {
swap( &punct -> x, &punct -> y);
return pas_fractal( k - 1, punct );
}
else if ( punct -> x >= ( 1LL << ( k - 1 ) ) && punct -> y < ( 1LL << ( k - 1 ) ) ) {
punct -> x = punct -> x - jumatate;
return arie + pas_fractal(k - 1, punct);
}
else if ( punct -> x >= ( 1LL << ( k - 1 ) ) && punct -> y >= ( 1LL << ( k - 1 ) ) ) {
punct -> x -= jumatate;
punct -> y -= jumatate;
return arie * 2 + pas_fractal( k - 1, punct );
}
else if ( punct -> x < ( 1LL << ( k - 1 ) ) && punct -> y >= ( 1LL << ( k - 1 ) ) ) {
swap ( &punct -> x, &punct -> y );
punct -> x = jumatate - 1 - ( punct -> x - jumatate);
punct -> y = jumatate - 1 -punct -> y;
return arie * 3 + pas_fractal( k - 1, punct );
}
}
int main() {
FILE *f = fopen("fractal.in", "r"),
*g = fopen("fractal.out", "w");
if ( f == NULL ) {
printf("nu s a deschis fractal.in");
return -1;
}
if ( g == NULL ) {
printf("nu s a deshcis fractal.out");
return -1;
}
int k, x, y;
fscanf(f, "%d %d %d", &k, &x, &y);
x--, y--;
struct punct punct;
punct.x = y, punct.y = x;
fprintf(g, "%lld", pas_fractal(k, &punct));
fclose(f), fclose(g);
return 0;
}