top1编程
← 返回题目
题解

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

1 条题解

  • 0
    @ 2026-7-29 0:17:32
    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        int n;
        cin >> n;
        int a[25];
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        int x;
        cin >> x;
        int w = -1;
        // 从最后一个元素往前找
        for (int i = n; i >= 1; i--) {
            if (a[i] == x) {
                w = i;
                break; // 找到就立刻退出循环
            }
        }
        cout << w;
        return 0;
    }
    
    • 1