Cod sursa(job #2157903)

Utilizator MoldooooooooMoldoveanu Stefan Moldoooooooo Data 9 martie 2018 23:41:25
Problema Cifra Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <fstream>
#include <cstring>
#include <cmath>
using namespace std;
ifstream fin("cifra.in");
ofstream fout("cifra.out");
short int vx2[]={0, 0, 4, 7, 6, 0, 0, 3, 6, 9};
short int restx0(char HN[101], short int x){
    short int S=0;
    if((HN[strlen(HN)-1]-'0')<x) S+=((HN[strlen(HN)-2]-'0')*x)%10;
          else S+=((HN[strlen(HN)-2]+1-'0')*x)%10;
    return S;
}

short int restx1(char HN[101], short int x){
    short int S=0;
    if((HN[strlen(HN)-1]-'0')<x) S+=((HN[strlen(HN)-2]-'0')*vx2[x])%10;
          else S+=((HN[strlen(HN)-2]+1-'0')*vx2[x])%10;
    return S;
}

short int restx2(char HN[101], short int x){
    short int S=0;
    if(((HN[strlen(HN)-1]-'0')<x && (HN[strlen(HN)-2]%2==1)) || ((HN[strlen(HN)-1]-'0')>=x && (HN[strlen(HN)-2]%2==0))) S+=vx2[x];
    return S;
}

int main()
{
    short int S=0, j;
    int T, i, p, k;
    fin>>T;
    char N[101];
    fin.get();
    for(i=1; i<=T; i++){
            S=0;
            fin.getline(N, 101);
            if(strlen(N)>=2){S+=restx0(N, 1); S+=restx0(N, 5); S+=restx0(N, 6); S%=10;
            S+=restx1(N, 4); S+=restx1(N, 9); S%=10;
            S+=restx2(N, 2); S+=restx2(N, 3); S+=restx2(N, 7); S+=restx2(N, 8); S%=10;}
             else for(j=1; j<=(N[strlen(N)-1]-'0'); j++) {p=1; for(k=1; k<=j; k++) {p*=j; p%=10;} S+=p; S%=10;}
            fout<<S<<endl;}
}