Cod sursa(job #472347)

Utilizator marius.bucurBucur Marius - Ovidiu marius.bucur Data 23 iulie 2010 23:39:49
Problema Cifra Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.14 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>

#define MAX_DIGITS 102

using namespace std;

char last_digit(char* bignum, char* bigpow)
{
    if(strlen(bigpow) == 1 && *bigpow == '0')
        return 1;
    char ld = bignum[strlen(bignum) - 1] - '0';
    char powu = bigpow[strlen(bigpow) - 1];
    char powz = bigpow[strlen(bigpow) - 2];
    char pow_last = (powu - '0') + 10 * (powz - '0');
    if(ld == 0)
        return 0;
    if(ld == 1)
        return 1;
    if(ld == 2)
    {
        if(pow_last % 4 == 0)
            return 6;
        if(pow_last % 4 == 1)
            return 2;
        if(pow_last % 4 == 2)
            return 4;
        if(pow_last % 4 == 3)
            return 8;
    }
    if(ld == 3)
    {
        if(pow_last % 4 == 0)
            return 1;
        if(pow_last % 4 == 1)
            return 3;
        if(pow_last % 4 == 2)
            return 9;
        if(pow_last % 4 == 3)
            return 7;
    }
    if(ld == 4)
    {
        if(pow_last % 2 == 0)
            return 6;
        else
            return 4;
    }
    if(ld == 5)
        return 5;
    if(ld == 6)
        return 6;
    if(ld == 7)
    {
        if(pow_last % 4 == 0)
            return 1;
        if(pow_last % 4 == 1)
            return 7;
        if(pow_last % 4 == 2)
            return 9;
        if(pow_last % 4 == 3)
            return 3;
    }
    if(ld == 8)
    {
        if(pow_last % 4 == 0)
            return 6;
        if(pow_last % 4 == 1)
            return 8;
        if(pow_last % 4 == 2)
            return 4;
        if(pow_last % 4 == 3)
            return 2;
    }
    if(ld == 9)
    {
        if(pow_last % 2 == 0)
            return 1;
        else
            return 9;
    }
}

char s[MAX_DIGITS];

int main()
{
    ifstream in("cifra.in");
    int N;
    in >> N;
    int total = 0;
    for(int i = 0; i < N; i++)
    {
        in >> s;
        total += last_digit(s, s);
    }
    ofstream out("cifra.out");
    out << total;
    in.close();
    out.close();
    return 0;
}