Cod sursa(job #1599369)

Utilizator mateinMatei Nistor Ionut matein Data 13 februarie 2016 20:03:57
Problema Cifra Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main() {
	int t,len,rest,cat,ult_cif,aux;
	char n[105];
	int ult[20] = {0,1, 5, 2, 8, 3, 9, 2, 8, 7, 7, 8, 4, 7, 3, 8, 4, 1, 5, 4}; 
	FILE *pFile,*oFile;
	pFile = fopen ( "cifra.in" , "r" );
	oFile = fopen ( "cifra.out" , "w" );
	fscanf(pFile, "%d", &t);
	fgets(n,10,pFile);
	for(int i=0; i<t; i++) {
		fgets(n, 105, pFile);
		len = strlen(n)-1;		
		if( len == 1) {
			aux = n[0]-'0';
			ult_cif = ult[aux];
		} else {
			rest = 10*(n[len-2]-'0') + n[len-1]-'0';
			cat = rest/20;
			rest = rest%20;
			ult_cif = cat*4+ult[rest];
			ult_cif %= 10;
		}
		ult_cif += '0';
		fprintf(oFile, "%c\n" , (char)ult_cif);
	}
	fclose(pFile);
	fclose(oFile);
	return 0;
}