从键盘上输入软干个数字要求按递减排序并将结果写入指定文件中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/449 著作权归作者所有。请勿转载和采集!