Cod sursa(job #830890)

Utilizator TheBestPessimistCristian Viorel Pasat TheBestPessimist Data 7 decembrie 2012 20:13:08
Problema Cifra Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
/*
    http://infoarena.ro/problema/cifra
    as i noticed (and many others, except for cacalin)
    this is solved by generating the last digit for the
    first 100 numbers then suming it.
    what matters from the input is the last 2 digits (if exist)
*/

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int t, n;           /// how many cases, current case

int digit[100];

int raise(int n)
{
    int j;          /// iterators
	int sum = 1;
	for(j = 1; j <= n; ++j)
	{
		sum = (sum * n) % 10;
	}
	return sum;
}


void preprocess()
{
    int i;          /// iterators
    for (i = 0; i < 100; ++i)
    {
        digit[i] = raise(i) % 10;
        std::cout << digit[i] << " ";
    }
    digit[0] = 0;
}

int getDigits(string s)
{
    int x;

    if(s.size() == 1)
        x = s[0] - '0';
    else if (s.size() == 2)
        x = (s[0] - '0') * 10 + s[1] - '0';
    else if (s.size() > 2)
        x = (s[s.size() - 2] - '0') * 10 + (s[s.size() - 1] - '0');

    return x;
}

int main()
{
    int i, j;          /// iterators
    int sum = 0;
    string input;

    ifstream cin ("cifra.in");
    ofstream cout ("cifra.out");

    preprocess();

    cin >> t;

    for (i = 0; i < t; ++i)
    {
        cin >> input;
        sum = 0;
        n = getDigits(input);

        for (j = 0; j <= n; ++j)
            sum = (sum + digit[j]) % 10;

        cout << sum << "\n";
//            cout << sum << endl;
    }
    return 0;
}