Wednesday, February 26, 2014

Sphere Online Judge Problem Code:ADDREV

Problem Source: http://www.spoj.com/problems/ADDREV/
--------------------
The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard because the basic sense of the play must be kept intact, although all the things change to their opposites. For example the numbers: if any number appears in the tragedy, it must be converted to its reversed form before being accepted into the comedy play.
Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros.
ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).

Input

The input consists of N cases (equal to about 10000). The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.

Output

For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.

Example

Sample input: 

3
24 1
4358 754
305 794

Sample output:

34
1998
1
--------------------


Solution CODE
#include<stdio.h>
#include<stdlib.h>
int main ()
{
int i,n;
scanf("%d", &n);
int *a = (int*)malloc(n*sizeof(int));
int *b = (int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
scanf("%d",&b[i]);
}

int *c = (int*)malloc(n*sizeof(int));
int *d = (int*)malloc(n*sizeof(int));
int *e = (int*)malloc(n*sizeof(int));
int *f = (int*)malloc(n*sizeof(int));

for(i=0;i<n;i++)
{ c[i]=0;
while(a[i]>0)
{ c[i]=c[i]*10 + a[i]%10 ;
a[i]=a[i]/10 ;
}
}

for(i=0;i<n;i++)
{  
d[i]=0;
while(b[i]>0)
     
d[i]=d[i]*10 + b[i]%10 ;
   b[i]=b[i]/10 ;
  }
}
for(i=0;i<n;i++)
e[i]=c[i]+d[i];

for(i=0;i<n;i++)
{  
f[i]=0;
while(e[i]>0)
     
f[i]=f[i]*10 + e[i]%10 ;
   e[i]=e[i]/10 ;
  }
}

for(i=0;i<n;i++)
{
printf("%d",f[i]);
printf("\n");
}
return 0;
}

Continue Reading →

Saturday, February 22, 2014

Algorithm:

CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
 int a[50],n,i,j,loc;
 clrscr();
 printf("Enter the number of elements:\n");
 scanf("%d",&n);
 printf("Enter the elements:\n");
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 for(i=1;i<n;i++)
 {
  loc=a[i];
  for(j=i-1;j>=0 && loc<a[j];j--)
  {
   a[j+1]=a[j];
  }
  a[j+1]=loc;
 }
 printf("Sorted Array is:\n");
 for(i=0;i<n;i++)
 {
  printf("%d\n",a[i]);
 }
 getch();
}

Continue Reading →

Friday, February 21, 2014

CODE:

#include<stdio.h>
#include<conio.h>
struct stack
{
 int info;
 struct stack *next;
};
struct stack *top;
void push(int element);
int pop();
int peek();
void main()
{
 int info,a;
 int x;
 int y;
 char ch;
 clrscr();
 top=NULL;
 do
 {
  printf("Enter your choice\n1:Push\n2:Pop\n");
  printf("3:Access the top element\n");
  scanf("%d",&a);
  if(a==1)
  {
   printf("Enter the element\n");
   scanf("%d",&info);
   push(info);
  }
  else if(a==2)
  {
   x=pop();
   if(x==0)
   {
    printf("Stack is empty\n");
   }
   else
   {
    printf("Element deleted: %d\n",x);
   }
  }
  else if(a==3)
  { 
   y=peek();
   printf("Top element: %d\n",y);
  }
 printf("Do you want to continue? y/n\n");
 scanf(" %c",&ch);
 clrscr();
 }
 while(ch=='y');
}
void push(int element)
{
 struct stack *newptr;
 newptr=(struct stack *)malloc(sizeof(struct stack));
 if(newptr==NULL)
 {
  printf("No memory available\n");
  return;
 }
 else
 {
  newptr->info=element;
  newptr->next=top;
  top=newptr;
 }
}
int pop()
{
 int value;
 struct stack *ptr;
 if(top==NULL)
 {
   return 0;
 }
 else
 {
  value=top->info;
  ptr=top;
  top=top->next;
  free(ptr);
  return value;
 }
}
int peek()
{
 return (top->info);
}

Continue Reading →

Wednesday, February 12, 2014

CODE:
#include<stdio.h>
#include<conio.h>
struct node
{
struct node *prev;
int info;
struct node *next;
};
struct node *start,*tail,*start2,*tail2,*tail3,*start3;
struct node *create_new_node(int i);
void insertend(struct node *n);
void add_before(struct node *p);
void add_after(struct node *o);
void del_before(int element);
void del_after(int ele);
void del(int elem);
void insertbeg(struct node *m);
void split(struct node *head);
void display(struct node *x);
int count(struct node *c);
void main()
{
int info,a,c;
struct node *np;
char ch;
start=NULL;
tail=NULL;
clrscr();
do
{
printf("Enter the choice of Operation to Perform :\n");
printf("Enter 1 to insert at begining\n");
printf("Enter 2 to insert before a given element\n");
printf("Enter 3 to after a given element\n");
printf("Enter 4 to insert at end\n");
printf("Enter 5 to delete after a given element\n");
printf("Enter 6 to delete before a given element\n");
printf("Enter 7 to delete a paarticular element\n");
printf("Enter 8 to split the current linked list into 2 new\n");
scanf("%d",&a);
if(a==1)
{
printf("Enter the new element\n");
scanf("%d",&info);
np=create_new_node(info);
insertbeg(np);
}
else if(a==2)
{
printf("Enter the new element\n");
scanf("%d",&info);
np=create_new_node(info);
add_before(np);
}
else if(a==3)
{
printf("Enter the new element\n");
scanf("%d",&info);
np=create_new_node(info);
add_after(np);
}
else if(a==4)
{
printf("Enter the new element\n");
scanf("%d",&info);
np=create_new_node(info);
insertend(np);
}
else if(a==5)
{
printf("Enter element whose next no. is to be deleted\n");
scanf("%d",&info);
del_after(info);
}
else if(a==6)
{
printf("Enter element whose previous no. is to be deleted\n");
scanf("%d",&info);
del_before(info);
}
else if(a==7)
{
printf("Enter the element to be deleted\n");
scanf("%d",&info);
del(info);
}
else if(a==8)
{
split(start);
printf("The two linked lists are\n");
printf("Even:\t");
display(start2);
printf("Odd:\t");
display(start3);
}
display(start);
c=count(start);
printf("The number of nodes are %d\n",c);
printf("\nDo you want to proceed? y/n\n");
scanf(" %c",&ch);
}
while(ch=='y');
getch();
}
/*CREATION OF NEW NODE*/
struct node *create_new_node(int i)
{
struct node *newptr;
newptr=(struct node *)malloc(sizeof(struct node));
newptr->info=i;
newptr->next=NULL;
newptr->prev=NULL;
return newptr;
}
/*INSERTION AT BEGINING*/
void insertbeg(struct node *m)
{
if(start==NULL)
{
start=m;
tail=m;
}
else
{
m->prev=NULL;
m->next=start;
start->prev=m;
start=m;
}
}
/*INSERTION AT END*/
void insertend(struct node *n)
{
if(start==NULL)
{
start=n;
tail=n;
}
else
{
n->next=NULL;
n->prev=tail;
tail->next=n;
tail=n;
}
/*INSERTION BEFORE A GIVEN ELEMENT*/
void add_before(struct node *p)
{
int value;
struct node *temp,*tmp;
temp=start;
printf("Enter no. before which new no. has to be inserted:\n");
scanf("%d",&value);
while(temp!=NULL)
{
if(temp->info==value)
{
tmp=temp;
}
temp=temp->next;
}
if(tmp->prev==NULL)
{
p->prev=NULL;
tmp->prev=p;
p->next=start;
start=p;
}
else
{
p->next=tmp;
p->prev=tmp->prev;
tmp->prev->next=p;
tmp->prev=p;
}
}
/*INSERTION AFTER A GIVEN ELEMENT*/
void add_after(struct node *o)
{
int value;
struct node *temp;
temp=start;
printf("Enter no. after which new no. has to be inserted:\n");
scanf("%d",&value);
while(temp!=NULL)
{
if(temp->info==value)
{
o->prev=temp;
o->next=temp->next;
temp->next->prev=o;
temp->next=o;
}
temp=temp->next;
}
}
/*DELETION BEFORE A GIVEN ELEMENT*/
void del_before(int element)
{
struct node *temp,*loc,*tmp;
temp=start;
tmp=start;
while(temp!=NULL)
{
if(temp->info==element)
{
loc=temp;
}
temp=temp->next;
}
if(loc->prev->prev==NULL)
{
tmp=loc->prev;
loc->prev=NULL;
start=loc;
free(tmp);
}
else
{
tmp=loc->prev;
loc->prev=tmp->prev;
tmp->prev->next=loc;
free(tmp);
}
}
/*DELETION AFTER A GIVEN ELEMENT*/
void del_after(int ele)
{
struct node *temp,*loc,*tmp;
temp=start;
tmp=start;
while(temp!=NULL)
{
if(temp->info==ele)
{
loc=temp;
}
temp=temp->next;
}
if(loc->next->next==NULL)
{
tmp=loc->next;
loc->next=NULL;
tail=loc;
free(tmp);
}
else
{
tmp=loc->next;
loc->next=tmp->next;
tmp->next->prev=loc;
free(tmp);
}
}
/*DELETE*/
void del(int elem)
{
struct node *temp,*ptr,*loc;
temp=start;
while(temp!=NULL)
{
if(temp->info==elem)
{
loc=temp;
}
temp=temp->next;
}
if(loc->prev==NULL)
{
ptr=loc;
loc->next->prev=NULL;
start=loc->next;
free(ptr);
}
else if(loc->next==NULL)
{
ptr=loc;
loc->prev->next=NULL;
tail=loc->prev;
free(ptr);
}
else
{
ptr=loc;
loc->prev->next=ptr->next;
loc->next->prev=ptr->prev;
free(ptr);
}}
void split(struct node *head)
{
struct node *temp,*newptr,*nptr;
start2=NULL;
start3=NULL;
tail2=NULL;
tail3=NULL;
temp=head;
while(temp!=NULL)
{
if((temp->info)%2==0)
{
newptr=(struct node *)malloc(sizeof(struct node));
newptr->info=temp->info;
newptr->next=NULL;
newptr->prev=NULL;
if(start2==NULL)
{
start2=newptr;
tail2=newptr;
}
else
{
newptr->next=NULL;
newptr->prev=tail2;
tail2->next=newptr;
tail2=newptr;
}
}
else
{
nptr=(struct node *)malloc(sizeof(struct node));
nptr->info=temp->info;
nptr->next=NULL;
nptr->prev=NULL;
if(start3==NULL)
{
start3=nptr;
tail3=nptr;
}
else
{
nptr->next=NULL;
nptr->prev=tail3;
tail3->next=nptr;
tail3=nptr;
}
}
temp=temp->next;
}
}
/*DISPLAY*/
void display(struct node *x)
{
printf("\nCurrent linked list is:\t");
do
{
printf("%d\t",x->info);
x=x->next;
}
while(x!=NULL);
printf("\n");
}
/*COUNT THE NO. OF NODES*/
int count(struct node *c)
{
int co=0;
do
{
co++;
c=c->next;
}
while(c!=NULL);
return co;
}

Continue Reading →

Friday, February 07, 2014

CODE:
#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *link;
};
struct node *start,*newptr;
struct node *create_new_node(int i);
void insertend(struct node *n);
void insertmid(struct node *o);
void insertbeg(struct node *m);
void reverse(int c);
void del(int d);
void display(struct node *x);
int count(struct node *c);
void main()
{
int info,a,c;
struct node *np;
char ch;
start=NULL;
clrscr();
do
{
printf("ENTER YOUR CHOICE:\n");
printf("Enter 1 to insert at begining\n");
printf("Enter 2 to insert after a given element\n");
printf("Enter 3 to insert at end\n");
printf("Enter 4 to delete a particular element\n");
printf("Enter 5 to reverse the current linked list\n");
scanf("%d",&a);
if(a==1)
{
printf("Enter the element\n");
scanf("%d",&info);
np=create_new_node(info);
insertbeg(np);
}
else if(a==2)
{
printf("Enter the element\n");
scanf("%d",&info);
np=create_new_node(info);
insertmid(np);
}
else if(a==3)
{
printf("Enter the element\n");
scanf("%d",&info);
np=create_new_node(info);
insertend(np);
}
else if(a==4)
{
printf("Enter the element\n");
scanf("%d",&info);
np=create_new_node(info);
del(info);
}
else if(a==5)
{
reverse(c);
}
display(start);
c=count(start);
printf("The number of nodes are %d\n",c);
printf("\nDo you want to proceed? y/n\n");
scanf(" %c",&ch);
}
while(ch=='y');
getch();
}
/*CREATION OF NEW NODE*/
struct node *create_new_node(int i)
{
newptr=(struct node *)malloc(sizeof(struct node));
newptr->info=i;
newptr->link=NULL;
return newptr;
}
/*INSERTION AT BEGINING*/
void insertbeg(struct node *m)
{
if(start==NULL)
{
start=m;
}
else
{
m->link=start;
start=m;
}
}
/*INSERTION AT END*/
void insertend(struct node *n)
{
struct node *temp;
if(start==NULL)
{
start=n;
}
else
{
temp=start;
while(temp->link!=NULL)
temp=temp->link;
temp->link=n;
}
}
/*INSERTION AFTER A GIVEN ELEMENT*/
void insertmid(struct node *o)
{
int value;
struct node *temp;
temp=start;
printf("Enter the number after which ");
printf("new element has to be inserted:\n");
scanf("%d",&value);
while(temp!=NULL)
{
if(temp->info==value)
{
o->link=temp->link;
temp->link=o;
}
temp=temp->link;
}}
/*DELETE*/
void del(int d)
{
struct node *temp,*ptr;
temp=start;
while(temp!=NULL)
{
if(temp->link->info==d)
{
ptr=temp->link;
temp->link=ptr->link;
free(ptr);
}
temp=temp->link;
}
}
/*REVERSE*/
void reverse(int c)
{
struct node *p1,*p2,*p3;
if(start->link==NULL)
return;
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}
/*DISPLAY*/
void display(struct node *x)
{
printf("\nCurrent linked list is:\t");
do
{
printf("%d\t",x->info);
x=x->link;
}
while(x!=NULL);
printf("\n");
}
/*COUNT THE NO. OF NODES*/
int count(struct node *c)
{
int co=0;
do
{
co++;
c=c->link;
}
while(c!=NULL);
return co;
}

Continue Reading →

Saturday, February 01, 2014

Spoj Problem: http://www.spoj.com/problems/TEST/

Code in C:
#include<stdio.h>
int main(){
int n;
while(1){
scanf("%d", &n);
if(n==42) break;
printf("%d\n", n); }
return 0;
}


Code in C++:
#include<iostream>
using namespace std;
int main()
{
int n;
while(1)
{
cin>>n;
if(n==42)
break;
cout<<n<<endl;
}
return 0;
}

Continue Reading →

Saturday, January 04, 2014

4/1/2014

This is my first post this year, so first of all Happy New Year Everyone! This post is result of some of my recent experiences of the taste of life!

There is a popular saying in English, "Big birds of storm" , whenever there is intense storm, the birds with smaller wings are caught up in it, & the birds with larger wings (large birds) fly firm & higher.
This is pretty analogue to life also! when the situation is good & fair it is usual to be happy & jolly, but when it's not then survival depends on our wings!, i.e. our perspective towards the situation & that's what tests us on the Litmus test of Life.

.... to be continued.

Continue Reading →