272 - TEX Quotes
Moderator: Board moderators
-
- New poster
- Posts: 2
- Joined: Sun May 05, 2002 10:12 am
- Location: Guangzhou, China
- Contact:
Problem 272: TeX Quotes
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]
[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
Instead of outputting 1 backquote "`", you should output 2: "``".
That is what I see so far. Try it first.
Good luck!
That is what I see so far. Try it first.
Good luck!
it returns wrong answer?!!!(No272)
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]
"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]
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![:wink:](./images/smilies/icon_wink.gif)
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
![:wink:](./images/smilies/icon_wink.gif)
Problem 272 - Why is it a wrong answer?
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]
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]
-----------------
Jo
Jo
-
- Experienced poster
- Posts: 106
- Joined: Sun Feb 17, 2002 2:00 am
- Location: Seoul, South Korea
- Contact:
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~ : )
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~ : )
I love Problem Solving!!!
![:)](./images/smilies/icon_smile.gif)
![:)](./images/smilies/icon_smile.gif)
![:)](./images/smilies/icon_smile.gif)
![:)](./images/smilies/icon_smile.gif)
Line by line or the whole thing
In this problem you should fetch sentence by sentence, transform and print it or fetch them all and just then transform and print them?
-----------------
Jo
Jo
about the input
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!!
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!!
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]
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]
-----------------
Jo
Jo
-
- New poster
- Posts: 35
- Joined: Sat Jan 05, 2002 2:00 am
- Contact:
-
- New poster
- Posts: 1
- Joined: Tue Oct 14, 2003 5:11 pm
Problem 272 - Why PE?
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]
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]
-
- Experienced poster
- Posts: 136
- Joined: Tue Apr 01, 2003 6:59 am
- Location: Jakarta, Indonesia