C,C++/JAVA/BASH/ASM ARENA

वह प्रदीप जो दीख रहा है झिलमिल दूर नही है थक कर बैठ गये क्या भाई मन्जिल दूर नही है चिन्गारी बन गयी लहू की बून्द गिरी जो पग से चमक रहे पीछे मुड देखो चरण-चिनह जगमग से बाकी होश तभी तक, जब तक जलता तूर नही है थक कर बैठ गये क्या भाई मन्जिल दूर नही है अपनी हड्डी की मशाल से हृदय चीरते तम का, सारी रात चले तुम दुख झेलते कुलिश का। एक खेय है शेष, किसी विध पार उसे कर जाओ; वह देखो, उस पार चमकता है मन्दिर प्रियतम का। आकर इतना पास फिरे, वह सच्चा शूर नहीं है; थककर बैठ गये क्या भाई! मंज़िल दूर नहीं है। दिशा दीप्त हो उठी प्राप्त कर पुण्य-प्रकाश तुम्हारा, लिखा जा चुका अनल-अक्षरों में इतिहास तुम्हारा। जिस मिट्टी ने लहू पिया, वह फूल खिलाएगी ही, अम्बर पर घन बन छाएगा ही उच्छ्वास तुम्हारा। और अधिक ले जाँच, देवता इतन क्रूर नहीं है। थककर बैठ गये क्या भाई! मंज़िल दूर नहीं है।

TJU 3181. Stock Exchange June 10, 2010

Filed under: C,C++ Programs,TJU — whoami @ 12:43
Tags: ,

TJU 3181. Stock Exchange
ACCEPTED

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
  char s[100],s1[100],s2[100];
  int i,j,k;
  
  vector<double>price;
 
  vector<string>agent,buysell;
  while(1){
   int n;
   scanf("%d%s",&n,s);
   string str=s,str1;
   double d;
   if(n==0&&str=="END") break;
     agent.clear();buysell.clear();price.clear();
      for(int i=0;i<n;i++){
        scanf("%s%s%lf",s1,s2,&d);
        str1=s1;agent.push_back(str1);
        str1=s2;buysell.push_back(str1);
        price.push_back(d);
        }
       vector< vector<string> > v;
       vector<string> tmp;
       vector< vector<string> >::iterator x;
       //for(x=v.begin();x<v.begin();x++)
         // (*x).clear();
       v.clear(); 
       tmp.clear();

       vector<string>::iterator it1,it2,it3,it4;
       it3=buysell.begin();it4=buysell.begin();
       vector<double>::iterator it5,it6;
       it5=price.begin();it6=price.begin();
      for(it1=agent.begin();it1<agent.end();it1++,it3++,it5++){
              //cout<<"yes";
              tmp.clear();
              int flag=0;
              for(it2=agent.begin(),it4=buysell.begin(),it6=price.begin();it2<agent.end();it2++,it4++,it6++){
                   if(it1!=it2){
                          if(*it3!=*it4){
                              if(*it3=="buy"){
                                      if(*it6<=*it5){
                                        tmp.push_back(*it2);
                                        flag=1;
                                        }
                                   }
                               else if(*it3=="sell"){
                                           if(*it5<=*it6){
                                              tmp.push_back(*it2);
                                              flag=1;
                                              }
                                   }
                               }
                          }
                    }
                 if(flag==0) {tmp.push_back("NO-ONE");
                              v.push_back(tmp);
                             }
                 else
                           v.push_back(tmp);
               }  
                    
         printf("%s\n",s); 
        vector<string>::iterator it=agent.begin();
         vector< vector<string> >::iterator ii;
         vector<string>::iterator ii2;
         ii=v.begin();                                   
         for(int i=0;i<n;i++,it++,ii++){
           cout<<*it<<":";
           for(ii2=(*ii).begin();ii2<(*ii).end();ii2++){
              cout<<" "<<(*ii2);
              }
            cout<<endl;
           }
       
       }

return 0;
}


Advertisements
 

TJU 3249. Happy Coins June 9, 2010

Filed under: C,C++ Programs,Pattern finding program,TJU — whoami @ 20:42
Tags: , ,

TJU 3249. Happy Coins
ACCEPTED

Earlier i was trying to code what was written in the problem statement. Then i found that its hard to decide programatically. So i took paper & pen started finding the repeating pattern.

I found this pattern
if we take
lxh->0
hhb->1
Then
=================
when n=2
input1 input2 Ans
0 0 1(hhb)
0 1 0(lxh)
1 0 0(lxh)
1 1 1(hhb)
=====================
when n=3
input1 input2 input3 Ans
0 0 0 0(lxh)
0 0 1 1(hhb)
0 1 0 1(hhb)
0 1 1 0(lxh)
1 0 0 1(hbb)
1 0 1 0(lxh)
1 1 0 0(lxh)
1 1 1 1(hhb)
==================
when n=4
inp1 inp2 inp3 inp4 ans
0 0 0 0 1
0 0 0 1 0
0 0 1 0 0
0 0 1 1 1
0 1 0 0 0
0 1 0 1 1
0 1 1 0 0
0 1 1 1 0
1 0 0 0 0
1 0 0 1 1
1 0 1 0 1
1 0 1 1 0
1 1 0 0 1
1 1 0 1 0
1 1 1 0 0
1 1 1 1 1
====================

my AC code

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;

int main()
{
   int cases;
   cin>>cases;
   int n,turn,zero,one;
   string s;
   
   while(cases--){
        s="";    
       cin>>n;
       zero=0;one=0;
       int tmp=n;   
       while(tmp--){
         cin>>s;
         if(s=="lxh") ++zero;
         else if(s=="hhb") ++one;

         }
       if(n%2==0){
              if(one%2==0) printf("hhb\n");
              else printf("lxh\n");
           }
       else{
              if(one%2!=0) printf("hhb\n");
              else printf("lxh\n");
           }
      }

return 0;
}



 

TJU 3513. Triangle

Filed under: C,C++ Programs,Precision error,TJU — whoami @ 13:23
Tags: , ,

TJU 3513. Triangle
This problem require effort to tackle Precision errors

ACCEPTED

My AC code

//precision error upto 5 digits after decimal taken 
//ACCEPTED
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
  long double PI=3.14159265/2.000000000,PI2=3.14159265;
  PI=(int)(PI*100000);PI2=(int)(PI2*100000);
  PI/=100000;PI2/=100000;
  //cout<<PI<<endl;
  int x1,y1,x2,y2,x3,y3;
  int cases,k=0;
  cin>>cases;
  while(cases--){
     cin>>x1>>y1>>x2>>y2>>x3>>y3;
     long double a,b,c;
     a=sqrt(pow((x1-x2),2.00000)+pow((y1-y2),2.00000));
     b=sqrt(pow((x2-x3),2.00000)+pow((y2-y3),2.00000));
     c=sqrt(pow((x3-x1),2.00000)+pow((y3-y1),2.00000));
     long double A,B,C;
     A=acos((b*b+c*c-a*a)/(2.00000*b*c));
     B=acos((a*a+c*c-b*b)/(2.00000*a*c));
     C=acos((b*b+a*a-c*c)/(2.00000*b*a));
     A=(int)(A*100000); B=(int)(B*100000); C=(int)(C*100000);
     A/=100000;B/=100000;C/=100000;
    //cout<<A<<" "<<B<<" "<<C<<endl;
   printf("Case #%d: ",++k);
if(x1==x2&&y1==y2||x2==x3&&y2==y3||x3==x1&&y3==y1||A==0.00000||B==0.00000||C==0.00000||A==PI2||B==PI2||C==PI2) printf("not a triangle\n");
else if(A<PI&&B<PI&&C<PI){
         
         if(a==b&&b!=c||b==c&&b!=a||c==a&&a!=b) printf("isosceles acute triangle\n");
         else if(a!=b&&b!=c&&c!=a) printf("scalene acute triangle\n");                          
         }
else if(A==PI||B==PI||C==PI){
         if(a==b&&b!=c||b==c&&b!=a||c==a&&a!=b) printf("isosceles right triangle\n");
        else if(a!=b&&b!=c&&c!=a) printf("scalene right triangle\n");                          
         }
else if(A>PI||B>PI||C>PI){
        if(a==b&&b!=c||b==c&&b!=a||c==a&&a!=b) printf("isosceles obtuse triangle\n");
        else if(a!=b&&b!=c&&c!=a) printf("scalene obtuse triangle\n");                          
         }
       
     
}
return 0;
}

 

TJU 1701. Stamps June 4, 2010

Filed under: C,C++ Programs,TJU — whoami @ 17:20
Tags: ,

TJU 1701. Stamps
AC

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
  int cases;
  cin>>cases;
  int n,a,arr[1009],i,j,k=0;
  while(cases--){
      
      cin>>n>>a;
      for(i=0;i<a;i++)
          cin>>arr[i];

      sort(arr,arr+a);
      int total=0;
         i=0;
       while(total<n){
         if(i==a) break;
         total+=arr[a-1-i];
         i++;
         }

      down:
        printf("Scenario #%d:\n",++k);
        if(total<n)
             printf("impossible\n\n");
        else
             printf("%d\n\n",i);
     }


return 0;
}


 

TJU 2199. A+B Problem May 30, 2010

Filed under: C,C++ Programs,SEARCHING,SORTING,TJU — whoami @ 23:30
Tags: ,

TJU 2199. A+B Problem
AC
This problem requires Sorting and Searching(Binary) approch

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;


int main()
{
 int i,j,k;
 int a[1009],n;
 int tmp,diff;
 int low,mid,high,flag,num;
 while(1){
    scanf("%d",&n);
    if(n==0) break;
    for(i=0;i<n;i++)
       scanf("%d",&a[i]);
    
    sort(a,a+n);//sort
    
    for(i=n-1;i>=0;i--){
       tmp=a[i];
       for(j=i-1;j>=0;j--){
         diff=tmp-a[j];
         high=j-1;low=0;
         flag=0;
         mid=(low+high)/2;
         while(low<=high){//binary search
            mid=(low+high)/2;
            
            if(a[mid]==diff){ flag=1;num=tmp;goto down;}
            else if(diff<a[mid]){high=mid-1;}
            else if(diff>a[mid]){low=mid+1;}
         }
       }
      
    }
     down:
          if(flag==1) printf("%d\n",tmp);
          else printf("-1\n");
   }    

return 0;
}


 

TJU 1547. To and Fro

Filed under: C,C++ Programs,TJU — whoami @ 22:39
Tags: ,

After a long time , playing TJU contest
TJU 1547. To and Fro
AC

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int main()
{
 int i,j,k;
 int n;
 char s[205];
 char str[100][21],tmp[100][21];
 while(1){

 scanf("%d",&n);
 if(n==0) break;
 scanf("%s",s);
 k=0;j=0;
 for(i=0;s[i]!='\0';){
  tmp[j][k]=s[i];
  i++;
  if(i%n==0)
     j=j+1;
    
  if(j%2==0&&i%n!=0) k=k+1;
  else if(j%2!=0&&i%n!=0) k=k-1;
  
  }
     
  for(i=0;i<n;i++){
      for(k=0;k<j;k++){
          printf("%c",tmp[k][i]);
      }
   }
    printf("\n");
      
 }


return 0;
}



 

TJU 1365. Hard to Believe, but True! February 25, 2010

Filed under: C,C++ Programs,TJU — whoami @ 20:52
Tags: ,

TJU 1365. Hard to Believe, but True!

–AC–

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
  char sa[10],sb[10],sc[10];
  int i,j,k,la,lb,lc,a,b,sum;
  char c;
  while(1){
  i=0;
  while(1){ c=getchar(); if(c=='+') break;sa[i++]=c;}
  sa[i]='\0';
  i=0;
  while(1){ c=getchar();if(c=='=') break;sb[i++]=c;}
  sb[i]='\0';
  i=0;
  while(1){ c=getchar();if(c=='\n') break;sc[i++]=c;}
  sc[i]='\0';
  la=strlen(sa);lb=strlen(sb);lc=strlen(sc);
  for(i=0;i<la/2;i++){
    c=sa[i];sa[i]=sa[la-1-i];sa[la-1-i]=c;
   }
   for(i=0;i<lb/2;i++){
    c=sb[i];sb[i]=sb[lb-1-i];sb[lb-1-i]=c;
   }
   for(i=0;i<lc/2;i++){
    c=sc[i];sc[i]=sc[lc-1-i];sc[lc-1-i]=c;
   }
   a=atoi(sa);b=atoi(sb);sum=atoi(sc);
   if(a==0&&b==0&&sum==0) {printf("True\n");break;}
   if((a+b)==sum) printf("True\n");
   else printf("False\n");
   }

return 0;
}