| Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
|---|---|---|---|---|---|---|---|---|
| 147956 | 王峻熙 | 寻找第二小的数 | C++ | Accepted | 1 MS | 272 KB | 1117 | 2026-02-09 09:28:39 |
#include <iostream> #include <vector> #include <limits> using namespace std; int findSecondSmallest(vector<int> numbers) { int smallest = numeric_limits<int>::max(); int secondSmallest = numeric_limits<int>::max(); for (int num : numbers) { if (num < smallest) { secondSmallest = smallest; smallest = num; } else if (num < secondSmallest && num != smallest) { secondSmallest = num; } } if (secondSmallest == numeric_limits<int>::max()) { return -1; // 表示不存在第二小的数,这里用 -1 标识,实际输出时会处理为 "NO" } return secondSmallest; } int main() { int C; cin >> C; for (int i = 0; i < C; i++) { int n; cin >> n; vector<int> numbers(n); for (int j = 0; j < n; j++) { cin >> numbers[j]; } int result = findSecondSmallest(numbers); if (result == -1) { cout << "NO" << endl; } else { cout << result << endl; } } return 0; }