C++ 代码实现数字降序排序并写入文件
#include
using namespace std;
int main() { int n; cout << '请输入数字的个数:' << endl; cin >> n;
vector<int> nums(n);
cout << '请输入数字:' << endl;
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
sort(nums.rbegin(), nums.rend()); // 递减排序
ofstream outfile('result.txt');
for (int i = 0; i < n; i++) {
outfile << nums[i] << ' ';
}
cout << '结果已写入文件 result.txt' << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/mr2I 著作权归作者所有。请勿转载和采集!