酷代码 AI
菜单
服务商

C++实现闪点裂隙颜色去重并从大到小排序

```cpp #include <iostream> using namespace std; // 选择排序函数,从大到小排序 void selectionSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { int maxIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] > arr[maxIndex]) { maxIndex = j; } } if (maxIndex != i) { int temp = arr[i]; arr[i] = arr[maxIndex]; arr[maxIndex] = temp; } } } int main() { int n; cin >> n; int colors[100]; for (int i = 0; i < n; i++) { cin >> colors[i]; } // 对颜色数组进行排序 selectionSort(colors, n); // 输出去重后的颜色 cout << colors[0]; for (int i = 1; i < n; i++) { if (colors[i] != colors[i - 1]) { cout << " " << colors[i]; } } cout << endl; return 0; } ``` ### 代码解释 1. **选择排序函数 `selectionSort`**:该函数实现了选择排序算法,将数组从大到小进行排序。它通过不断地选择未排序部分的最大值,并将其与未排序部分的第一个元素交换位置。 2. **主函数 `main`**: - 读取输入的闪点裂隙个数 `n` 和每个闪点裂隙的颜色。 - 调用 `selectionSort` 函数对颜色数组进行排序。 - 输出排序后去重的颜色,从第二个元素开始,如果当前元素与前一个元素不同,则输出该元素。 ######[AI问答 | 462点数解答 | 2026-05-30 19:08:19]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]