330 - Inventory Maintenance

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post 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.

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post 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
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

N|N0
New poster
Posts: 36
Joined: Tue Jan 25, 2005 10:33 pm
Location: Ljubljana, Slovenija
Contact:

Problem 330 - Inventory Maintenance

Post 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?

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

'Profit since last report can be negative. But profit is always positive.' - You should consider this.
Ami ekhono shopno dekhi...
HomePage

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post 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
*

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post 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'.
Ami ekhono shopno dekhi...
HomePage

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post 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. :)

razor_blue
New poster
Posts: 27
Joined: Mon Nov 27, 2006 4:44 am
Location: Indonesia

Post 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;
}

rio
A great helper
Posts: 385
Joined: Thu Sep 21, 2006 5:01 pm
Location: Kyoto, Japan

Post by rio »

Try mamun's testcase. And debug.

rushafi
New poster
Posts: 2
Joined: Wed Sep 13, 2006 7:25 pm
Location: Dhaka, Bangladesh.

Post 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;
}

ferd@u$
New poster
Posts: 4
Joined: Fri Jul 16, 2010 2:02 pm
Location: CUET
Contact:

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

Post 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;
}


surya ss
New poster
Posts: 22
Joined: Sat Jun 11, 2005 7:31 pm

Re: 330 - Inventory Maintenance

Post 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

Post Reply

Return to “Volume 3 (300-399)”