蓝桥基础练习 查找整数 BASIC-5

问题描述

给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个。

输入格式

第一行包含一个整数n。

第二行包含n个非负整数,为给定的数列,数列中的每个数都不大于10000。

第三行包含一个整数a,为待查找的数。

输出格式

如果a在数列中出现了,输出它第一次出现的位置(位置从1开始编号),否则输出-1。

样例输入

6
1 9 4 8 3 9
9

样例输出

2

数据规模与约定

1 <= n <= 1000。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <cmath>
#include <map>

using namespace std;
typedef long long ll;

#define INF 0x7fffffff
const double inf=1e20;
const int maxn=1000+10;
const int mod=1e9+7;
const double pi=acos(-1);

int a[maxn];

int main(){
    int n;
    scanf("%d",&n);
    int m;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    int j=-1;
    scanf("%d",&m);
    for(int i=1;i<=n;i++){
        if(a[i]==m){
            j=i;
            break;
        }
    }
    printf("%d\n",j);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wz-archer/p/12499273.html
今日推荐