Basic Exercises Finding Integers

Problem Description
Given a sequence of n integers, ask the first occurrence of the integer a in the sequence.


Input format
The first line contains an integer n.


The second line contains n non-negative integers for the given sequence, each number in the sequence is not greater than 10000.


The third line contains an integer a, the number to be searched.


Output format
If a appears in the sequence, output the position of its first occurrence (positions are numbered from 1), otherwise output -1.
Sample Input
6
1 9 4 8 3 9
9
Sample Output
2
Data Size and Convention

1 <= n <= 1000。

#include<cstdio>
#include<iostream>
#include<cmath>

using namespace std;

int main(){
		int n,m,flag=0;
		int a[1001];
		cin>>n;
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		cin>>m;
		for(int i=0;i<n;i++){
			if(a[i]==m){
				flag=1;
				cout<<i+1;
				break;
			}
		}
		if(flag==0)
			cout<<"-1";
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326211751&siteId=291194637