Cod sursa(job #1537737)

Utilizator Balescu_OvidiuBalescu Ovidiu-Gheorghe Balescu_Ovidiu Data 27 noiembrie 2015 21:32:35
Problema Cifra Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <stdio.h>
#include <string.h>

short pow(short cf,short p){
	if(cf==1)
		return 1;
	if(cf==2){
		if(p%4==0)
			return 6;
		if(p%4==1)
			return 2;
		if(p%4==2)
			return 4;
		return 8;
	}
	if(cf==3){
		if(p%4==0)
			return 1;
		if(p%4==1)
			return 3;
		if(p%4==2)
			return 9;
		return 7;
	}
	if(cf==4){
		if(p%2==0)
			return 6;
		return 4;
	}
	if(cf==5)
		return 5;
	if(cf==6)
		return 6;
	if(cf==7){
		if(p%4==0)
			return 1;
		if(p%4==1)
			return 7;
		if(p%4==2)
			return 9;
		return 3;
	}
	if(cf==8){
		if(p%4==0)
			return 6;
		if(p%4==1)
			return 8;
		if(p%4==2)
			return 4;
		return 2;
	}
	if(cf==9){
		if(p%2==0)
			return 1;
		return 9;
	}
}
int main(){
	FILE*f=fopen("cifra.in","r");
	FILE*g=fopen("cifra.out","w");
	unsigned T;
	fscanf(f,"%u",&T);
	while(T--){
		char n[101];
		unsigned long nr=0,s=1; short k;
		fscanf(f,"%s",&n);
		k=strlen(n)-1;
		while(n[k]=='0'&&k)
			k--;
		for(short i=k;i<strlen(n);i++)
			nr=nr*10+n[i]-48;
		for(unsigned long i=2;i<=nr;i++)
			s+=pow(i,i)%10;
		fprintf(g,"%lu\n",s%10);
	}
	fclose(f);
	fclose(g);
	return 0;
}