Hello,
with the following Input & Output for
484 I found a bug in my C++ program, but I don't unterstand the problem.
_.B._ wrote:Input:
(Note that there are spaces after the numbers).
Output:
My program can handle several white spaces and empty lines between numbers well.
But it seems it can't handle a white space at the very end of the input.
Every time a white space is given at the very end of the input the last read number is read twice... Why?
My Output is:
Code: Select all
-3 1
65535 2
0 2 // <-- WRONG ANSWER
Here is my code:
Code: Select all
/* @JUDGE_ID: 48058YJ 484 C++ " " */
//@BEGIN_OF_SOURCE_CODE
#include <iostream>
#include <map>
#include <vector>
using namespace std;
bool contains(vector<int> numbers, int number)
{
for(vector<int>::iterator i = numbers.begin(); i != numbers.end(); i++)
{
if(number == *i)
{
return true;
}
}
return false;
}
int main(void)
{
int number;
map<int, unsigned int> occurrences;
vector<int> numbers;
while(!cin.eof())
{
cin >> number;
occurrences[number]++;
if(!contains(numbers, number))
{
numbers.push_back(number);
}
}
for(vector<int>::iterator i = numbers.begin(); i != numbers.end(); i++)
{
cout << *i << " " << occurrences[*i] << endl;
}
return 0;
}
//@END_OF_SOURCE_CODE
I hope some one can explain this issue, and/or give a solution.
THX in advance!
EDIT:
Code added...