題目分析:
將輸入數據存到數組中,設置一個標志位temp判斷是否找到數據。
源代碼:
[cpp] #include <iostream>
using namespace std;
int main()
{
int n;
while (cin>>n)
{
int a[210] = {0};
int x;
int temp = -1;
for (int i = 0; i < n; i ++)
{
cin>>a[i];
}
cin>>x;
for (int j = 0; j < n; j ++)
{
if (x == a[j])
{
temp = j;
cout<<temp<<endl;
}
}
if (temp == -1)
{
cout<<"-1"<<endl;
}
}
return 0;
}