327 - Evaluating Simple C Expressions

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

nejhum
New poster
Posts: 8
Joined: Sun Jun 09, 2002 9:36 pm

327 - Evaluating Simple C Expressions

Post by nejhum »

can any one give me a sample input why problem #327 is WA?
by

S M Shahed Nejhum

..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. »

hm....I guess there may be something like
a + + - b
in the input. At first I got WA on this problem, but after I try to remove all space from the input, I got accepted. So you can try to remove space from the input first. :D

nejhum
New poster
Posts: 8
Joined: Sun Jun 09, 2002 9:36 pm

Post by nejhum »

here is the out put of my program;

Expression: a + + - b
value = -1
a = 2
b = 2

i think this is ok.

what do you think?

i have trancated the blanksalso..

nejhum
by

S M Shahed Nejhum

Yarin
Problemsetter
Posts: 112
Joined: Tue Sep 10, 2002 5:06 am
Location: Ume
Contact:

Post by Yarin »

Here's some sample input and my programs (which is AC) output. Maybe it can help?
b++---c+d-c+b
z+d+c++-++b-a
++b-++c+--e-b+f++
--a-++b+c+a-c++
should yield
Expression: b++---c+d-c+b
value = 5
b = 3
c = 2
d = 4
Expression: z+d+c++-++b-a
value = 29
a = 1
b = 3
c = 4
d = 4
z = 26
Expression: ++b-++c+--e-b+f++
value = 6
b = 3
c = 4
e = 4
f = 7
Expression: --a-++b+c+a-c++
value = -3
a = 0
b = 3
c = 4

nejhum
New poster
Posts: 8
Joined: Sun Jun 09, 2002 9:36 pm

Post by nejhum »

dear yarin ,

thank you very much for your sample input data,
it helped me a lot to CORRECT my problem

nejhum :lol:
by

S M Shahed Nejhum

jonas
New poster
Posts: 4
Joined: Mon Nov 25, 2002 2:59 am

Post by jonas »

Yarin wrote:Here's some sample input and my programs (which is AC) output. Maybe it can help?

<snip input and output>
I could use some help with this one. I still get Wrong Answer, even though my program works with Yarin's input and output data. I am stumped.
:cry:

MV
New poster
Posts: 4
Joined: Thu Oct 31, 2002 11:30 am

Post by MV »

Have you considered the following cases?

-a
+a

Anyway, test cases like a+b-a won't be in the input.

jonas
New poster
Posts: 4
Joined: Mon Nov 25, 2002 2:59 am

Post by jonas »

MV wrote:Have you considered the following cases?

-a
+a

Anyway, test cases like a+b-a won't be in the input.
I have intentionally ignored those cases, since unary plus and minus are not mentioned in the problem description.

But you are right about Yarin's test cases - they do contain some lines with "undefined behaviour".

MV
New poster
Posts: 4
Joined: Thu Oct 31, 2002 11:30 am

Post by MV »

OK, you are rigth about the unary operations. Here are some more test cases and the output my AC program gives.
  • a + b
    b - z
    a+b--+c++
    c+f--+--a
    f-- + c-- + d-++e

    a--+b--+c--+d--+e--+f--+g--+h--+i++-j++-k++-l++-m++-n++-o++-p++-q++-r++-s+t-u+v
    --z+a--
    a
    z
    ++ x+--y+z++
    a++-c++-m+ + +o+k
output:
  • Expression: a + b
    value = 3
    a = 1
    b = 2
    Expression: b - z
    value = -24
    b = 2
    z = 26
    Expression: a+b--+c++
    value = 6
    a = 1
    b = 1
    c = 4
    Expression: c+f--+--a
    value = 9
    a = 0
    c = 3
    f = 5
    Expression: f-- + c-- + d-++e
    value = 7
    c = 2
    d = 4
    e = 6
    f = 5
    Expression:
    value = 0
    Expression: a--+b--+c--+d--+e--+f--+g--+h--+i++-j++-k++-l++-m++-n++-o++-p++-q++-r++-s+t-u+v
    value = -79
    a = 0
    b = 1
    c = 2
    d = 3
    e = 4
    f = 5
    g = 6
    h = 7
    i = 10
    j = 11
    k = 12
    l = 13
    m = 14
    n = 15
    o = 16
    p = 17
    q = 18
    r = 19
    s = 19
    t = 20
    u = 21
    v = 22
    Expression: --z+a--
    value = 26
    a = 0
    z = 25
    Expression: a
    value = 1
    a = 1
    Expression: z
    value = 26
    z = 26
    Expression: ++ x+--y+z++
    value = 75
    x = 25
    y = 24
    z = 27
    Expression: a++-c++-m+ + +o+k
    value = 11
    a = 2
    c = 4
    k = 11
    m = 14
    o = 15
There propably aren't any empty test cases :roll: in the real input, so those cases can be omitted when comparing the outputs.
Anyway, my program could be wrong in some cases too, though AC.

jonas
New poster
Posts: 4
Joined: Mon Nov 25, 2002 2:59 am

Post by jonas »

Thank you for your help. I have modified my program to deal with empty lines, so now my output for the test cases you supplied matches yours exactly.

But alas... still WA.

sjn
Learning poster
Posts: 73
Joined: Mon Apr 08, 2002 8:22 am
Contact:

help

Post by sjn »

i have passed all of the test data above

but still WA

who can help me :cry:


thx in advance :roll:

sjn
Learning poster
Posts: 73
Joined: Mon Apr 08, 2002 8:22 am
Contact:

i got it

Post by sjn »

At last i got AC :evil:

there is no input like following

-a
a--b
++a + ++a + ++a

all of the input are very strict

But i don't why my code submited twice and got WA & AC

rodriwan
New poster
Posts: 8
Joined: Mon Jun 03, 2002 8:13 pm

line by itself and will contain no more than 80 characters.

Post by rodriwan »

OK. Ive spent a lot of time trying to solve this problem.
After more than a week I decided to make the vector were I was reading the input bigger (5512 position, instead of 100 initialy). That made me get an accepted.

But in the problem specification it is really clear "Each of the expressions will appear on a line by itself and will contain no more than 80 characters.". He says "characters" not "valid characters", and, at least to me, space is a character also.

For those who are trying to solve this problem, just enlarge the input vector (at least it worked for me).

Betty
New poster
Posts: 19
Joined: Sun Aug 17, 2003 2:10 pm

Post by Betty »

Yarin wrote:Here's some sample input and my programs (which is AC) output. Maybe it can help?
b++---c+d-c+b
should yield
Expression: b++---c+d-c+b
value = 5
b = 3
c = 2
d = 4
I get value = 4 and my program was AC

jaracz
Learning poster
Posts: 79
Joined: Sun Sep 05, 2004 3:54 pm
Location: Poland

Post by jaracz »

It can't be input like
a + ++a or a+ --a ...and so on
I mean it can't be same letter used a few times
cause it was written
Execute the statements generated in step 1, then those generated in step 3, and finally the one generated in step 2, in that order.
so at first calculate expresisons like ++a and so on
suppose it was such an input
--a + ++a
so according to problem description first calculate all values
so a = 1 then 0 then 1
and now calculate expression so it should be 1 + 1 so final value is 2
according to some of you guys it will be
--a + ++a so
(0) + (1) and value will be 1

that's why these input (b++---c+d-c+b) gave 2 different answers and is invalid so there mustn't be input like this

for those who still get WA:
consider this a+++++b case
and remember to clear string from spaces
i.e. string[remove(string,string+strlen(string),' ') - string] = '\0';
Regards
keep it real!

Post Reply

Return to “Volume 3 (300-399)”