Responsive Ads Here
Showing posts with label SPOJ. Show all posts
Showing posts with label SPOJ. Show all posts

Saturday, 13 May 2017

Factorial of large number (even 100!) using array

code:--

#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[500]={0};
a[0]=1;
int l=1;
int carry=0,num;
for(int i=2;i<=n;++i)
{
for(int j=0;j<l;++j)
{
num=a[j]*i+carry;
a[j]=num%10;
carry=num/10;
}
while(carry)
a[l]=carry%10,++l,carry/=10;
}
for(int i=l-1;i>=0;--i)
cout<<a[i];
cout<<'\n';
}
return 0;
}

Wednesday, 10 May 2017

CPTTRN2 - Character Patterns (Act 2)

problem id:-http://www.spoj.com/problems/CPTTRN2/

code:-

#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
if(i==0||i==n-1||j==0||j==m-1)
cout<<"*";
else
cout<<".";
}
cout<<'\n';
}
cout<<'\n';
}
return 0;
}

CPTTRN1 - Character Patterns (Act 1)

problem id:-http://www.spoj.com/problems/CPTTRN1/

code:--

#include <iostream>
using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
string str=".*";
int k=1;
for(int i=0;i<n;++i)
{
k=(i&1);
for(int j=0;j<m;++j)
{
k=k^1;
cout<<str[k];
}
cout<<'\n';
}
cout<<'\n';
}

return 0;
}

STRHH - Half of the half

problem id:-http://www.spoj.com/problems/STRHH/

code:-

#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
string str;
cin>>str;
int i=str.length()/2,j=0;
while(j<i)
{
cout<<str[j];
j+=2;
}
cout<<'\n';
}

return 0;
}

TEST - Life, the Universe, and Everything

problem id:-http://www.spoj.com/problems/TEST/

code:-

#include <iostream>
using namespace std;

int main() 
{
int n;
while(1)
{
cin>>n;
if(n==42)
break;
cout<<n<<'\n';
}
return 0;
}