时间限制: 1000 ms 内存限制: 65536 KB
提交数: 67092 通过数: 33035
在一个序列(下标从1开始)中查找一个给定的值,输出第一次出现的位置。
第一行包含一个正整数n,表示序列中元素个数。1 <=n<= 10000。
第二行包含n个整数,依次给出序列的每个元素,相邻两个整数之间用单个空格隔开。元素的绝对值不超过10000。
第三行包含一个整数x,为需要查找的特定值。x的绝对值不超过10000。
若序列中存在x,输出x第一次出现的下标;否则输出-1。
5 2 3 6 7 3 3
2
【代码】
#includeusing namespace std;int main() {int n;cin >> n;int a[n];for (int i = 0; i < n; i++) {cin >> a[i];}int m;cin >> m;int s = 0;int tag = 0;for (int i = 0; i < n; i++) {if (a[i] == m) {tag = i + 1;s++;break;}}if (s == 1) {cout << tag;} else {cout << -1;}return 0; }