Page 1 of 2

10875 - Big Math

Posted: Mon Jun 27, 2005 1:56 pm
by Eduard
Hello.
I'm getting this problem WA.
I can't find any mistake.Please give me some tests.
Can answer be negative? :(
Thanks.
Eduard.

Posted: Mon Jun 27, 2005 2:06 pm
by Adrian Kuegel
According to the problem statement:
"the intermediate and final results will fit in a 32-bit signed integer."
So I guess you can answer your question yourself :wink:

And tricky test inputs: try one which has an answer < 0, like 2-3.

Posted: Mon Jun 27, 2005 2:28 pm
by Eduard
Hello.
Thanks Adrian for replay.
My program works for neative answers.
Input

Code: Select all

000 000 ... 000 000
..0 ..0 ... ..0 ..0
000 000 000 000 000
0.. 0.. ... ..0 ..0
000 000 ... 000 000

.0. ... 000
.0. ... ..0
.0. 000 000
.0. ... 0..
.0. ... 000

.0. ... .0. 000 000 0.0 000 0.. 000 000 000
.0. ... .0. ..0 ..0 0.0 0.. 0.. ..0 0.0 0.0
.0. 000 .0. 000 000 000 000 000 ..0 000 000
.0. ... .0. 0.. ..0 ..0 ..0 0.0 ..0 0.0 ..0
.0. ... .0. 000 000 ..0 000 000 ..0 000 ..0

000
0.0
0.0
0.0
000
Output

Code: Select all

... .0. .0.
... .0. .0.
000 .0. .0.
... .0. .0.
... .0. .0.

... .0.
... .0.
000 .0.
... .0.
... .0.

... .0. 000 000 0.0 000 0.. 000 000 000
... .0. ..0 ..0 0.0 0.. 0.. ..0 0.0 0.0
000 .0. 000 000 000 000 000 ..0 000 000
... .0. 0.. ..0 ..0 ..0 0.0 ..0 0.0 0.0
... .0. 000 000 ..0 000 000 ..0 000 000
I need some more tests.
Thanks.
Eduard.

Posted: Mon Jun 27, 2005 2:33 pm
by misof
Check that:
- you don't have spaces at the end of the lines
- you print a blank line after the last data set
also, you may try inputs like:
10/3*3
1+2*3

Posted: Mon Jun 27, 2005 3:05 pm
by Eduard
Thanks Misof but I'm have consider all that.
Input

Code: Select all

.0. 000 .0. 000 0.0 000
.0. 0.0 ... ..0 0.0 ..0
.0. 0.0 000 000 .0. 000
.0. 0.0 ... ..0 0.0 ..0
.0. 000 .0. 000 0.0 000

000
0.0
0.0
0.0
000
Output

Code: Select all

000
0.0
000
..0
..0

I need some other test.
Thanks.
Eduard.

Posted: Mon Jun 27, 2005 5:26 pm
by Eduard
Thanks to everybody who helped me, finally I got AC. :D
Eduard.

Posted: Mon Jun 27, 2005 9:01 pm
by Larry
What was your problem?

Posted: Mon Jun 27, 2005 9:19 pm
by Eduard
Hello Larry.
It's hard to tell what was my problem but for some reason my program were losing some zeros.For example 11*10=11
Eduard.

10875(Big Math wrong answer)

Posted: Mon Jun 27, 2005 9:30 pm
by TISARKER
I checked for negative number.
I checked for 32bit signed integer number.
I checked for not having space in the end of the line.
I checked for having newline in the end of each output.
I checked operator precidence according to problem statement.
I checked not processing for last input 0.
But I am getting wrong answer again and again and ...... .
I am going to be hopeless.
Please help me any how.

Posted: Tue Jun 28, 2005 1:05 am
by misof
Try a single non-zero number (without any operators).
Also, double-check, whether you handle the priorities correctly, 1-10/3*3-1 should give -9.
Only stop when the input is 0, not when the result is 0.

Nothing else comes to my mind... if this doesn't help, probably try posting your code :(

Posted: Tue Jun 28, 2005 6:02 am
by TISARKER
A lot of Thx to misof
You are really genious.

10875: What was meant by RUNTIME ERROR (SIGFPE)?

Posted: Tue Jul 12, 2005 8:35 pm
by rahurprem
Hi,
I don't understand what was meant by that Floating Point Exception generated during submission my code of 10875 (Big Math).

Code: Select all

#include<stdio.h>

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

char input[10][1000];
char inputP[100][5][3];
int operands[100];
char opt[100];
int t;
int ooo[256];

char abc[100];
int xyz[100];



const char p[14][5][3]=
{
	
	'0','0','0',
	'0','.','0',
	'0','.','0',
	'0','.','0',
	'0','0','0',

	
	'.','0','.',
	'.','0','.',
	'.','0','.',
	'.','0','.',
	'.','0','.',

	
	'0','0','0',
	'.','.','0',
	'0','0','0',
	'0','.','.',
	'0','0','0',

	
	'0','0','0',
	'.','.','0',
	'0','0','0',
	'.','.','0',
	'0','0','0',

	
	'0','.','0',
	'0','.','0',
	'0','0','0',
	'.','.','0',
	'.','.','0',

	
	'0','0','0',
	'0','.','.',
	'0','0','0',
	'.','.','0',
	'0','0','0',

	
	'0','.','.',
	'0','.','.',
	'0','0','0',
	'0','.','0',
	'0','0','0',

	
	'0','0','0',
	'.','.','0',
	'.','.','0',
	'.','.','0',
	'.','.','0',

	
	'0','0','0',
	'0','.','0',
	'0','0','0',
	'0','.','0',
	'0','0','0',

	
	'0','0','0',
	'0','.','0',
	'0','0','0',
	'.','.','0',
	'.','.','0',

	
	'.','0','.',
	'.','0','.',
	'0','0','0',
	'.','0','.',
	'.','0','.',

	
	'.','.','.',
	'.','.','.',
	'0','0','0',
	'.','.','.',
	'.','.','.',

	
	'0','.','0',
	'0','.','0',
	'.','0','.',
	'0','.','0',
	'0','.','0',

	
	'.','0','.',
	'.','.','.',
	'0','0','0',
	'.','.','.',
	'.','0','.',

};

int whatSymbol(int i)
{
	int j;
	int w=0;
	for(j=0; j<14; j++)
	{
		if(memcmp(inputP[i],p[j],sizeof(inputP[i]))==0)
		{
			w = j;	
		}
	}
	return w;
}



void findHighestOperator()
{
	int h=1;
	int i=0,j;

	ooo['+']=1;
	ooo['-']=1;
	ooo['*']=2;
	ooo['/']=2;

	
	for(j=0; j<t; j++)
	{
		if(ooo[opt[j]]>h)
		{
			h = ooo[opt[j]];
			i = j;
		}
	}

	switch(opt[i])
	{
		case '+':
			{
				operands[i] = operands[i]+operands[i+1];
				break;
			}
		case '-':
			{
				operands[i] = operands[i]-operands[i+1];
				break;
			}
		case '*':
			{
				operands[i] = operands[i]*operands[i+1];
				break;
			}
		case '/':
			{
				operands[i] = operands[i]/operands[i+1];
				break;
			}
	}
	memcpy(abc,&opt[i+1],sizeof(&opt[i]));
	memcpy(&opt[i],abc,sizeof(abc));

	memcpy(xyz,&operands[i+2],sizeof(&operands[i+2]));
	memcpy(&operands[i+1],xyz,sizeof(xyz));	
}


int main()
{
	int o,k;

	char hh[100];
	
	int i=0,j;
	char m[1000];
	int space=0;
	int len;
	char *pq=NULL;
	int b;
	int z=0;
	int flag=0,flag2=0;
	char op1[100];
	int count=0;
	int y;
	int x;
	char *ghi = NULL;
	char *gh=NULL;
	int xx,minus;
	int c;




	freopen("10875.in","r",stdin);
	
	do
	{
		if(flag)
		{
			printf("\n");			
		}
		flag=1;
		space =0;
		pq = NULL;

		gets(m);		
		len = strlen(m);
		
		for(j=0; j<len; j++)
		{
			if(m[j]==' ')
				space++;
		}	
		space++;
		z=0;
		while(len>0 || m[0]==' ')
		{
			pq = strtok(m," \n");
			for(k=0; k<space; k++)
			{
				for(b=0; b<3; b++)
				{
					inputP[i][z][b] = pq[b];
				}
				pq = strtok(NULL," \n");
				i = (i+1)%space;
			}			
			gets(m);
			len = strlen(m);
			
			z++;
		}
		int count=0;

		if(whatSymbol(0)==0 && space==1)
			break;
		for(i=0; i<space; i++)
		{
			x =whatSymbol(i);
			if(x<=9)
			{
				op1[count++]=x+48;
				op1[count]='\0';
			}
			else
			{
				if(x==10)
				{
					op1[count++]='+';
					op1[count]='\0';
				}
				else if(x==11)
				{
					op1[count++]='-';
					op1[count]='\0';
				}
				else if(x==12)
				{
					op1[count++]='*';
					op1[count]='\0';
				}
				else
				{
					op1[count++]='/';
					op1[count]='\0';
				}
			}
		}


		/*
		Equation processing  */

		
		strcpy(hh,op1);

		y=0;
		t=0;
		
		ghi = strtok(op1,"0123456789");
		while(ghi)
		{
			opt[t++]=ghi[0];
			ghi = strtok(NULL,"0123456789");
		}
		opt[t]='\0';


		
		gh = strtok(hh,"+-*/");
		while(gh)
		{
			operands[y++]=atoi(gh);
			gh = strtok(NULL,"+-*/");
		}


		
		while((len = strlen(opt))>0)
		{
			findHighestOperator();
		}
		
	
		

		xx=operands[0];

		minus=0;
		if(xx<0)
		{
			xx = xx*(-1);
			minus = 1;
		}

		char a[100];


		
		for(int h=0; h<5; h++)
		{
		
		  sprintf(a,"%d",xx);
			c = strlen(a);
			if(minus)
				c++;
			
			int f;
			if(minus)
			{
				f = 11;
			}
			else
			{
				f=a[0]-'0';
			}

			
			for(o=0; o<c ; ) 
			{
				for(k=0; k<3; k++)
				{
					printf("%c",p[f][h][k]);
				}		
				printf(" ");
				if(minus)
				{
					f = a[o++]-'0';
				}
				else
				{
					f = a[++o]-'0';
				}
			}
			printf("\n");
			i=0;
		}					
	}while(1);
	return 0;
}


With Thanks,
rahurprem.

Posted: Wed Jul 13, 2005 5:36 am
by Cho
SIGFPE is divided-by-zero exception.

Posted: Tue Jul 26, 2005 6:14 pm
by james299
hi!
I try to solve the prob but I got several WA, and I don't know why.

I check TISARKER and misof said.

1. I use long long int to proceed the compute, that is, my code can handle prob's input data without overflow.(array size is 1000)
2. And for each case I can sure no extra space at each end of line.
3. I don't sure that whether I should print last the blank line at final case or not. But I try both, and all got WA. So I think the err maybe is not only this one.
4. For single non-zero number, both positive and negative number is correctly printed.
5. For those input data which result is 0, my output seems to correct.

Follow are my I/O for this prob :

Code: Select all

.0. ... .0. 000 .0. 000 0.0 000 ... .0.
.0. ... .0. 0.0 ... ..0 0.0 ..0 ... .0.
.0. 000 .0. 0.0 000 000 .0. 000 000 .0.
.0. ... .0. 0.0 ... ..0 0.0 ..0 ... .0.
.0. ... .0. 000 .0. 000 0.0 000 ... .0.

000
..0
000
0..
000

000
..0
000
..0
000

0.0
0.0
000
..0
..0

000
0..
000
..0
000

000
0..
000
0.0
000

... 000 .0.
... ..0 .0.
000 ..0 .0.
... ..0 .0.
... ..0 .0.

000
0.0
000
0.0
000

000
0.0
000
..0
..0

.0. 000 000 0.0 000 000 000 000 000 .0. 000 000 000 000 000 000
.0. ..0 ..0 0.0 0.. 0.. ..0 0.0 0.0 .0. 0.0 0.0 0.0 0.0 0.0 0.0
.0. 000 000 000 000 000 ..0 000 000 .0. 0.0 0.0 0.0 0.0 0.0 0.0
.0. 0.. ..0 ..0 ..0 0.0 ..0 0.0 ..0 .0. 0.0 0.0 0.0 0.0 0.0 0.0
.0. 000 000 ..0 000 000 ..0 000 ..0 .0. 000 000 000 000 000 000

... .0. 000 000
... .0. ..0 0.0
000 .0. 000 000
... .0. 0.. 0.0
... .0. 000 000

000
0.0
0.0
0.0
000

Code: Select all

... 000
... 0.0
000 000
... ..0
... ..0

000
..0
000
0..
000

000
..0
000
..0
000

0.0
0.0
000
..0
..0

000
0..
000
..0
000

... 000 .0.
... ..0 .0.
000 ..0 .0.
... ..0 .0.
... ..0 .0.

000
0.0
000
0.0
000

000
0.0
000
..0
..0

.0. 000 000 0.0 000 000 000 000 000 .0. 000 000 000 000 000 000
.0. ..0 ..0 0.0 0.. 0.. ..0 0.0 0.0 .0. 0.0 0.0 0.0 0.0 0.0 0.0
.0. 000 000 000 000 000 ..0 000 000 .0. 0.0 0.0 0.0 0.0 0.0 0.0
.0. 0.. ..0 ..0 ..0 ..0 ..0 0.0 ..0 .0. 0.0 0.0 0.0 0.0 0.0 0.0
.0. 000 000 ..0 000 000 ..0 000 ..0 .0. 000 000 000 000 000 000

... .0. 000 000
... .0. ..0 0.0
000 .0. 000 000
... .0. 0.. 0.0
... .0. 000 000

Thaks for you guys precious time, please give me a little hint or some suggestions.
:)

Posted: Wed Jul 27, 2005 5:58 pm
by Raj Ariyan
Hi James,
Have u check this type of input. Like 0-2 = -2, i mean start with 0 but not only have one digit. 0 follows with some operation. Good luck.