Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Thursday, March 27, 2014

FERT21_0-Matches

Matches

below given code is for fert21_0 spoj or matches spoj.
you can make this code more faster using character array ans fast i/o;
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cfloat>
#include <map>
#include <fstream>
#include <sstream>
#include <bits/stdc++.h>
#include <climits>
using namespace std;
string arr[1009];
string multiply(string s,int a)
{
 int i,carry=0,j;
 string temp;
 char c;
 for(i=s.size() - 1;i>=0;i--)
 {
  j= s[i] - 48;
  c = (j*a + carry)%10 + 48;
  carry = (j*a + carry)/10;
  temp = c + temp;
 }
 if(carry!=0)
  c=carry+48,temp=c+temp;
 return temp;
}
void pre()
{
 arr[0] =49;
 for(int i=1;i<=1000;i++)
  arr[i] = multiply(arr[i-1],5);
}
int main()
{
 int t;
 scanf("%d",&t);
 pre();
 while(t--)
 {
  int n,l,p;
  ios_base::sync_with_stdio(false);
  scanf("%d",&n);
  if(n==1)
   cout<<"1"<<endl;
  else
  {
   cout<<"0.";
   p=arr[n-1].size();
   l= abs((n-1) - p);
   for(int i=0;i<l;i++)
    cout<<"0";
   cout<<arr[n-1]<<endl;
  }
 }
 return 0;
}

No comments:

Post a Comment

Your comment is valuable to us