Cod sursa(job #1416511)

Utilizator MarianVasilcaMarian Vasilca MarianVasilca Data 8 aprilie 2015 11:26:01
Problema Cifra Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>

using namespace std;

const char *in_file_name = "cifra.in";
const char *out_file_name = "cifra.out";
int *single_digits = new int[10];
int *double_digits = new int[100];

void die(bool assertion, const char *message)
{
	if (assertion) {
		fprintf(stderr, "(%s, %d): ",__FILE__, __LINE__);
		perror(message);
		exit(EXIT_FAILURE);
	}
}

void set_single_vector()
{
	int result;

	for (int i = 0; i < 10; i++) {
		result = i;
		for (int j = 1; j < i; j++) {
			result *= i;
		}
		single_digits[i] = result % 10;
	}

}

void set_double_vector()
{
	int digit;
	for (int i = 0; i < 100; i++) {
		digit = 0;
		for (int j = 0; j <= i; j++) {
			digit += single_digits[j % 10];
		}
		double_digits[i] = digit % 10;
	}
}


int get_last_digit(int n)
{
	int digit = 0;

	for (int i = 1; i <= n; i++)
		digit += single_digits[i % 10];

	return digit % 10;
}

int main()
{

	string line;
	string characters;
	int N, x, digit;

	ifstream in_file;
	ofstream out_file;

	in_file.open(in_file_name, ios::in);
	die(!in_file, "Error opening file for reading");

	out_file.open(out_file_name, ios::out);
	die(!out_file, "Error opening file for writing");

	getline(in_file, line);
	N = atoi(line.c_str());

	set_single_vector();
	set_double_vector();

	for (int i = 0; i < N; i++) {

		getline(in_file, line);

		if (line.length() == 1)
			characters = line.substr(line.length() - 1);
		else
			characters = line.substr(line.length() - 2);

		x = atoi(characters.c_str());
		digit = double_digits[x];
		out_file << digit << endl;

	}

	in_file.close();
	out_file.close();

	return 0;
}