Hi. I want to first get user input of a whole number n. In the next line n-1 times, numbers which are smaller or equal to n are going to be read. and the number which was left out gets printed.
Example:
5
1 5 4 3
Output:2
I was trying to make an algorithm with a vector, because I am actually learning on how to manage them. I know there are many other algorithms, which are much more easy, but in this case I want vectors.
My program:
#include <iostream>
#include <sstream>
usingnamespace std;
int main()
{
// istream &in = cin;
istringstream in( "5\n""1 5 4 3\n" );
int N, num, sum = 0;
in >> N;
for ( int i = 1; i <= N - 1; i++ )
{
in >> num;
sum += num;
}
cout << N * ( N + 1 ) / 2 - sum << '\n';
}
I did it like this, which is essentially identical to your second solution.
But that first one is great. Always thinking about the mathematical solution!