Cod sursa(job #452802)

Utilizator miculprogramatorA Cosmina - vechi miculprogramator Data 10 mai 2010 20:02:24
Problema Cifre Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <stdio.h>
#include <math.h>
#include <string.h>

char buffer[20];
long long a, b, aux;
int c, k, j, z, cf;
long long aux, i, nr, put1, put2;
long int l1, l2;
long long total;

long int fct (int x)
{
	if (x == 1)
		return 1;
	else
		return 9 * fct(x-1) + pow (10,x-1);
	
}

int main()
{
	FILE *f = fopen ("cifre.in","r");
	FILE *g = fopen ("cifre.out","w");
	
	fscanf (f,"%lld %lld %d %d", &a, &b, &k, &c);
	
	//printf ("%ld\n", fct(4));
	
	sprintf (buffer,"%lld", a);
	put1 = strlen (buffer);
	l1 = pow (10, put1) - 1;
	//printf ("put1 = %lld\n", put1);
	
	sprintf (buffer,"%lld", b);
	put2 = strlen (buffer);
	l2 = pow (10, put2-1) - 1;
	//printf ("put2 = %lld\n%ld %ld",put2, l1, l2);
	
	for (i=a; i<=l1; ++i)
	{
		aux = i;
		cf = 0;
		while (aux)
		{
			if (aux % 10 == c)
				cf ++;
			aux /= 10;
		}
		if (cf >= k)
			total ++;
	}
	
	for (i=l2+1; i<=b; ++i)
	{
		aux = i;
		cf = 0;
		while (aux)
		{
			if (aux % 10 == c)
				cf ++;
			aux /= 10;
		}
		if (cf >= k)
			total ++;
	}
	
	///printf ("\ntotal = %lld", total);
	
	//printf ("\ninterval = %ld", fct(put2-1) - fct(put1));
	total += (fct(put2-1) - fct(put1));
	
	fprintf (g,"%.4f", (float)total / (b-a+1));
	
	return 0;
}