1 条题解

  • 0
    @ 2023-6-11 12:21:11

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    
    
    int main() {
    	int n,a[110],m,x,p = -1;
    	cin>>n;
    	int i;
    	for(i = 0; i < n; i++) {
    		cin>>a[i];
    	}
    	cin>>x;
    	for(i = 0; i < n; i++) {
    		if(a[i] == x){
    			p = i + 1;
    		}
    	}
    	
    	cout<<p;
    }
    
    

    Java :

    import java.util.Scanner;
    public class Main{
    public static void main (String[] args){
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	int[] a = new int[n];
    	for(int i = 0; i < n; i++) {
    		a[i] = sc.nextInt();
    		}
    	int m = sc.nextInt();
    	int p = 0;
    	boolean f = false;
    	for(int i = 0; i < n ; i++) {
    		
    		if(m == a[i]) {
    			p = i + 1;
    			f = true;
    		}
    	}
    	if(f == false) {
    		System.out.print(-1);	
    	}else {
    		System.out.print(p);
    	}
    	
    	
    	
    
    }
    }
    

    Python :

    n = int(input())
    s = input().split()
    k = int(input())
    f = -1
    for i in range(len(s) - 1, -1, -1):
        if int(s[i]) == k:
            f = i + 1
            break
    print(f)
    
    
    • 1

    【入门】最后一次出现该数的位置

    信息

    ID
    2685
    时间
    1000ms
    内存
    16MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者