Cod sursa(job #1219290)

Utilizator allexx2200Atanasiu Alexandru-Marian allexx2200 Data 13 august 2014 22:17:11
Problema Cifra Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define FIN "cifra.in"
#define FOUT "cifra.out"

#define ASCII(c) (int)c

int main() {
	int t,i,k,n,rez,aux;
	char x[100];
	FILE *in, *out;
	in = fopen(FIN, "rt");
	out = fopen(FOUT, "wt");
	
	fscanf(in, "%d", &t);
	for(i=0; i < t; i++){
		fscanf(in, "%s", x);
		if(strlen(x) > 1){
			n = (x[strlen(x) - 2] - ASCII('0'))*10 + x[strlen(x) - 1] - ASCII('0'); 
		} else {
			n = x[0] - ASCII('0');
		} 		
		for(k=1,rez=0; k <= n; k++){
			if(k%10 == 1){
				rez += 1;
			} else if (k%10 == 2) {
				aux = k%4;
				if(aux == 1) {
					rez += 2;
				} else if(aux == 2) {
					rez += 4;
				} else if(aux == 3) {
					rez += 8;
				} else {
					rez += 6;
				}
			} else if (k%10 == 3) {
				aux = k%4;
				if(aux == 1) {
					rez += 3;
				} else if(aux == 2){
					rez += 9;
				} else if(aux == 3){
					rez += 7;
				} else {
					rez += 1;
				}
			} else if (k%10 == 4) {
				aux = k%2;
				if(aux == 1){
					rez += 4;
				} else {
					rez += 6;
				}
			} else if (k%10 == 5) {
				rez += 5;
			} else if (k%10 == 6) {
				rez += 6;
			} else if (k%10 == 7) {
				aux = k%4;
				if(aux == 1){
					rez += 7;
				} else if (aux == 2){
					rez += 9;
				} else if (aux == 3){
					rez += 3;
				} else {
					rez += 1;
				}
			} else if (k%10 == 8) {
				aux = k%4;
				if(aux == 1){
					rez += 8;
				} else if (aux == 2){
					rez += 4;
				} else if (aux == 3){
					rez += 2;
				} else {
					rez += 6;
				}
			} else if (k%10 == 9) {
				aux = k%2;
				if(aux == 1){
					rez += 9;
				} else {
					rez += 1;
				}
			}
			rez %= 10;
		}
		fprintf(out, "%d\n", rez%10);
	}
	return 0;
}