Page 3 of 3

Posted: Fri Aug 18, 2006 8:10 pm
by shanto86
What about

Code: Select all

-9/-3 - 3
-9/-3 + 3
-9/-3+-3
-9/+3-+3

Posted: Fri Aug 18, 2006 8:35 pm
by Tosgin
Your first two test cases are invalid because they contain blanks.
Qute from the problem statement:
Because main memory was very small in those days, the string will contain no blanks or tabs, nor parathensis.

Posted: Fri Aug 18, 2006 8:44 pm
by shanto86
well... actually just omit the blanks. i did not want to give blank. it was a mistake!

Posted: Fri Aug 18, 2006 9:34 pm
by little joey
Just some randomly generated input:

Code: Select all

-30.886/-47.793*-60.492--2.362*-20.059*80.540+-55.736+-2.567*21.530
-74.067-79.802*-33.069--18.456*-76.229-44.919-75.198/-64.370*76.091+41.873*-6.996-20.925/-60.336*-21.729--16.124*0.545/15.434/-13.750+17.276+-93.584/-92.754+95.060+47.739*98.586
40.795*-60.378-10.097-70.492+97.301*-9.441--28.444+-44.729--38.097--90.675/4.567-72.353--55.306/28.624/-5.732
30.019+-59.708+18.149/-42.618
-93.451/92.379+-88.228*65.193*87.764*36.987/-46.491-9.859+-52.551+-53.275*76.121/-36.029/73.793+-66.143+39.529+-64.443-54.538+2.904+-70.688-69.917+87.743/-98.490
6.725*-17.505*-69.786*8.542--39.507+-76.348*-27.828/95.746+-55.422*-67.605-73.730-29.320+48.626-86.708*13.258-42.062+32.036/19.379+-90.071/3.881-45.894/-57.199/-52.996
72.813*81.095*65.084*27.684*-55.936--19.756*6.887+-32.172-2.336*67.587-97.713--1.255/17.721/39.811/11.705/29.150*63.920*67.269-45.630+11.972*-47.625*39.299*-83.898*56.190
-88.209/99.336/95.994/-14.769+-68.850+-48.142+-21.993-79.567-1.961*54.259*13.202+2.021+-89.528+49.908*48.036--86.248
32.133*-99.754*-90.368+38.046-66.249/-3.033-10.253*19.125+45.975
3.729+-53.414+26.304/78.050+8.902--58.699/32.002/-44.500
-8.538/22.535/-71.692/-20.504--90.964--95.343-2.900/-16.949/17.988/40.743-58.390+-53.541*-74.232*-90.360/-66.761--3.190+-6.367*30.690+26.057+28.205+47.386*2.726+56.552
28.946/-56.970+-99.631-18.627*-39.214
20.090/-9.610--52.274--56.620/77.888*87.770+90.417+-25.849--73.059*-34.945--28.458--4.289/-70.478
-34.471+33.459*58.025*-31.233-3.493*-10.699/-75.569*-28.794/96.431*39.390/-5.057-96.157/-1.947/-35.021+85.054
-35.325+53.516/46.139+82.338*39.021*-84.812-92.685+-24.176/-99.759+-93.499/63.950+-67.818/40.049/41.805/-77.375*33.589--44.918*44.822--24.030*-30.126*-50.253*-27.814/38.377/-27.088
6.732--0.346--45.691/22.573-49.291*77.217/75.055*78.130/-72.661--4.789+-56.851
-67.868
-39.006/62.949/-80.226--90.596-87.560/67.770/66.342+-72.379*94.984/44.234/70.072
-1.063*-82.030*86.059/26.924/71.232*30.443
75.640/50.262/61.041/8.324*-54.154--37.457/91.171--25.218/-14.653/53.959*15.797+21.308
And its output:

Code: Select all

-3966.030
5721.216
-3493.352
-30.115
401263.465
72715.847
-489478929.616
772.852
289574.936
-40.487
5555.057
630.300
-2346.095
-60589.550
-269634.795
11.568
-67.868
88.366
119.117
21.055

I got AC!!

Posted: Fri Aug 18, 2006 11:35 pm
by ivan.cu
After ages i got AC, the problem is floating point error, first in my Java program and next in C++ program for use float instead of double. for read double use the format "%lf" in function scanf or sscanf.

Thank to all for your help.

Help please

Posted: Sat Aug 19, 2006 1:08 pm
by Zaspire
I get WA many times. Why? Help please!

Code: Select all

#include <stdio.h>
#include <math.h>

double work(char *t1,int size)
{
	if (*t1=='+') return work(t1+1,size-1);
	double res=0;
	for (int i=size-1;i>0;i--)
	{
		if (t1[i]=='+'||t1[i]=='-')
			if (t1[i-1]!='/'&&t1[i-1]!='*')
			{
				res=work(t1,i);
				if (t1[i]=='+') res+=work(t1+i+1,size-i-1);
				else res-=work(t1+i+1,size-i-1);
				return res;
			}
	}
	for (int i=size-1;i>-1;i--)
	{
		if (t1[i]=='/'||t1[i]=='*')
		{
			res=work(t1,i);
			if (t1[i]=='/') res/=work(t1+i+1,size-i-1);
			else res*=work(t1+i+1,size-i-1);
			return res;
		}
	}
	sscanf(t1,"%lf",&res);
	return res;
}

int main()
{
#ifndef ONLINE_JUDGE
	//freopen("in.txt","r",stdin);
#endif
	char str[270],*t1,c,last;
	int size;
	double res;
	do
	{
		do
		{
			c=getchar();
		}
		while (c!=EOF&&(c<'0'||c>'9')&&c!='-'&&c!='+');
		t1=str;
		size=0;
		last='/';
		while (c!='\n'&&c!=EOF)
		{
			if (last=='/'||last=='*')
			{
				if (c=='-'||(c>='0'&&c<='9')) {*t1=last=c;t1++;size++;}
			}
			else
				if (last=='+'||last=='-')
				{
					if (c=='-') 
						if (last=='-') *(t1-1)=last='+';
						else *(t1-1)=last='-';
					else
						if (c=='+')
							if (last=='-') *(t1-1)=last='-';
							else *(t1-1)=last='+';
						else
						{
							*t1=last=c;
							t1++;
							size++;
						}
				}
				else
				{
					*t1=last=c;
					t1++;
					size++;
				}
			c=getchar();
		}
		*t1='\0';
		if (size)
		{
			res=work(str,size);
			if (abs(res)<0.0001) printf("0.000\n");
			else printf("%.3lf\n",res);
		}
	}while (c!=EOF);
	return 0;
}

Posted: Sat Aug 19, 2006 8:42 pm
by Sedefcho
Thanks for the input, little joey.

The first answer of my prog for your test cases was:
-3966.-29
( That's because I have a stupid custom Java method which
rounds doubles up to three digits after the decimal point,
and that method was not working for negative numbers at all ).

Which explains the WA which I was getting...

Regards.

Re: Help please

Posted: Wed Aug 23, 2006 9:18 pm
by Martin Macko
Zaspire wrote:I get WA many times. Why? Help please!
I don't know how the floating point numbers in the input file may or may not be written, but your solution isn't working well for numbers such as .5:

Code: Select all

1/.5
The correct output:

Code: Select all

2

Posted: Wed Aug 23, 2006 9:23 pm
by Darko
My AC code produces:

Code: Select all

-0.067
I assumed that numbers start with digits and used do-while instead of while.

Posted: Mon Aug 28, 2006 10:01 am
by Zaspire
I corect my program and it's work with 1/.5 but still get WA.
After that I simple remove line :P

Code: Select all

if (abs(res)<0.0001) printf("0.000\n");
A get AC. But Why :o ?
if abs(res)<0.0001 the output must be 0.000, But it's wrong?

Posted: Mon Aug 28, 2006 2:51 pm
by helloneo
Zaspire wrote:I corect my program and it's work with 1/.5 but still get WA.
After that I simple remove line :P

Code: Select all

if (abs(res)<0.0001) printf("0.000\n");
A get AC. But Why :o ?
if abs(res)<0.0001 the output must be 0.000, But it's wrong?
abs() is for integers.. you may use fabs() instread

Posted: Tue Aug 29, 2006 10:38 pm
by Martin Macko
helloneo wrote:abs() is for integers.. you may use fabs() instread
It depends. In plain C abs() is for integers only, however in C++ abs() is an overloaded symbol for:
  • - double abs(double __x);
    - float abs(float __x);
    - long double abs(long double __x);
defined in <cmath> and for:
  • - long abs(long __i);
    - long long abs(long long __x);
defined in <cstdlib>. Therefore, if you write the following in C++, you get the correct answer (i.e., 4.700).
  • #include <cmath>

    int main()
    {
    • ...
      double a=-4.7;
      printf("%3lf\n", abs(a));
      ...
    }

Re: 11070 - The Good Old Times

Posted: Fri Apr 29, 2011 9:43 pm
by Shafaet_du
Can anyone tell me why C++ shows -0.000 in case like -10*0 ?? Does that make any sense? I wrote this way to avoid the trap:

Code: Select all

if(STK[0]<1e-11 && STK[0]>-(1e-11)) STK[0]=0;
        printf("%.3lf\n",STK[0]);