Problem Type : Number Theory
#include<stdio.h>
#include<string.h>
#define max 367
#define len 1000
int fact[max][len];
void factorial()
{
fact[0][0]=1;
fact[1][0]=1;
int i,j,k;
for(i=2;i<max;i++)
{
for(j=0;j<len;j++)
{
fact[i][j] = fact[i][j] + fact[i-1][j]*i;
if(fact[i][j]>9)
{
fact[i][j+1] = fact[i][j]/10;
fact[i][j] = fact[i][j]%10;
}
}
}
}
int main()
{
factorial();
int i,j,k,n,count,zero,one ,two ,three,four,five,six,seven,eight,nine;
while(scanf("%d",&n)==1)
{
if(n==0)
break;
zero=0;
one =0;
two =0;
three =0;
four=0;
five =0;
six =0;
seven =0;
eight=0;
nine =0;
for(i=len-1;i>=0;i--)
{
if(fact[n][i]!=0)
break;
}
printf("%d! --\n",n);
for(j=i;j>=0;j--)
{
if(fact[n][j]==0)
{
zero++;
}
else if(fact[n][j]==1)
{
one++;
}
else if(fact[n][j]==2)
{
two++;
}
else if(fact[n][j]==3)
{
three++;
}
else if(fact[n][j]==4)
{
four++;
}
else if(fact[n][j]==5)
{
five++;
}
else if(fact[n][j]==6)
{
six++;
}
else if(fact[n][j]==7)
{
seven++;
}
else if(fact[n][j]==8)
{
eight++;
}
else if(fact[n][j]==9)
{
nine++;
}
// printf("%d",fact[n][j]);
}
printf(" (0)%5d (1)%5d (2)%5d (3)%5d (4)%5d\n",zero,one,two,three,four);
printf(" (5)%5d (6)%5d (7)%5d (8)%5d (9)%5d\n",five,six,seven,eight,nine);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#define max 367
#define len 1000
int fact[max][len];
void factorial()
{
fact[0][0]=1;
fact[1][0]=1;
int i,j,k;
for(i=2;i<max;i++)
{
for(j=0;j<len;j++)
{
fact[i][j] = fact[i][j] + fact[i-1][j]*i;
if(fact[i][j]>9)
{
fact[i][j+1] = fact[i][j]/10;
fact[i][j] = fact[i][j]%10;
}
}
}
}
int main()
{
factorial();
int i,j,k,n,count,zero,one ,two ,three,four,five,six,seven,eight,nine;
while(scanf("%d",&n)==1)
{
if(n==0)
break;
zero=0;
one =0;
two =0;
three =0;
four=0;
five =0;
six =0;
seven =0;
eight=0;
nine =0;
for(i=len-1;i>=0;i--)
{
if(fact[n][i]!=0)
break;
}
printf("%d! --\n",n);
for(j=i;j>=0;j--)
{
if(fact[n][j]==0)
{
zero++;
}
else if(fact[n][j]==1)
{
one++;
}
else if(fact[n][j]==2)
{
two++;
}
else if(fact[n][j]==3)
{
three++;
}
else if(fact[n][j]==4)
{
four++;
}
else if(fact[n][j]==5)
{
five++;
}
else if(fact[n][j]==6)
{
six++;
}
else if(fact[n][j]==7)
{
seven++;
}
else if(fact[n][j]==8)
{
eight++;
}
else if(fact[n][j]==9)
{
nine++;
}
// printf("%d",fact[n][j]);
}
printf(" (0)%5d (1)%5d (2)%5d (3)%5d (4)%5d\n",zero,one,two,three,four);
printf(" (5)%5d (6)%5d (7)%5d (8)%5d (9)%5d\n",five,six,seven,eight,nine);
}
return 0;
}
No comments:
Post a Comment