Cod sursa(job #523621)

Utilizator SadmannCornigeanu Calin Sadmann Data 18 ianuarie 2011 17:49:21
Problema Cifra Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.96 kb
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
FILE *in,*out;
int T,j,q;
char N[100],rep[100];
int S;

int main()
{
	in=fopen("cifra.in","rt");
	out=fopen("cifra.out","wt");
	fscanf(in,"%d",&T);
	for(q=1;q<=T;q++)
	{
		S=0;
		fscanf(in,"%s",N);
		strcpy(rep,N);
		rep[strlen(rep)-1]=NULL;
		//nr_rep=atoi(rep);
		if(strlen(rep))
		{
			S=( (rep[strlen(rep)-1]-48) * ((1+6+5+6+9)%10)) %10;		
			if( (rep[strlen(rep)-1]-48)%2)
			//	S+= ( ((rep[strlen(rep)-1]-48)/2)* ((3+7+4+6+3+7+6+4)%10) )%10;
			//else
			{
				if(strlen(rep)==1 && rep[strlen(rep)-1]=='1')
					S+=(7+4+3+6)%10;
				else
				{
					if(strlen(rep)==1)
						S+= ( ((rep[strlen(rep)-1]-48)/2+1)*((3+4+3+6)%10) + ( (rep[strlen(rep)-1]-48)/2)*((7+6+7+4)%10) )%10;
					else
					{
						int m=((rep[strlen(rep)-2]-48)*10 + rep[strlen(rep)-1]-48)%10;
						S+= ( (m/2+1)*((3+4+3+6)%10) + (m/2)*((7+6+7+4)%10) )%10;
					}
				}
			}
		}
				
		j=N[strlen(N)-1]-48;
		if(j)
		{
			bool ok=true;
			if(strlen(rep) && ((rep[strlen(rep)-1]-48)%2) )
				ok=false;
			
			if(j==1)
				S++;
			if(j==2)
			{
				S++;
				if(ok)
					S+=4;
				else
					S+=6;
			}
			if(j==3)
			{
				S++;
				if(ok)
					S+=11;
				else
					S+=9;
			}
			if(j==4)
			{
				S++;
				S+=6;
				if(ok)
					S+=11;
				else
					S+=9;
			}
			if(j==5)
			{
				S++;
				S+=6;
				S+=5;
				if(ok)
					S+=11;
				else
					S+=9;
			}
			if(j==6)
			{
				S++;
				S+=6;
				S+=5;
				S+=6;
				if(ok)
					S+=11;
				else
					S+=9;
			}
			if(j==7)
			{
				S++;
				S+=6;
				S+=5;
				S+=6;
				if(ok)
					S+=14;
				else
					S+=16;
			}
			if(j==8)
			{
				S++;
				S+=6;
				S+=5;
				S+=6;
				if(ok)
					S+=20;
				else
					S+=20;
			}
			if(j==9)
			{
				S++;
				S+=6;
				S+=5;
				S+=6;
				S+=9;
				if(ok)
					S+=20;
				else
					S+=20;
			}
		}
		fprintf(out,"%d\n",S%10);
	}
	
	
	return 0;
}