Page 2 of 4

Posted: Sun Jan 30, 2005 7:36 pm
by Abednego
You can read a line of input with getline() and then parse the line using a stringstream.
The following program reads lines of input, adds all the numbers on that line and outputs the sum.

Code: Select all

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    string line;
    while( getline( cin, line ) )
    {
        istringstream in( line );
        int sum = 0, x;
        while( in >> x ) sum += x;
        printf( "%d\n", sum );
    }
    return 0;
}

Posted: Sun Jan 30, 2005 9:37 pm
by Zuberul
get the lines by string & learn how to use the C library function strtok()
that will help.

Posted: Mon Jan 31, 2005 12:54 pm
by sqm
Thank you all. I think strtok() is easier and faster way to read.

I've written code which in my opinion is good (I got good answers on my computer) and I submitted it but I got Runtime Error (SIGSEGV).
I think the problem is the way I read data because I deleted all additional code and send only part that should read input. I got SIGSEGV again. Does anybody know what's wrong with it:

char string[255], token[]=" ", *res;
for(int i=0,a,b;i<=e;i++){
gets(string);
res=strtok(string, token);
while(1){
a=atoi(res);
res=strtok(NULL, token);
if(res==NULL) break;
b=atoi(res);
//...
}
}

need more tricky I/O for 10800 plz

Posted: Thu Feb 03, 2005 11:41 pm
by abductor
could u provide me with some more tricky I/O for 10800, I keep getting WA and I can't see y
thanx a lot

10801 - Lift Hopping

Posted: Fri Feb 04, 2005 4:17 pm
by abductor
could u provide me with some more tricky I/O for 10801, I keep getting WA and I can't see y
thanx a lot

Posted: Fri Feb 04, 2005 5:02 pm
by michaelh
me too!

I tried every tricky input i could think of. Including k=0, non stopping elevators, taking elevators up- and downward, ... I'm using a modified dijkstra to add the time when changing elevators. And my program is correct for all example input and previously posted examples in this board.


Michael

Posted: Fri Feb 04, 2005 6:43 pm
by Abednego
The most common mistake people make is to not handle this input correctly:

Code: Select all

3 1
100 10 100
0 1
0 1
0 1
PS. Also, it would be easier if you could keep all the posts for one problem in one thread.

Posted: Fri Feb 04, 2005 8:51 pm
by abductor
I am pretty sure its not the input problem, unless of course input has some blank lines in it coz what I do is:
while( cin >> n >> t )
{
////
.
.
.
/////

for( i = 0; i < n; i++ )
{
cin >> speed;
}

// read end of the line character and get to the next line
getline(cin, s);

// read line by line
for( i = 0; i < n; i++ )
{
// read the whole line
getline(cin, s);

.
.
//////////////////////////////////

I think I am missing something else...

Posted: Tue Feb 08, 2005 11:37 pm
by abductor
anyone can help? any more test cases? please?
thanx

Posted: Wed Feb 09, 2005 12:54 am
by michaelh
You should test whether your graph code can store all edges. That was my mistake and now i got accepted.

Posted: Sun Apr 03, 2005 8:08 pm
by Guest
I've used Dijkstra's algorithm, but always getting WA.
Are there empty lines possibly filled with spaces in arbitrary positions in judge's input?
Please help :(

Posted: Thu Apr 14, 2005 7:17 am
by Abednego
Each line in the input contains at least one integer. Make sure you handle the ground floor correctly. Does your code work when no elevator stops there, when the wrong elevator stops there, when all elevators stop there?

Posted: Thu Apr 14, 2005 8:07 pm
by Guest
Sorry for I forgot to edit my earlier post, I've got AC several days ago.. :D
..Thanks for the reply, anyway.

About I/O

Posted: Sun May 01, 2005 8:38 am
by hts
It is really anoying to read this kind of input in C, I did this:

Code: Select all

c = 0;
while(c != '\n' && scanf("%d%c", &j, &c) == 2)
	acesso[i][j] = 1;
acesso[i][j] = 1; 
But I got WA, I used Dijkstra as well... I think there are spaces left after the last number in each line.

Posted: Sun May 01, 2005 8:48 am
by Abednego
Use gets() or fgets() and then parse the line using scanf. Also, there are no spaces at the end of any line in the judge's input.