Pagini recente » Cod sursa (job #108713) | Cod sursa (job #1617425) | Cod sursa (job #1424920) | Cod sursa (job #2681525) | Cod sursa (job #1416516)
#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 *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);
}
}
int get_power(int n)
{
int digit = 1;
for (int i = 1; i <= n; i++) {
digit *= n; digit %= 10;
}
return digit;
}
void set_double_vector()
{
int digit;
double_digits[0] = 0;
for (int i = 1; i < 100; i++)
double_digits[i] = (double_digits[i - 1] + get_power(i)) % 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_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;
}