10875 - Big Math

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

Moderator: Board moderators

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

10875 - Big Math

Post by Eduard » Mon Jun 27, 2005 1:56 pm

Hello.
I'm getting this problem WA.
I can't find any mistake.Please give me some tests.
Can answer be negative? :(
Thanks.
Eduard.
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel » Mon Jun 27, 2005 2:06 pm

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.

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard » Mon Jun 27, 2005 2:28 pm

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.
Last edited by Eduard on Mon Jun 27, 2005 2:30 pm, edited 1 time in total.
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650

misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof » Mon Jun 27, 2005 2:33 pm

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

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard » Mon Jun 27, 2005 3:05 pm

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.
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard » Mon Jun 27, 2005 5:26 pm

Thanks to everybody who helped me, finally I got AC. :D
Eduard.
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650

Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry » Mon Jun 27, 2005 9:01 pm

What was your problem?

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard » Mon Jun 27, 2005 9:19 pm

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.
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650

TISARKER
Learning poster
Posts: 88
Joined: Tue Oct 12, 2004 6:45 pm
Location: Bangladesh
Contact:

10875(Big Math wrong answer)

Post by TISARKER » Mon Jun 27, 2005 9:30 pm

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.
Mr. Arithmetic logic Unit

misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof » Tue Jun 28, 2005 1:05 am

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 :(

TISARKER
Learning poster
Posts: 88
Joined: Tue Oct 12, 2004 6:45 pm
Location: Bangladesh
Contact:

Post by TISARKER » Tue Jun 28, 2005 6:02 am

A lot of Thx to misof
You are really genious.
Mr. Arithmetic logic Unit

rahurprem
New poster
Posts: 10
Joined: Mon Mar 28, 2005 5:59 pm
Location: Dhaka, Bangladesh
Contact:

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

Post by rahurprem » Tue Jul 12, 2005 8:35 pm

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.
Programmer? No, no, i am a speedy typist.

User avatar
Cho
A great helper
Posts: 274
Joined: Wed Oct 20, 2004 11:51 pm
Location: Hong Kong

Post by Cho » Wed Jul 13, 2005 5:36 am

SIGFPE is divided-by-zero exception.

james299
New poster
Posts: 10
Joined: Tue Jul 26, 2005 8:58 am
Location: Taiwan

Post by james299 » Tue Jul 26, 2005 6:14 pm

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

Raj Ariyan
Learning poster
Posts: 70
Joined: Sat Feb 05, 2005 9:38 am
Location: Gurukul

Post by Raj Ariyan » Wed Jul 27, 2005 5:58 pm

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.
Some Love Stories Live Forever ....

Post Reply

Return to “Volume 108 (10800-10899)”