Cod sursa(job #2589688)

Utilizator MihaiutcnStancu Mihai Cristian Mihaiutcn Data 26 martie 2020 18:30:58
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <stdio.h>
#include <stdlib.h>

float putere(float nr,int exp)
{
    if(exp < 0)
        return putere(1/nr, -exp);
    else if(exp==0)
        return 1;
    else if(exp==1)
        return nr;
    else if(exp%2==0)
        return putere( nr*nr , exp/2);
    else
        return(nr*putere( nr*nr , exp/2));
}


int main()
{
    FILE* f=fopen("lgput.in","r");
    FILE* g=fopen("lgput.out","w");
    float n;
    int exp;
    fscanf(f,"%f %d",&n,&exp);
    fprintf(g,"%g",putere(n,exp));
    fclose(f);
    fclose(g);
    return 0;
}