KY158 找xC++

发布时间 2024-01-13 15:01:59作者: 神奇的萝卜丝

摆了几天,重新再来学习。

把数据输入数组,然后遍历数组就行了,没什么难度。

#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
    int n;
    while(cin >> n){
        int* A=(int*)malloc(sizeof(int)*n);
        for(int i = 0 ; i < n ; i++){
            cin >> A[i];
        }
        int t = 0 ;
        cin >> t ;
        int i = 0;
        for(; i < n ; i++){
            if(A[i]==t){
                t = i;
                break;
            }
        }
        if(i==n) {
            cout << -1 <<'\n' ;
        }else{
            cout << t << '\n';
        }
    }
    return 0;
}

结果如下: