Cod sursa(job #1357060)

Utilizator cojocariustefancojocariu cojocariustefan Data 23 februarie 2015 18:57:34
Problema Cifra Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

ifstream f("cifra.in");
ofstream g("cifra.out");

int mat[5][11] = {{1, 1, 4, 4, 2, 1, 1, 4, 4, 2},
                  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
                  {0, 1, 4, 9, 6, 5, 6, 9, 4, 1},
                  {0, 1, 8, 7, 4, 5, 6, 3, 2, 9},
                  {0, 1, 6, 9, 6, 5, 6, 1, 6, 1}
                  };
int arr[100], k;

void calc(int i, int j)
{
    int s;
    for(; i<=j; i++)
    {
        s = 0;
        if(mat[0][i%10] == 1)
            s = i%10;
        else
            if(mat[0][i%10] == 2)
                if(i%2 == 0)
                    s = mat[2][i%10];
                else
                    s = mat[i%2][i%10];
            else
                if(i%4 == 0)
                    s = mat[4][i%10];
                else
                    s = mat[i%4][i%10];
        arr[i] = s;
    }
    k = j;
}
int gen(int x)
{
    int i, s=0;
    if(x > k)
        calc(k, x);
    for(i=1; i<=x; i++)
        s = s + arr[i];
    return s%10;
}

int main()
{
    int t, i, j, x;
    char n[101];

    f >> t;
    for(i=1; i<=t; i++)
    {
        f.get();
        f.get(n, 101);
        j = strlen(n);
        if(j == 1)
            x = n[0] - 48;
        else
            x = (n[j-2] - 48)*10 + n[j-1]-48;
        g << gen(x) << "\n";
    }

    return 0;
}