In the recent written test, the programming question encountered such a required input, and it was necessary to input a line of integers, the number of which was unknown, and then separate the integers with spaces, as shown below:
11 22 333 45 62
The code looks like this:
int main()
{
vector<int> inputs;
int n = 0, k=0;
int i;
cin >> i;
inputs.push_back(i);
while (cin.get() != '\n') {
cin >> i;
inputs.push_back(i);
}
system("pause");
return 0;
}
A more complete code is shown below. The programming problem is to find the Kth largest number in a given sequence of integers:
#include<iostream>
#include<vector>
using namespace std;
int find_k_max_nums(vector<int> array, int k){
int res = 0;
sort(array.begin(), array.end(), greater<int>());
res = array[k - 1];
return res;
}
int main()
{
vector<int> inputs;
int n = 0, k=0;
int i;
cin >> i;
inputs.push_back(i);
while (cin.get() != '\n') {
cin >> i;
inputs.push_back(i);
}
cin >> k;
int res = find_k_max_nums(inputs, k);
printf("%d\n", res);
system("pause");
return 0;
}
The result is as follows: