Cod sursa(job #1437201)

Utilizator george.stefanGeorge Stefan george.stefan Data 17 mai 2015 02:41:53
Problema Cifra Scor 10
Compilator c Status done
Runda Arhiva de probleme Marime 1.98 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
1->1
2->2 4 8 6
3->3 9 7 1
4->4 6
5->5
6->6
7->7 9 3 1
8->8 4 2 6
9->9 1
0->0
*/
int ultima_cifra(int x)
{
    switch(x%10)
    {
        case 1: return 1;
        case 2:
            switch(x%4)
            {
                    case 0: return 6;
                    case 2: return 4;
            }
        case 3:
            switch(x%4)
            {
                    case 1: return 3;
                    case 3: return 7;
            }
        case 4: return 6;
        case 5: return 5;
        case 6: return 6;
        case 7:
            switch(x%4)
            {
                    case 1: return 7;
                    case 3: return 3;
            }
        case 8:
            switch(x%4)
            {
                    case 0: return 6;
                    case 2: return 4;
            }
        case 9: return 9;
        case 0: return 0;

    }
}
int main(void)
{
    int nr_valori,i,j,val[100];
    int suma;
    char x[200];

    FILE* f=fopen("cifra.in","rt");
    FILE* g=fopen("cifra.out","wt");

    fgets(x,1000,f);

    nr_valori=atoi(x);

    for(i=0;i<100;i++)
    {
        suma=0;
        for(j=1;j<=i;j++)
        {
            suma+=ultima_cifra(j);
            suma=suma%10;
        }
        val[i]=suma;
        printf("%d ",val[i]);
    }

    for(i=0;i<nr_valori-1;i++)
    {
        fgets(x,1000,f);

        if((x[strlen(x)-3]-48)<=0)
            suma=val[x[strlen(x)-2]-48];
        else
            suma=val[(x[strlen(x)-2]-48)+(x[strlen(x)-3]-48)*10];

        fprintf(g,"%d\n",suma);
    }

        suma=0;

        fgets(x,1000,f);

        if((x[strlen(x)-2]-48)>0)
        for(j=1;j<=(x[strlen(x)-1]-48)+(x[strlen(x)-2]-48)*10;j++)
            suma=val[(x[strlen(x)-1]-48)+(x[strlen(x)-2]-48)*10];
        else
            suma=val[(x[strlen(x)-2]-48)];

        fprintf(g,"%d\n",suma);



    return 0;

}