ACM Question

16 views
Skip to first unread message

bahador saket

unread,
May 28, 2010, 1:46:14 AM5/28/10
to FIT Cafeteria
Dear all

I want to solve this ACM question. The algorithm of solving this
program is so easy but i do not know
how I should read this type of Data from file? because by using "
ifstream " we will get a string.
so how should I separate it?



Question:

Summation is the addition of a set of numbers; the result is their
sum. The "numbers" to be summed may be natural numbers,
complex numbers, matrices, or still more complicated objects.
This problem is simple. Just sum up all the numbers given and
display the required output.
Again, this is an easy question, but you just have to be careful!

INPUT
Each line of input consists of k integers where each non-zero integer
n lies in the range of
-2147483647 ≤ n < 0 and 0 < n ≤ 2,147,483,647
Each line of input is terminated by the integer 0. Also note that 1 ≤
k ≤ 20.

OUTPUT
Each line of output consists of the number of integers in each input
line (excluding the terminating
zero), followed by the sum of them.

Sample Input
1 2 3 4 5 0
-1 1 -1 0
101 0

Sample Output
5 15
3 –1
1 101



Regards,
Bahador Saket

hamid vosough

unread,
May 28, 2010, 5:32:36 AM5/28/10
to fit-ca...@googlegroups.com
Dear Bahador

There are functions such as "atof " or "strtod" that you can use to convert the string input type to appropriate type.

Regrads
Hamid

--- On Fri, 5/28/10, bahador saket <bahado...@gmail.com> wrote:

bahador saket

unread,
May 28, 2010, 11:38:45 PM5/28/10
to fit-ca...@googlegroups.com
Dear Hamid

thank for your answer.
I THINK that for solving the question with atoi() method should spend a long time. it should be a easier way to solve this question.
but i am not sure :) :)

for example:
                 we can get numbers without atoi() in this way:
                
                  ifstream file("1.txt");
                  file>>line[i];             (it will get the number as an integer)

but unfortunately i can not control the lines.
               for example:   in one line we have
                                    23 455 12 1 -1

how can I find I arrived end of the line?  and -1 is the last number?
I wrote this simple program, but it does not work correctly
It is not the solution of the program i wrote it to test my algorithm.

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
   int line[4];
   char ab[1];
   string line1;
   ifstream file("1.txt");
   int i=0;
   int sum;
   while(!file.eof())
   {
        getline(file,line1);
        while(1)
        {
           file>>line[i];

           if(line1[i]==' ' && line1[i+1]==' ')
           {
               break;
           }

           i++;
        }

   }

   cout<<line[0]<<endl;
}

Any help would be must appreciated.

Best Regards,
Bahador Saket

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Dear Hamid

tnx for your answer.
I know , how work these two functions, but exctactly
360.gif

johnsee

unread,
May 29, 2010, 11:44:04 AM5/29/10
to FIT Cafeteria
I'm sure you got this question from our ACM basic training problems.

If I'm not mistaken, the problem probably have stated that it's a
standard input/output process, so there's no need to read from any
file. The numbers stated in the problem are all integers, so just
treat them as integers. (if you think of an input stream, a cin simply
ignores the spaces)


On May 29, 11:38 am, bahador saket <bahador.sa...@gmail.com> wrote:
> Dear Hamid
>
> thank for your answer.
> I THINK that for solving the question with atoi() method should spend a long
> time. it should be a easier way to solve this question.
> but i am not sure :) :)
>
> for example:
>                  we can get numbers without atoi() in this way:
>
>                   ifstream file("1.txt");
>                   file>>line[i];             (it will get the number as an
> integer)
>
> but unfortunately i can not control the lines.
>                for example:   in one line we have
>                                     23 455 12 1 -1
>
> how can I find I arrived end of the line?  and -1 is the last number?
> I wrote this simple program, but it does not work correctly [?][?]
> On Fri, May 28, 2010 at 5:32 PM, hamid vosough <hamidvos...@yahoo.com>wrote:
>
> > Dear Bahador
>
> > There are functions such as "atof " or "strtod" that you can use to convert
> > the string input type to appropriate type.
>
> > Regrads
> > Hamid
>
> > --- On *Fri, 5/28/10, bahador saket <bahador.sa...@gmail.com>* wrote:
> > Post: fit-ca...@googlegroups.com<http://mc/compose?to=fit-ca...@googlegroups.com>
> > Unsubscribe: fit-cafeteri...@googlegroups.com<http://mc/compose?to=fit-cafeteri...@googlegroups.com>
>  360.gif
> < 1KViewDownload

bahador saket

unread,
May 29, 2010, 12:13:23 PM5/29/10
to FIT Cafeteria
Dear Johnsee

yes , I got it from your ACM basic training problems.
I found some solution with getline() and storing in string and convert
it to integer....
but I can remember, you used the file very well and so easy. :)
but really how can use them with cin?
Is it possible for you that show me some examples please?
unfortunately, I could not save your examples in that class.


Best Regards,
Bahador

johnsee

unread,
May 30, 2010, 9:20:06 AM5/30/10
to FIT Cafeteria
As i said, if you need to process lots of numbers, then process them
as numbers. Don't create unnecessary problem by reading strings and
attempt to parse it.

I think it's very easy to use with cin. You just need to keep in mind
that it's an input stream (many people don't understand what's a
stream). So, write your program to continuously get input, as long as
there's input.

Wong Ya Ping

unread,
May 30, 2010, 9:24:09 AM5/30/10
to fit-ca...@googlegroups.com
As john said, just treat them as a sequence of numbers. The fact that the numbers are in different lines does not make it any harder as newlines and spaces are treated as whitespaces and will be ignored by the extraction operator (>>).
--
YP
“If we knew what it was we were doing, it would not be called research, would it?” - Albert Einstein

bahador saket

unread,
May 30, 2010, 3:42:03 PM5/30/10
to fit-ca...@googlegroups.com
Dear all

Finally, I solved this question. Tnx for your helps.
I will attach my program, it is my pleasure if you read it and suggest me better solutions or your ideas.


Best Regards,
Bahador Saket
New folder.rar
Reply all
Reply to author
Forward
0 new messages