Cod sursa(job #1814201)

Utilizator MihalachiRazvanMihalachi Razvan MihalachiRazvan Data 23 noiembrie 2016 19:01:55
Problema Cifra Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
ifstream f("cifra.in");
ofstream g("cifra.out");
int t,n,p,sx,a[10],px,b[10];
char s[105];
int main()
{
    f>>t;
    a[1]=1;b[1]=1;
    a[2]=5;b[2]=7;
    a[3]=2;b[3]=0;
    a[4]=8;b[4]=6;
    a[5]=3;b[5]=1;
    a[6]=9;b[6]=7;
    a[7]=2;b[7]=4;
    a[8]=8;b[8]=8;
    a[9]=7;b[9]=7;
    for(int i=1;i<=t;i++)
    {
        f>>s;
        n=strlen(s);
        if(n==1)
        {
            p=int(s[0])-48;
            sx=0;
            sx=sx+a[p];
            g<<sx%10<<endl;
        }
        else
        {
            p=int(s[n-2])-48;
            sx=7*p;
            px=int(s[n-1])-48;
                if(p%2==0&&p!=0)
                    sx=sx+a[px];
                else
                    sx=sx+b[px];
             g<<sx%10<<endl;
        }
    }

    f.close();
    g.close();
    return 0;
}