Page 3 of 17

Posted: Fri Jan 16, 2004 8:00 pm
by turuthok
Yes.

-turuthok-

Posted: Sat Jan 17, 2004 3:54 am
by AaronWu
Could you show me with a simple example ?(concerning gets(),followed by the atoi() to obtain n...)
Thanks

Posted: Sat Jan 17, 2004 5:18 am
by turuthok
Ok, this line shall replace your cin >> n;

gets(chs); n = atoi(chs);

And don't forget to include stdlib.h

-turuthok-

Posted: Sat Jan 17, 2004 3:50 pm
by AaronWu
Thank you,turuthok. :D
I got AC for it now. But if it is not too bothered, would you mind explaining the reason why the online-judge System refuses to accept my code and how this change of code could be accepted again.
:(

Posted: Sat Jan 17, 2004 4:22 pm
by anupam
you can also use

gets(a);
sscanf(a,"%d",&n);

another very easy method.

Posted: Sun Jan 18, 2004 8:50 am
by AaronWu
But what is wrong with the following way to obtain an integer? Aren't they correct?
[cpp]
cin>>n;
or scanf("%d",&n);

[/cpp]

Re: 673Parentheses Balance

Posted: Mon Jan 26, 2004 9:39 am
by LPH
here are part of your code:
AaronWu wrote: [cpp]
cin>>n;
.
.
gets(chs);
[/cpp]
they are correct, but differ from gets(), when you use these two, they will left a '\n' in the input stream('cause they only see the number before it). after then, you use gets(), and it see this '\n' and get it back for the first string, which will be a empty string. since this problem should run when empty string entered, you get "Yes" before your first input every time you run, so it resulted to WA.

using gets() instead of these two method will read in the '\n' (though gets() didn't put it into your string).you can use atoi() or sscanf() to read the number you want from the string just read.

hope these will explain your question :)

Posted: Mon Jan 26, 2004 12:51 pm
by anupam
Just debug with the any known debugger with watch window and you can easily find what the problem is to use scanf and gets together. There is space and endline problems with gets and scan used together.

Posted: Wed Jan 28, 2004 6:13 am
by AaronWu
Thank you very much indeed.I've never noticed that before :oops:

Posted: Thu Jan 29, 2004 4:06 pm
by Zhao Le
I have occurred to the same question.

I can solve when using
first gets();
then atoi();

but it is C like.

I hate to both include iostream and stdio
my question is how can I turn it to C++ iostream when occurred to the multiply input.

673: WA help plzzzz

Posted: Wed Apr 28, 2004 5:37 pm
by Jewel of DIU
My prog is ok. But i have got wa plz help me.

My Input
[c]7
([])
(([()])))
([()[]()])()
)
()
)(
([)][/c]
My Output
[c]Yes
No
Yes
No
Yes
No
No[/c]

Are my output ok? :cry: :cry: :cry:

Posted: Fri May 07, 2004 2:40 pm
by the LA-Z-BOy
hi, your outputs are okay.
some tricky cases to think of:

Code: Select all

3
()()(((
<blank line>
()())))
output is:

Code: Select all

No
Yes
No
thank you.

Thanx

Posted: Sat May 08, 2004 5:35 pm
by Jewel of DIU
Thank u very much for ur help. My program gives Wrong output for ur 1st input. I fixed the prob and got AC.

BTW, I think ur from BUET. Do u know UTTAM? He tells me abt u.

Posted: Mon May 10, 2004 4:23 am
by the LA-Z-BOy
yah, i'm from BUET. i know UTTAM too.
cheers

673 Accepted but so long time. but I think my algorithm is..

Posted: Fri May 14, 2004 9:07 am
by 20717TZ
673 Accepted but so long time. CPU=0:07.137 My God!!!!!
I think my algorithm is so simple and it should execute quickly, but I am wrong.
Why?
help me plzzzzzzz!

[cpp]
#include <stdio.h>
#include <string>
using namespace std;

int fnBalance(string s)
{
int place;

for(int i=0;s.find("[]")!=-1 || s.find("()")!=-1;i++)
{
while((place=s.find("[]"))!=-1)
s.erase(place,2);
while((place=s.find("()"))!=-1)
s.erase(place,2);
}
return s.length()==0?1:0;
}

void main()
{
FILE* fp=fopen("Input.txt","r");
int n; char c; string s;

fscanf(fp,"%d",&n);
c=fgetc(fp);//fgets(cs,MAX,fp);//skip a '\n'
for(int i=0;i<n;i++)
{
s="";//reset
while((c=fgetc(fp))!='\n' && c!=EOF)
s+=c;
printf("%s\n",fnBalance(s)?"Yes":"No");
}
}
[/cpp]