Cod sursa(job #1823222)

Utilizator deresurobertoFMI - Deresu Roberto deresuroberto Data 6 decembrie 2016 00:57:13
Problema Cifra Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
#include <cstring>
using namespace std;

ifstream fin("cifra.in");
ofstream fout("cifra.out");
int t, n, puteri[10][5];
char str[101];

int main()
{
    //precalcul puterile pana la ordin 4 ale cifrelor 1..9
    //dupa ordinul 4 toate se repeta
    for (int cifra = 1; cifra <= 9; cifra++)
    {
        puteri[cifra][1] = cifra;
        for (int putere = 2; putere <= 4; putere++)
            puteri[cifra][putere] = (puteri[cifra][putere - 1] * cifra) % 10;
    }

    fin >> t;

    fin.getline(str, sizeof(str));
    while (t--)
    {
        fin.getline(str, sizeof(str));

        if (strlen(str) >= 2)
            n = (str[strlen(str)-2]-'0') * 10 + str[strlen(str)-1]-'0';
        else
            n = str[strlen(str)-1]-'0';

        int res = 0;
        for (int i = 1; i <= n; i++)
        {
            int pow = i % 4;
            if (pow == 0)
                pow = 4;

            res = (res + puteri[i%10][pow])%10;
        }

        fout << res << "\n";
    }

    return 0;
}