120 - Stacks of Flapjacks

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

junbin
Experienced poster
Posts: 174
Joined: Mon Dec 08, 2003 10:41 am

Post by junbin » Sat Jan 17, 2004 9:26 pm

I think it is possible.. though it shouldn't matter

cfalcon
New poster
Posts: 13
Joined: Fri Dec 19, 2003 12:33 pm

Post by cfalcon » Sat Jan 17, 2004 10:47 pm

thanx.
also about the same problem. it says input is terminated by end of file. so should i loop it like this?

while (!cin.eof())
{
....
....
}

if i do it says compile error.

junbin
Experienced poster
Posts: 174
Joined: Mon Dec 08, 2003 10:41 am

Post by junbin » Sun Jan 18, 2004 5:48 am

Can't help you there.. I don't use cin/cout.. :p

If you decide to use gets or scanf, it's:

while (scanf("%s", buiff) == 1)

or

while (gets(buff))

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Mon Jan 19, 2004 4:52 pm

cfalcon, do you get CE on OJ or on your own computer? Anyway, I recommend using [cpp]while(cin)[/cpp]instead of
[cpp]while(!cin.eof())[/cpp]

cfalcon
New poster
Posts: 13
Joined: Fri Dec 19, 2003 12:33 pm

Post by cfalcon » Tue Jan 20, 2004 2:57 am

hi,
i get compile error on OJ and on my computer the program compiles fine and runs too but it just keeps on repeating the output...so teh output just keeps on getting printed infinitely and never stops.

here is my code:

[cpp]
#include <iostream>
using namespace std;

short di[31];

int flip(short n, short s)
{
n=s+1-n;
short i,j;
for (i=n,j=1; i>j;j++,i--)
{
swap(di[j], di);
}
return 0;
}

int main()
{
short a=0, c=0, j, count=0;
short i, max=0,n;
short cdi[31];
bool sorted;
while (cin)
{
while (cin.peek()!='\n')
{
a++;
cin>>di[a];
cdi[a]=di[a];
}
for (i=1;i<=a;i++)
{
cout<<di;
if (i!=a) cout<<" ";
}
cout<<endl;
for (i=1;i<=a;i++)
for (j=i+1;j<=a;j++)
if (cdi>cdi[j]) swap(cdi,cdi[j]);
c=0;
for (i=1;i<=a;i++)
{
if (di!=cdi){ sorted=false; break;} else sorted = true;
}
short fb=0,ft;
while (!sorted)
{
ft=cdi[a-fb];
fb++;
for (i=1;i<=a;i++)
{
if (di==ft) n=a+1-i;
}

if (n!=a) flip(n, a);
flip(fb,a);

for (i=1;i<=a;i++)
{
if (di!=cdi){ sorted=false; break;} else sorted = true;
}
if (n!=a) cout<<n<<" ";
cout<<fb<<" ";
}
if (sorted) cout<<"0"<<endl;
}
return 0;
}
[/cpp]

thanx a lot. any help would be greatly appreciated.

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity » Wed Jan 21, 2004 4:42 am

Your code:
[cpp]
while (cin){
while (cin.peek()!='\n'){ // this does not remove '\n' from input
a++;
cin>>di[a];
cdi[a]=di[a];
}
.....................
..... output .....
}

[/cpp]
No wonder your code keeps on repeating the output...
The way I read input in this problem:

[cpp]
char buffer[300];
while(gets(buffer)){
solution/output
}
[/cpp]

cfalcon
New poster
Posts: 13
Joined: Fri Dec 19, 2003 12:33 pm

Post by cfalcon » Wed Jan 21, 2004 8:23 am

thanx but i'm still having a slight problem. i want to try to make it work by not taking input as string so i did the following:

[cpp]
while (cin.peek()!='\n')
{
a++;
cin>>di[a];
cdi[a]=di[a];
}
cin.getline(temp, 1, '\n');
[/cpp]
i hoped to take off the '\n' by reading it in. however, when i run this...the first output works fine then from the second time on it just keeps on outputting some weird numbers. and when i try to debug using F10 (msvc++) it just stops at teh while (cin.peek()!='\n') line. plz help. thanx a lot.

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity » Wed Jan 21, 2004 8:03 pm

From C++ reference:
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

Get a line from stream.
Extracts characters from the stream and stores them into successive locations in the array pointed by s.
Characters are extracted until either (n - 1) characters have been extracted, the delimiter (parameter delim or '\n' if not specified) is found, or if the end of file or any error occurs in the input sequence.
If the delimiter is found it is extracted but not not stored. Use get if you don't want this character to be extracted.
An ending null character is automatically appended after the data stored in s.
Note (n - 1);

I do not recommend you to use this kind of input, try to use sstream:

[cpp]#include <iostream>
#include <sstream>
char buffer[500];
istringstream instream;
.....................................
while(gets(buffer)){
instream.clear();
instream.str(buffer);
for(a = 0; instream >> di[a]; a++);
... solution/output ...
}[/cpp]
I hope this will help...

cfalcon
New poster
Posts: 13
Joined: Fri Dec 19, 2003 12:33 pm

Post by cfalcon » Thu Jan 22, 2004 1:57 am

hi,
thanx a lot. now it works perfectly on my computer, but it still says compile error. do you think i have to take input from a file? if so...how would i use gets for that...i can only use file inputs when the command 'cin' is involved. thanx a lot.

cfalcon
New poster
Posts: 13
Joined: Fri Dec 19, 2003 12:33 pm

Post by cfalcon » Thu Jan 22, 2004 1:57 am

hi,
thanx a lot. now it works perfectly on my computer, but it still says compile error. do you think i have to take input from a file? if so...how would i use gets for that...i can only use file inputs when the command 'cin' is involved. thanx a lot.

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity » Thu Jan 22, 2004 2:11 am

If you have a compile error, post error message that you get from the judge. :-?
ps: for all problems posted on this site you should read from standard input stream, not file...

cfalcon
New poster
Posts: 13
Joined: Fri Dec 19, 2003 12:33 pm

Post by cfalcon » Thu Jan 22, 2004 7:39 am

how do i check what error message i got. the judge only says compiler error and memory used and ID number and stuff like that. thanx

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity » Thu Jan 22, 2004 7:44 am

cfalcon wrote:how do i check what error message i got. the judge only says compiler error and memory used and ID number and stuff like that. thanx
Every time I get compile error judge includes error message to his reply. Post your new code - I will submit it and see what the error message is.

cfalcon
New poster
Posts: 13
Joined: Fri Dec 19, 2003 12:33 pm

Post by cfalcon » Fri Jan 23, 2004 6:06 am

[cpp]
#include <iostream>
#include <sstream>
using namespace std;

short di[31];

int flip(short n, short s)
{
n=s+1-n;
short i,j;
for (i=n,j=1; i>j;j++,i--)
{
swap(di[j], di);
}
return 0;
}

int main()
{
short a=0, c=0, j, count=0;
char temp[500];
istringstream instream;
short i, max=0,n;
short cdi[31];
bool sorted;
while(gets(temp))
{
instream.clear();
instream.str(temp);
for(a = 1; instream >> di[a]; a++){
cdi[a]=di[a]; }
a--;
for (i=1;i<=a;i++)
{
cout<<di;
if (i!=a) cout<<" ";
}
cout<<endl;
for (i=1;i<=a;i++)
for (j=i+1;j<=a;j++)
if (cdi>cdi[j]) swap(cdi,cdi[j]);
c=0;
for (i=1;i<=a;i++)
{
if (di!=cdi){ sorted=false; break;} else sorted = true;
}
short fb=0,ft;
while (!sorted)
{
ft=cdi[a-fb];
fb++;
for (i=1;i<=a;i++)
{
if (di==ft) n=a+1-i;
}

if (n!=a) flip(n, a);
flip(fb,a);

for (i=1;i<=a;i++)
{
if (di!=cdi){ sorted=false; break;} else sorted = true;
}
if (n!=a) cout<<n<<" ";
cout<<fb<<" ";
}
if (sorted) cout<<"0"<<endl;
}
return 0;
}
[/cpp]


one thing though...i use the submitting program on this site to submit. i don't know if emailing will make a difference. thanx a lot.

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity » Fri Jan 23, 2004 6:46 am

02235073_24.c: In function `int flip (short int, short int)':
02235073_24.c:14: `swap' undeclared (first use this function)
02235073_24.c:14: (Each undeclared identifier is reported only once for
each function it appears in.)
02235073_24.c: In function `int main ()':
02235073_24.c:27: `gets' undeclared (first use this function)
gets needs <stdio.h>
swap needs <algorithm>
After these corrections you get "Invalid memory reference", which (most likely) mean your code is trying to work with elements out of you arrays limits (negative or greater than 30 indices).
I hope you'll find your error.

Post Reply

Return to “Volume 1 (100-199)”