Cod sursa(job #546818)

Utilizator aleph0Ionut-Gabriel Radu aleph0 Data 5 martie 2011 15:30:45
Problema Cifra Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 0.95 kb
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int finalCif(char *s)
{
    int even[] = {7, 1, 5, 2, 8, 3, 9, 2, 8, 7};
    int odd[] =  {7, 1, 7, 0, 6, 1, 7, 4, 8, 7}, c1, c2, x, y;
    if(strlen(s) == 1)
        return even[s[0] - '0'];
    else {
        c1 = s[strlen(s) - 2] - '0';
        c2 = s[strlen(s) - 1] - '0';
        if(c1 == 0 && c2 == 0)
            return 0;
        if(c1 == 0)
            return even[c2];
        else if(c1 % 2 == 0)
            return (7 * (c1) + even[c2]) % 10;
        else
            return (7 * (c1) + odd[c2]) %10;
    }

}

int main()
{
    FILE *f = fopen("cifra.in","r");
    FILE *g = fopen("cifra.out","w");
    int T, i;
    char *N;
    fscanf(f,"%d",&T);
    fflush(stdin);
    for(i = 0; i < T; i++){
        N = (char*)malloc(101 * sizeof(char));
        fscanf(f,"%s",N);
        fprintf(g,"%d\n", finalCif(N));
        free(N);
        fflush(stdin);
    }
    fclose(f);
    fclose(g);
    return 0;
}