Problem link-click here
Believe me this is after all a very easy problem. you can easily solve it using 2D array or by processing string.
just pre-calculate 1000 factorial then for any number of N and just print the Nth row. solution is given below:
#include<bits/stdc++.h>
#define m 1001
using namespace std;
vector<long long int> v[m];
void generatef()
{
v[0].push_back(1);
v[1].push_back(1);
for(int i=2;i<m;i++)
{
long long int r=0,s,t;
t=v[i-1].size();
for(int j=0;j<t;j++)
{
s=v[i-1][j]*i+r;
if(s>=10)
{
v[i].push_back(s%10);
r=s/10;
}
else
{
v[i].push_back(s);
r=0;
}
}
if(r>0)
{
while(r!=0)
{
v[i].push_back(r%10);
r=r/10;
}
}
}
}
int main()
{
generatef();
int n;
while(scanf("%d",&n)==1)
{
cout<<n<<"!"<<endl;
for(int i=v[n].size()-1;i>=0;i--)
{
cout<<v[n][i];
}
cout<<endl;
}
return 0;
}
Believe me this is after all a very easy problem. you can easily solve it using 2D array or by processing string.
just pre-calculate 1000 factorial then for any number of N and just print the Nth row. solution is given below:
#include<bits/stdc++.h>
#define m 1001
using namespace std;
vector<long long int> v[m];
void generatef()
{
v[0].push_back(1);
v[1].push_back(1);
for(int i=2;i<m;i++)
{
long long int r=0,s,t;
t=v[i-1].size();
for(int j=0;j<t;j++)
{
s=v[i-1][j]*i+r;
if(s>=10)
{
v[i].push_back(s%10);
r=s/10;
}
else
{
v[i].push_back(s);
r=0;
}
}
if(r>0)
{
while(r!=0)
{
v[i].push_back(r%10);
r=r/10;
}
}
}
}
int main()
{
generatef();
int n;
while(scanf("%d",&n)==1)
{
cout<<n<<"!"<<endl;
for(int i=v[n].size()-1;i>=0;i--)
{
cout<<v[n][i];
}
cout<<endl;
}
return 0;
}
No comments:
Post a Comment