← Back to topics
Topic

hi(proizvod help)

m
marcelo
hi i was trying to solve proizvod in C and it works only for 3 test cases. can someone check my noble code and tell me where is my mistake.
thanks

#include <stdio.h>
int last_two(long long int n);
int main(void)
{
int n;
long long int fak=1;
scanf("%d",&n);

while(n>0)
{
fak=fak*n;
fak=last_two(fak);
n--;
}

printf("%lld\n",fak);
return 0;
}
int last_two(long long int n)
{
int i=0;
int dig;
int a[3];
while(n>0&&i<3)
{
dig=n;
if(dig!=0)
a[i++]=dig;
n/=10;
}
return a[1]*10+a[0];
}

g
goran_f2
You are remembering only the last two non-zero digits but in some cases more digits are relevant, for example 325*4 = 1300, and the last non-zero digit is 3, but your program remembers only 2 and 5; 25*4=100, and it would print 1.
m
marcelo
if n is two-digit we remember the last two digit
if n is n-digit we remember the last n digits of fak
am i right?
g
goran_f2
my idea for the task is different, but i found out that a 5-digit memorization passes the tests
t
tgudlek
Yes, five digits are enough :)