#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int k, n=6, q=3, m = 0;
int a[6] = { 1,2,2,3,3,4 };
while (q--)
{
cin >> k;
//(m=0)
for (int i = 0; i < n; i++)
{
if (a[i] == k)m++;
if (a[i] == k && a[i - 1] != k)cout << i << " ";
if (a[i] == k && a[i + 1] != k)cout << i << endl;
}
if (m == 0) cout << "-1 -1" << endl;
}
return 0;
}
输入样例:3,4,5;
预期答案
3 4
5 5
-1-1
问题:输入一个数组里存在的数后m就不是0了,所以每次while循环都要把m还原为0;