The program reads from the keyboard, on the first line, the natural number n. On the next line reads the string of n natural numbers, sorted in ascending order. From the third line we read a natural number m, and from the fourth we read m natural numbers, representing possible values for x.
Displays, for each of the m numbers read, the minimum position in the initial string.
#include <iostream>
usingnamespace std;
int main() {
int n, m ,i, j, v[10002], w[102], ok = 0;
cin >> n;
for (i = 1; i <=n; i++) {
cin >> v[i];
}
cin >> m;
for (j = 1; j <= m; j++) {
cin >> w[j];
for (i = 1; i <= n; i++) {
if ( v[i] == w[j]) {
cout << i << " ";
}
}
}
return 0;
}
#include <iostream>
#include <vector>
usingnamespace std;
int main() {
vector<int> v;
int n;
cin >> n;
for (int i = 0, x; i < n && cin >> x; ++i)
v.push_back(x);
int m;
cin >> m;
for (int i = 0, j = 0, x; i < m && cin >> x; ++i) {
while (j < n && x > v[j]) ++j;
cout << j + 1 << ' ';
}
cout << '\n';
}