Page 1 of 10
Problem 272: TeX Quotes
Posted: Sun May 05, 2002 11:04 am
by Zhichao Li
Could you tell my why I got wrong answers?
[pascal] {$E+,I-,N+,Q-,R-,S-}
var
c:char;
ch:char;
begin
c:='`';
while not(eof(input)) do
begin
read(ch);
if ch='"' then
begin
write(c,c);
if c='`' then c:=''''
else c:='`';
end
else write(ch);
end;
end.[/pascal]
i think i understand why
Posted: Tue Jul 09, 2002 4:45 pm
by ec3_limz
Instead of outputting 1 backquote "`", you should output 2: "``".
That is what I see so far. Try it first.
Good luck!
it returns wrong answer?!!!(No272)
Posted: Sat Oct 05, 2002 3:18 am
by jiangwen
my program returns exactly the right answer in my view:
"To be or not to be," quoth the Bard, "that
is the question".
The programming contestant replied: "I must disagree.
To `C' or not to `C', that is The Question!"
^Z
``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''
Press any key to continue
but the judge-system said it returned wrong answer!
i really can't find out where 's the mistake.
and that's my source file in C++:
[cpp]
#include "iostream.h"
#include "stdio.h"
#include "malloc.h"
void main()
{
struct charlist
{
char alpha;
charlist * next;
};
char ch;
bool lq;
struct charlist* first,*p,*pre;
first=(struct charlist*)malloc(sizeof(charlist));
first->alpha='a';
pre=first;
ch=getchar();
while (true)
{
if (ch=='"')
{
if (lq)
{
ch='`';
lq=!lq;
p=(charlist*)malloc(sizeof(charlist));
p->alpha=ch;
pre->next=p;
p->next=NULL;
pre=p;
p=(charlist*)malloc(sizeof(charlist));
p->alpha=ch;
pre->next=p;
p->next=NULL;
pre=p;
}
else
{
ch='\'';
lq=!lq;
p=(charlist*)malloc(sizeof(charlist));
p->alpha=ch;
pre->next=p;
p->next=NULL;
pre=p;
p=(charlist*)malloc(sizeof(charlist));
p->alpha=ch;
pre->next=p;
p->next=NULL;
pre=p;
}
}
else
{
p=(charlist*)malloc(sizeof(charlist));
p->alpha=ch;
pre->next=p;
p->next=NULL;
pre=p;
}
if (ch==EOF) break;
ch=getchar();
}
p=first->next;
while(p!=NULL)
{
putchar(p->alpha);
p=p->next;
}
pre=first;
p=pre->next;
while (pre!=NULL)
{
p=pre->next;
free(pre);
pre=p;
}
}
[/cpp]
So,please help me,tell me where's the problem?thank you very very very much![/cpp]
Posted: Fri Jan 10, 2003 10:54 am
by Jalal
I dont know why u deals with this components like '/' in this problem.
Its so easy. Just print ` ` for the first " and for the secon print the "
thats all. Just a simple for loop which will count even and odd numbered "
in the problem and for odd it will print ` ` and for the even it will print
"
thats all.
not more than this

Problem 272 - Why is it a wrong answer?
Posted: Tue Apr 22, 2003 10:01 pm
by Sarmento
Can anyone tell me what's the problem with this?????
It's converting all the sentences perfectly.....
Thanks in advance,
joao sarmento
[java]
// @JUDGE_ID: 30701AE 272 Java
import java.io.*;
class Main{
static String ReadLn (int maxLg){
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n'))
break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) || (lg == 1)) return (null); // eof
return (new String (lin, 0, lg));
}
public static void main (String args[]){
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}
void Begin(){
int l;
boolean closing_quote;
String str;
while((str = Main.ReadLn(255)) != null){
l = str.length();
closing_quote = false;
for(int i = 0; i < l;i++){
if (str.charAt(i) == '"'){
if (closing_quote)
str = str.substring(0,i) + "''" + str.substring(i+1);
else
str = str.substring(0,i) + "``" + str.substring(i+1);
closing_quote = !closing_quote;
}
l = str.length();
}
System.out.println(str);
}
}
}[/java]
Posted: Wed Apr 23, 2003 3:01 am
by soyoja
In fact, I don't know about Java code...
But in your program, you read an input string line by line.
I think it's not a good method.
Fix your code that reading one character at a time and converting
character each time. I don't know what type of input data will come in.
So I think using character buffer is so dangerous.
P.S ) It's one of the simplest and easiest problem in valladolod problem set. Cheer up~ : )
Line by line or the whole thing
Posted: Wed Apr 23, 2003 1:48 pm
by Sarmento
In this problem you should fetch sentence by sentence, transform and print it or fetch them all and just then transform and print them?
about the input
Posted: Wed Apr 23, 2003 2:29 pm
by Nick
1. There will be one input and in multiple lines.
2. So one sentence can be in two or more lines.
3. And one line can be more than 255 characters.
4. You always set closing_quote to false each line of input
This means your code will output two "``" s on a parsed sentence
Okay, good luck Sarmento!!
Posted: Wed Apr 23, 2003 4:52 pm
by Sarmento
How do I check for the EOF in java? In C you have '\0', but how about in Java? Is the "\n" too?
Posted: Wed Apr 23, 2003 7:59 pm
by turuthok
When you use System.in.read() and it returns a negative value ... assume it's EOF ...
By the way, '\n' is newline, ... it's not EOF.
-turuthok-
Posted: Wed Apr 23, 2003 8:53 pm
by Sarmento
I decided to try another aproach (more C like), and it was Acc(P.E.).
Thanks
joao sarmento
[java]
import java.io.*;
class Main{
public static void main (String args[]){
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}
void Begin(){
int c = 0;
boolean closing_quote = false;
String opening = "``";
String closing = "\'\'";
while (c>=0){
try{
c = System.in.read();
}
catch(IOException e){
System.out.println(e.getMessage());
System.out.println("Fatal error. Ending Program.");
System.exit(0);
}
if (c == '\"'){
System.out.print(closing_quote?closing:opening);
closing_quote = !closing_quote;
}
else
System.out.print((char) c);
}
}
}[/java]
Posted: Thu Apr 24, 2003 4:30 am
by soyoja
Dear Sarmento.
Congratulations.. But I suggest an opinion.
Please do not post your accepted code...
I think it's no benefit to us ( Especially someone who not solved this
problem yet ). I believe you understand my words.
Thanks.
Posted: Wed May 21, 2003 9:40 am
by Alexander Denisjuk
By the way, if you wish something more complicated, consider my variant of main (I skip details):
[cpp]
int main(){
automat A;
while(cin>>A>>cout);
return 0;
}
[/cpp]

Problem 272 - Why PE?
Posted: Tue Oct 14, 2003 5:20 pm
by acm_beginner
I know this is a very extremely easy problem... I get Accepted but PE. I suppose that reading character by character and only change " to `` or '' would be enough.
Someone can tell me what's wrong with this code?
[cpp]
#include <iostream>
using namespace std;
int main(void) {
char c;
bool flag;
flag=true;
while (cin) {
cin.read(&c,1);
if (c=='"') {
if(flag) {
cout << "``";
} else {
cout << "''";
}
flag=!flag;
} else
cout << c;
}
return 0;
}
[/cpp]
Posted: Wed Oct 15, 2003 5:12 am
by Joseph Kurniawan
try adding a newline character right after the last character;
instead of :
[cpp]
return 0;
[/cpp]
try
[cpp]
cout<<endl;
return 0;
[/cpp]
Hope it helps!!
