Page 2 of 2

Posted: Thu May 20, 2004 2:22 am
by UFP2161
What's the output to:
new Shade01 0.50 3.79
new shade01 1.47 6.98
new Shade02 0.63 4.29
report
*
Basically, I do:
If "new", put values into a C++ map.
If "delete", decrease current inventory value and profit, and remove from map.
If "buy", update values in map, and increase inventory value.
If "sell", update values in map, decrease inventory value, and add/subtract from profits.
If "report", output and reset profit to 0.

I use ints for everything except profit and inventory which are long longs, because 100 items x 10000 on hand x $100.00 = 1 billion dollars, or 100 billion cents, which can only fit in long long.

Posted: Mon Sep 06, 2004 2:42 pm
by Dominik Michniewski
Finally I got Accepted :-)

Problem is rather easy , I think now ;-)
To everyone who can't solved this problem: think about your reports - do you correct output all values, including profit ? :-)

Best regards
DM

Problem 330 - Inventory Maintenance

Posted: Tue Jun 21, 2005 11:26 pm
by N|N0
Hello!
I've recently been working on this problem.
I can't get rid of the P.E. thing.

How many newlines follow the last report?
One, two, three, none?
Does anyone know?

Posted: Sat Oct 08, 2005 5:33 am
by Jan
'Profit since last report can be negative. But profit is always positive.' - You should consider this.

Posted: Tue Jan 31, 2006 2:46 pm
by mamun
This program looks easy but so less percentage of acceptance!
I'm getting WA. Can somebody post some I/O plz? Can the item names contain space? I'm using int for quantity and double for rests. What should be the output of this?

Code: Select all

new c 0.99 1.01
new a 0.99 1.01
new b 0.99 1.01
new C 0.99 1.01
new A 0.99 1.01
new B 0.99 1.01
report
buy C 1
buy A 1
buy B 1
buy c 1
buy a 1
buy b 1
report
sell c 1
report
delete A
delete B
delete C
report
*

Posted: Tue Jan 31, 2006 6:55 pm
by Jan
My Accepted code returned...

Output:

Code: Select all

                  INVENTORY REPORT
Item Name     Buy At      Sell At      On Hand        Value
---------     ------      -------      -------        -----
A               0.99         1.01            0         0.00
B               0.99         1.01            0         0.00
C               0.99         1.01            0         0.00
a               0.99         1.01            0         0.00
b               0.99         1.01            0         0.00
c               0.99         1.01            0         0.00
------------------------
Total value of inventory                               0.00
Profit since last report                               0.00

                  INVENTORY REPORT
Item Name     Buy At      Sell At      On Hand        Value
---------     ------      -------      -------        -----
A               0.99         1.01            1         0.99
B               0.99         1.01            1         0.99
C               0.99         1.01            1         0.99
a               0.99         1.01            1         0.99
b               0.99         1.01            1         0.99
c               0.99         1.01            1         0.99
------------------------
Total value of inventory                               5.94
Profit since last report                               0.00

                  INVENTORY REPORT
Item Name     Buy At      Sell At      On Hand        Value
---------     ------      -------      -------        -----
A               0.99         1.01            1         0.99
B               0.99         1.01            1         0.99
C               0.99         1.01            1         0.99
a               0.99         1.01            1         0.99
b               0.99         1.01            1         0.99
c               0.99         1.01            0         0.00
------------------------
Total value of inventory                               4.95
Profit since last report                               0.02

                  INVENTORY REPORT
Item Name     Buy At      Sell At      On Hand        Value
---------     ------      -------      -------        -----
a               0.99         1.01            1         0.99
b               0.99         1.01            1         0.99
c               0.99         1.01            0         0.00
------------------------
Total value of inventory                               1.98
Profit since last report                              -2.97
I used scanf to read the item name. So, there is no space in the item name. But I used int for all the computations. Firstly I read the numbers as strings, then converted all into integers multiplying by a common number. After all the computations, I convert them to double and print them as the problem states.

Its too tough to generate tricky inputs for this problem. And I m too lazy. Sorry for that. Congratulations for becoming a 'GURU'.

Posted: Tue Jan 31, 2006 10:22 pm
by mamun
Thank you Jan.

As said in the problem
Note that this activity line doesn't actually result in a change in the inventory
so I was printing a table without any item row for the first report. Fixing this wasn't enough. Doing string->int->double conversion was necessary to get it accepted. :)
Jan wrote:Congratulations for becoming a 'GURU'.
Thank you. To say the truth I'm v-e-r-y far from to be a Guru. A great helper identifies me correctly. :)

Posted: Tue Feb 13, 2007 6:17 am
by razor_blue
Why can I have Runtime Error (Signal 11) for my code???

Code: Select all

/*
	Inventory Maintenance
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct data node;
struct data
{
	int jum;
	char nama[12];
	double hbeli,hjual,nilai;
	node *prev,*next;
};
int main()
{
	int jum,first=1;
	char cmd[500],*p;
	double total,laba=0;
	node *head,*tail,*curr,*temp;
	while(gets(cmd))
	{
		if(!strcmp(cmd,"*")) break;
		p=strtok(cmd," ");
		if(!strcmp(p,"new"))
		{
			curr=(node *)malloc(sizeof(node));
			p=strtok(NULL," ");  strcpy(curr->nama,p);
			p=strtok(NULL," ");  curr->hbeli=atof(p);
			p=strtok(NULL,"\n"); curr->hjual=atof(p);
			curr->jum=0;			
			curr->next=curr->prev=NULL;
			if(!head) head=tail=curr;	
			else
			{
				if(strcmp(curr->nama,head->nama)<0)
				{
					curr->next=head;
					head->prev=curr;
					head=curr;					
				}
				else if(strcmp(curr->nama,tail->nama)>0)
				{
					curr->prev=tail; 
					tail->next=curr;
					tail=curr;	
				}
				else
				{
					for(temp=head;strcmp(curr->nama,temp->next->nama)>0;temp=temp->next);
					curr->next=temp->next;					
					temp->next->prev=curr;
					temp->next=curr;
					curr->prev=temp;
				}				
			}		
		}
		else if(!strcmp(p,"delete"))
		{
			p=strtok(NULL,"\n");
			for(curr=head;curr;curr=curr->next)
				if(!strcmp(curr->nama,p)) break;
			laba-=curr->jum*curr->hbeli;
			curr->prev->next=curr->next;
			curr->next->prev=curr->prev;
			free(curr);
		}
		else if(!strcmp(p,"buy"))
		{
			p=strtok(NULL," ");
			for(curr=head;curr;curr=curr->next)
				if(!strcmp(curr->nama,p)) break;
			p=strtok(NULL,"\n"); curr->jum+=atoi(p);
		}
		else if(!strcmp(p,"sell"))
		{
			p=strtok(NULL," ");
			for(curr=head;curr;curr=curr->next)
				if(!strcmp(curr->nama,p)) break;
			p=strtok(NULL,"\n"); curr->jum-=atoi(p);
			laba+=atoi(p)*(curr->hjual - curr->hbeli);
		}
		else if(!strcmp(p,"report"))
		{
			if(first) first=0;
			else printf("\n");
			printf("                  INVENTORY REPORT\n");
			printf("Item Name     Buy At      Sell At      On Hand        Value\n");
			printf("---------     ------      -------      -------        -----\n");
			for(total=0,curr=head;curr;curr=curr->next)
			{
				curr->nilai=curr->jum*curr->hbeli;
				total+=curr->nilai;
				printf("%-10s %9.2lf %12.2lf %12d %12.2lf\n",
					curr->nama,curr->hbeli,curr->hjual,curr->jum,curr->nilai);
			}
			printf("------------------------\n");
			printf("Total value of inventory                       %12.2lf\n",total);
			printf("Profit since last report                       %12.2lf\n",laba);
			laba=0;
		}				
	}
	while(head)
	{
		curr=head;
		head=head->next;
		free(curr);	
	}
	return 0;
}

Posted: Tue Feb 13, 2007 11:51 am
by rio
Try mamun's testcase. And debug.

Posted: Sun Nov 11, 2007 12:12 pm
by rushafi
My code passed all the test cases above, but I am getting WA. Can anybody help?

Code: Select all

#pragma warning(disable:4786)

#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>



using namespace std;

struct type{
	string name;
	char name2[20];
	double cost;
	double sell;
	int quantity;
};

bool operator < (const type &a,const type &b){
	if(a.name<b.name) return true;
	else return false;
}

map <string,int> mymap;
vector <type> v;
int obj_count;
double expence,earn,loss,profit;

void add(char name[],double buy,double sell){
	type temp;
	strcpy(temp.name2,name);
	temp.name=name;
	temp.cost=buy;
	temp.sell=sell;
	temp.quantity=0;
	mymap[temp.name]=obj_count++;
	v.push_back(temp);
}

void remove(char name[]){
	string temp=name;
	int mark=mymap[temp];
	loss+=v[mark].cost * v[mark].quantity;
	v.erase(v.begin()+mark);
	obj_count--;
	//remapping
	for(int i=mark;i<obj_count;i++){
		mymap[v[i].name]=mymap[v[i].name]-1;
	}
}

void buy(char name[],int amount){
	string temp=name;
	int index=mymap[temp];
	v[index].quantity+=amount;
}

void _sell(char name[],int amount){
	string temp=name;
	int index=mymap[temp];
	earn+=v[index].sell * amount;
	expence+=v[index].cost * amount;
	v[index].quantity-=amount;
}

void report(){
	int i;
	double sum=0.;
	profit=earn - expence - loss;
	earn=0.;
	expence=0.;
	loss=0.;
	sort(v.begin()+1,v.end());

	//remapping

	for(i=1;i<obj_count;i++){
		mymap[v[i].name]=i;
	}



	printf("                  INVENTORY REPORT\n");
	printf("Item Name     Buy At      Sell At      On Hand        Value\n");
	printf("---------     ------      -------      -------        -----\n");
	for(i=1;i<obj_count;i++){
		printf("%-15s%5.2lf%13.2lf%13d%13.2lf\n",v[i].name2,v[i].cost,v[i].sell,v[i].quantity,v[i].quantity * v[i].cost);
		sum+= v[i].quantity * v[i].cost;
	}
	printf("------------------------\n");
	printf("Total value of inventory%35.2lf\n",sum);
	printf("Profit since last report%35.2lf\n",profit);

}





int main(){
	//freopen("330.in","r",stdin);
	bool flag=false;
	char line[1005],cmnd[50];
	char name[50];
	double cost,sell;
	expence=0.,earn=0.,loss=0.;
	type dummy;
	dummy.name="";
	dummy.name2[0]=0;
	dummy.cost=0.;
	dummy.quantity=0;
	dummy.sell=0.;
	v.push_back(dummy);
	int quantity;
	obj_count=1;
	while(gets(line)){
		if(line[0]=='*') break;
		sscanf(line,"%s",cmnd);
		if(!strcmp(cmnd,"new")) {
			sscanf(line,"%s %s %lf %lf",cmnd,name,&cost,&sell);
			add(name,cost,sell);
		}
		else if(!strcmp(cmnd,"delete")) {
			sscanf(line,"%s %s",cmnd,name);
			remove(name);
		}
		else if(!strcmp(cmnd,"buy")) {
			sscanf(line,"%s %s %d",cmnd,name,&quantity);
			buy(name,quantity);
		}
		else if(!strcmp(cmnd,"sell")) {
			sscanf(line,"%s %s %d",cmnd,name,&quantity);
			_sell(name,quantity);
		}
		else if(!strcmp(cmnd,"report")) {
			if(!flag) flag=true;
			else printf("\n");
			report();
		}
	}
	return 0;
}

Getting WA 330 : someone plz help me.........

Posted: Fri Jul 16, 2010 2:28 pm
by ferd@u$

Code: Select all

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

#define MAX 100010
#define LEN 20

struct List
{
	char s[LEN+1];
	char t[LEN+1];
}list[MAX];

long M;
char map[10005][100],temp[10000],buf[10000],str[10000];
double mat[10005][10]={0};

int sort_function( const void *a, const void *b)
{
	char *s,*t;

	s = ((struct List*)a)->s; //by changing ->t/s we can get a diffrnt sort
	t = ((struct List*)b)->s; //by changing ->t/s we can get a diffrnt sort
	return strcmp(s,t);
}

int main()
{
	long N,flag,i,j,c,n;
	double b,s,profit,totalValue;

	N=c=0;
	while(gets(buf))
	{
		if(buf[0]=='*')
			break;
		else if(buf[0]=='n')
		{
			sscanf(buf,"%s %s %lf %lf",&temp,&str,&b,&s);
			flag=0;
			for(i=0;i<N;i++)
			{
				if(strcmp(map[i],str)==0)
				{
					flag=1;
					break;
				}
			}
			if(flag==0)
			{
				strcpy(map[N],str);
				mat[N][0]=b;
				mat[N][1]=s;
				N++;
			}
		}
		else if(buf[0]=='b')
		{
			sscanf(buf,"%s %s %ld",temp,str,&n);

			for(i=0;i<N;i++)
			{
				if(strcmp(map[i],str)==0)
				{
					mat[i][2]+=n;
					break;
				}
			}
		}
		else if(buf[0]=='s')
		{
			sscanf(buf,"%s %s %ld",&temp,&str,&n);

			for(i=0;i<N;i++)
			{
				if(strcmp(map[i],str)==0)
				{
					if(n<=mat[i][2])
					{
						mat[i][3]+=n;
						mat[i][2]-=n;
					}
					break;
				}
			}
		}
		else if(buf[0]=='d')
		{
			sscanf(buf,"%s %s",&temp,&str);

			for(i=0;i<N;i++)
			{
				if(strcmp(map[i],str)==0)
				{
					c+=(mat[i][0]*mat[i][2]);
					strcpy(map[i],"|||||||||");
					break;
				}
			}
		}
		else if(buf[0]=='r')
		{
			totalValue=profit=M=0;
			for(i=0;i<N;i++)
			{
				if(strcmp(map[i],"|||||||||")!=0)
				{
					mat[i][4]=mat[i][0]*mat[i][2];
					totalValue+=mat[i][4];
					profit+=(mat[i][1]*mat[i][3])-(mat[i][0]*mat[i][3]);
					mat[i][3]=0;
					strcpy(list[M].s,map[i]);
					
					M++;
				}
			}
			qsort((void *)list,M, sizeof(list[0]), sort_function);

			printf("                  INVENTORY REPORT\n");
			printf("Item Name     Buy At      Sell At      On Hand        Value\n");
			printf("---------     ------      -------      -------        -----\n");

			for(i=0;i<M;i++)
			{
				for(j=0;j<N;j++)
				{
					if(strcmp(list[i].s,map[j])==0)
					{
						printf("%-10s %9.2lf   %10.2lf   %10.0lf   %10.2lf\n",map[j],mat[j][0],mat[j][1],mat[j][2],mat[j][4]);
						break;
					}
				}
			}
			printf("------------------------\n");
			printf("Total value of inventory                         %10.2lf\n",totalValue);
			printf("Profit since last report                         %10.2lf\n\n",profit-c);

			c=0;
		}
	}
	return 0;
}


Re: 330 - Inventory Maintenance

Posted: Fri May 13, 2011 9:14 pm
by surya ss
hello can anybody help me?
I've tried all the I/O I found has the same output as my code but still WA
is there anything need to be check?
here is my code:

Code: Select all

AC
PS : I should print blank line after each report not between report, such a mistake ..., and check the full string for action not just the first character