Cod sursa(job #2876975)

Utilizator NedusNedelcu Alexandru Nedus Data 23 martie 2022 22:51:53
Problema Fractal Scor 100
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int getResult(int n, int x, int y) {

            if(n==1){
                if(x==1 && y==1) return 0;
                if(x==1 && y==2) return 1;
                if(x==2 && y==1) return 3;
                if(x==2 && y==2) return 2;
            }
            int putere = (int) pow(2,n-1);
            int de_adaugat = (int) pow(4,n-1);
            if(x>putere){
                if(y>putere) {
                    return 2 * de_adaugat + getResult(n-1,x-putere,y-putere);
                }
                else {
                    return 3 * de_adaugat + getResult(n-1,putere-y+1,putere-(x-putere)+1);
                }
            }
            else{
                if(y>putere){
                    return de_adaugat + getResult(n-1,x,y-putere);
                }
                else {
                    return getResult(n-1,y,x);
                }
            }
        }


int main()
{
    FILE *fr;
    fr = fopen("fractal.in","r");
    int n,x,y;
    fscanf(fr,"%d",&n);
    fscanf(fr,"%d",&x);
    fscanf(fr,"%d",&y);
    fclose(fr);

    FILE *fw;
    fw = fopen("fractal.out","w+");
    int r = getResult(n,x,y);

    fprintf(fw, "%d",r);
    fclose(fw);
    return 0;
}