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;
}

ALCATRAZ1 - SUM OF DIGITS

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

code:-

#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string str;
cin>>str;
long long sum=-48*str.length();
int i=0;
while(str[i]!='\0')
{
sum+=str[i];
++i;
}
cout<<sum<<'\n';
}
return 0;
}

SMPCPH1 - Substitution cipher

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

code:--

#include<iostream>
#include<string>  
#include<vector>
#include<unordered_map>
#include<algorithm>  
using namespace std;
int main(){

 int n;
 cin>>n;
 char arr[n+1];
 unordered_map<char,bool> Map;
 unordered_map<char,int> Qf;
 cin>>arr;
 for(int i=0;arr[i]!='\0';++i)
  Map[arr[i]]=true,Qf[arr[i]]=i;  
 int m;
 cin>>m;
 vector<string> str;
 cin.ignore(100,'\n');
 for(int i=0; i<m; i++)  
  {
string str1;
   getline(cin,str1);
   str.push_back(str1);
  }
 for(int i=0;i<m;++i)
  {
  int j=0;
  while(str[i][j])
  {
  if(Map[str[i][j]])
  str[i][j]=arr[(Qf[str[i][j]]+1)%n];
++j;
   }
   }
      vector<string>::iterator it;
       for(it = str.begin(); it != str.end(); ++it)
      cout << *it << "\n";    
return 0;
}         

Tuesday, 9 May 2017

codechef contest problem :Median of adjacent maximum numbers

problem id:-https://www.codechef.com/MAY17/problems/MXMEDIAN

code:--
//it works good for small test case but not for large test case
// wait till we upload the optimized code
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int num;
int max=0;
for(int i=0;i<n;++i)
{
cin>>num;
if(max<num)
max=num;
}
cout<<n-max<<'\n';
}
return 0;
}

Courses in an university

problem id:-https://www.codechef.com/MAY17/problems/UNICOURS

code:-

#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int num;
int max=0;
for(int i=0;i<n;++i)
{
cin>>num;
if(max<num)
max=num;
}
cout<<n-max<<'\n';
}
return 0;
}

Chef and his daily routine

problem id:-https://www.codechef.com/MAY17/problems/CHEFROUT
code:-

#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string str;
cin>>str;
int i=0;
int min=0,sec,flag=0;
string s[]={"yes","no"};
while(str[i]!='\0')
{
switch(str[i])
{
case 'C':
sec=1;
break;

case 'E':
sec=2;
break;

case 'S':
sec=3;
}
if(sec-min<0)
{
flag=1;
break;
}
min=sec;
++i;
}
cout<<s[flag]<<'\n';
}
return 0;
}