Cod sursa(job #1586863)

Utilizator PraetorGrigorosoaia Florin Praetor Data 1 februarie 2016 18:05:47
Problema Cifra Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include<fstream>
#include<string.h>
#define NRMAXCIFRE 101

using namespace std;

FILE*in;
ofstream out("cifra.out");

int T;
char NUMBER[NRMAXCIFRE];
int LAST[NRMAXCIFRE];
int nr_cifre_NUMBER;

int POWER(int ordin)
{
    int P=ordin, solution=1;

    while(P)
    {
        if (P % 2)
            solution=(ordin*solution)%10;
        ordin=(ordin*ordin)%10;
        P/=2;
    }

    return solution;
}

void build_LAST()
{
    for (int i=1; i<=100; i++)
        LAST[i]=(LAST[i-1]+POWER(i))%10;
}

void read()
{
    in=fopen("cifra.in", "r");

    fscanf(in, "%d", &T);

    build_LAST();

    for (int i=1; i<=T; i++)
    {
        fscanf(in, "%s", &NUMBER);

        nr_cifre_NUMBER=strlen(NUMBER);

        if (nr_cifre_NUMBER == 1)
            out<<LAST[NUMBER[0]-'0']<<'\n';
        else
            out<<LAST[(NUMBER[nr_cifre_NUMBER-2]-'0')*10+(NUMBER[nr_cifre_NUMBER-1]-'0')]<<'\n';
    }
}

int main()
{
    read();

    return 0;
}