Amdocs Code Mania 2013
Weighty Matters
Lalu owns a shop that sells weighing scales (see figure alongside) and weights. All the scales in his shop weigh the same – ten kilograms. They are of high quality and are well calibrated such that when equal weights are placed on both sides, they balance correctly. During his free time, Lalu imagines a grand tower made of many scales placed on one another as well as weights placed on some of them. He imagines the challenge it would pose to balance the entire tower! Your aim is to write a program to help Lalu balance any tower configuration by adding minimum weights on some scales. The balancing is always done by adding additional weights on lower scales and not by adding additional scales.
Souce Code...... of this . Weighty Matters is follows...
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int lindex,rindex;
int lwt,rwt;
int a,la,ra;
int flag;
};
void calculate(struct node *n,int index);
int main(int argc,char *argv[])
{
int wt,no,scale;
int put,position;
struct node *n;
int i=0,max=0;
char ch;
char input[30],output[30];
FILE *fp,*fp1;
strcpy(input,argv[1]);
strcpy(output,argv[2]);
fp = fopen(input,"r");
fp1 = fopen(output,"w+");
if(!fp || !fp1)
{
printf("File error");
return 1;
}
fscanf(fp,"%d",&no);
n = malloc(sizeof(struct node)*no);
max = no*2;
ch = fgetc(fp);
int flag = 0;
int j;
for(j=0;j<no;j++)
{
n[j].lindex=n[j].rindex=n[j].lwt=n[j].rwt=n[j].flag = -1;
n[j].a=n[j].la=n[j].ra=0;
}
while(i<max)
{
flag = 0;
fscanf(fp,"%d",&wt);
if((ch = fgetc(fp))==32)
{
fscanf(fp,"%d",&scale);
flag = 1;
}
else
fseek(fp,-1,SEEK_CUR);
if(i%2==0)
{
n[i/2].lwt = wt;
if(flag == 1)
n[i/2].lindex = scale;
}
else if(i%2 ==1)
{
n[i/2].rwt = wt;
if(flag == 1)
n[i/2].rindex = scale;
}
ch = fgetc(fp);
i++;
}
calculate(n,0);
for(j=0;j<no;j++)
{
fprintf(fp1,"%d ",j);
fprintf(fp1,"%d ",n[j].la);
fprintf(fp1,"%d\n",n[j].ra);
}
close(fp);
close(fp1);
return 0;
}
void calculate(struct node *n,int index)
{
if(n[index].flag == 1)
return;
if(n[index].lindex != -1)
{
calculate(n,n[index].lindex);
}
if(n[index].rindex != -1)
{
calculate(n,n[index].rindex);
}
if(n[index].flag == -1)
{
n[index].flag = 1;
int left,right;
if(n[index].lindex != -1)
left = n[index].lwt + n[n[index].lindex].a;
else
left = n[index].lwt;
if(n[index].rindex != -1)
right = n[index].rwt + n[n[index].rindex].a;
else
right = n[index].rwt;
if(left > right)
{
n[index].ra = left-right;
n[index].a = 10 + left + right + n[index].ra;
}
else if(left <= right)
{
n[index].la = right - left;
n[index].a = 10 + left + right + n[index].la;
}
}
}
this is Weighty Matters
Weighty Matters
Lalu owns a shop that sells weighing scales (see figure alongside) and weights. All the scales in his shop weigh the same – ten kilograms. They are of high quality and are well calibrated such that when equal weights are placed on both sides, they balance correctly. During his free time, Lalu imagines a grand tower made of many scales placed on one another as well as weights placed on some of them. He imagines the challenge it would pose to balance the entire tower! Your aim is to write a program to help Lalu balance any tower configuration by adding minimum weights on some scales. The balancing is always done by adding additional weights on lower scales and not by adding additional scales.
Souce Code...... of this . Weighty Matters is follows...
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int lindex,rindex;
int lwt,rwt;
int a,la,ra;
int flag;
};
void calculate(struct node *n,int index);
int main(int argc,char *argv[])
{
int wt,no,scale;
int put,position;
struct node *n;
int i=0,max=0;
char ch;
char input[30],output[30];
FILE *fp,*fp1;
strcpy(input,argv[1]);
strcpy(output,argv[2]);
fp = fopen(input,"r");
fp1 = fopen(output,"w+");
if(!fp || !fp1)
{
printf("File error");
return 1;
}
fscanf(fp,"%d",&no);
n = malloc(sizeof(struct node)*no);
max = no*2;
ch = fgetc(fp);
int flag = 0;
int j;
for(j=0;j<no;j++)
{
n[j].lindex=n[j].rindex=n[j].lwt=n[j].rwt=n[j].flag = -1;
n[j].a=n[j].la=n[j].ra=0;
}
while(i<max)
{
flag = 0;
fscanf(fp,"%d",&wt);
if((ch = fgetc(fp))==32)
{
fscanf(fp,"%d",&scale);
flag = 1;
}
else
fseek(fp,-1,SEEK_CUR);
if(i%2==0)
{
n[i/2].lwt = wt;
if(flag == 1)
n[i/2].lindex = scale;
}
else if(i%2 ==1)
{
n[i/2].rwt = wt;
if(flag == 1)
n[i/2].rindex = scale;
}
ch = fgetc(fp);
i++;
}
calculate(n,0);
for(j=0;j<no;j++)
{
fprintf(fp1,"%d ",j);
fprintf(fp1,"%d ",n[j].la);
fprintf(fp1,"%d\n",n[j].ra);
}
close(fp);
close(fp1);
return 0;
}
void calculate(struct node *n,int index)
{
if(n[index].flag == 1)
return;
if(n[index].lindex != -1)
{
calculate(n,n[index].lindex);
}
if(n[index].rindex != -1)
{
calculate(n,n[index].rindex);
}
if(n[index].flag == -1)
{
n[index].flag = 1;
int left,right;
if(n[index].lindex != -1)
left = n[index].lwt + n[n[index].lindex].a;
else
left = n[index].lwt;
if(n[index].rindex != -1)
right = n[index].rwt + n[n[index].rindex].a;
else
right = n[index].rwt;
if(left > right)
{
n[index].ra = left-right;
n[index].a = 10 + left + right + n[index].ra;
}
else if(left <= right)
{
n[index].la = right - left;
n[index].a = 10 + left + right + n[index].la;
}
}
}
this is Weighty Matters
No comments:
Post a Comment