Cod sursa(job #1996804)

Utilizator Rodik_RodyRodica Vasilescu Rodik_Rody Data 2 iulie 2017 16:55:51
Problema Cifra Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 2.22 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int cifra(int x, int p)
{
    if(x==0||x==1||x==5||x==6)
        return x;

    if(x==2){
        if(p%4==1)
          return 2;
         else
            if (p%4==2)
                  return 4;
             else
                if(p%4==3)
                   return 8;
                 else
                    return 6;
    }
     if(x==3){
        if(p%4==1)
          return 3;
         else
            if (p%4==2)
                  return 9;
             else
                if(p%4==3)
                   return 7;
                 else
                    return 1;
     }
     if(x==4){
        if(p%2==1)
          return 4;
         else
            return 6;
     }
     if(x==7){
        if(p%4==1)
          return 7;
         else
            if (p%4==2)
                  return 9;
             else
                if(p%4==3)
                   return 3;
                 else
                    return 1;
     }
     if(x==8){
        if(p%4==1)
          return 8;
         else
            if (p%4==2)
                  return 4;
             else
                if(p%4==3)
                   return 2;
                 else
                    return 6;
     }
     if(x==9){
        if(p%2==1)
          return 9;
         else
           return 1;
     }
     return 1;
}

void scadere_1(char a[]){
    int i=0, x=0;
    strrev(a);
    while(x<10){
        x = a[i] - '0' -1 + 10 ;
        a[i] = x%10 + '0';
        i++;
    }

    strrev(a);
    while(a[0]=='0'){
        strcpy(a,a+1);
    }
    if(a[0]==NULL)
        a[0]='0';
}
int main()
{

    int t,i,s,c,p;
    char n[30001],x;
    FILE *f = fopen("cifra.in", "r");
    FILE *g = fopen("data.out", "w");
    fscanf(f,"%d",&t);
    fscanf(f,"%c",&x);
    for(i = 1 ; i <= t ; i++){
        fscanf(f,"%s",n);
        s=0;
        while(strcmp(n,"0")){
            c = n[strlen(n)-1]-'0';
            if(strlen(n)> 1)
                p = (n[strlen(n)-2]-'0')*10+c;
            else p = c;
            s = (s + cifra(c,p))%10;

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

    return 0;
}