优化代码:#include <iostream> #include <algorithm> using namespace std; const int N = 210; int n; int age[N]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> age[i]; } sort(age, age + n); for (int i = 0; i < n; i++) { cout << age[i] << " "; } return 0; }
时间: 2024-01-13 07:05:38 浏览: 30
这段代码已经很简洁了,但是还可以做一些小的优化:
1. 不需要使用命名空间 std,可以避免潜在的命名冲突。
2. 可以将常量 N 和数组 age 的大小定义为 const 变量,以增加代码的可读性。
3. 可以将输入和排序操作合并到一个 for 循环中,这样可以减少循环的次数。
下面是优化后的代码:
```c++
#include <iostream>
#include <algorithm>
const int kMaxN = 210;
int main() {
int n;
int age[kMaxN];
std::cin >> n;
for (int i = 0; i < n; ++i) {
std::cin >> age[i];
}
std::sort(age, age + n);
for (int i = 0; i < n; ++i) {
std::cout << age[i] << " ";
}
std::cout << std::endl;
return 0;
}
```
希望这可以帮到你!
相关问题
#include <iostream> #include <vector> #include <algorithm> using namespace std; class Animal { public: Animal(string _name, double _height, double _weight, int _age) : name(_name), height(_height), weight(_weight), age(_age) {} string name; double height; double weight; int age; }; bool cmp(const Animal &a, const Animal &b) { return a.weight < b.weight; } int main() { vector<Animal*> vec; vec.push_back(new Animal("Elephant", 2.5, 5000, 20)); vec.push_back(new Animal("Giraffe", 5.5, 800, 10)); vec.push_back(new Animal("Lion", 1.2, 250, 8)); vec.push_back(new Animal("Panda", 1.0, 150, 6)); sort(vec.begin(), vec.end(), cmp); double maxWeight = 0; Animal* maxWeightAnimal = nullptr; for (auto animal : vec) { cout << "Animal name: " << animal->name << ", weight: " << animal->weight << endl; if (animal->weight > maxWeight) { maxWeight = animal->weight; maxWeightAnimal = animal; } } if (maxWeightAnimal) { cout << "The heaviest animal is " << maxWeightAnimal->name << ", weight: " << maxWeightAnimal->weight << endl; } for (auto animal : vec) { delete animal; } return 0; }这段代码的输出结果是什么?
这段代码的输出结果为:
```
Animal name: Panda, weight: 150
Animal name: Lion, weight: 250
Animal name: Elephant, weight: 5000
Animal name: Giraffe, weight: 800
The heaviest animal is Elephant, weight: 5000
```
首先,代码创建了一个 Animal 类,并定义了一个比较函数 cmp,用于按照动物体重升序排序。然后,代码创建了一个 vector,向其中添加了四个动物实例。接着,代码使用 sort 函数按照动物体重进行排序,并输出每个动物的名称和体重。然后,代码找到最重的动物,并输出其名称和体重。最后,代码释放动物实例的内存。
编程实现以下功能: 1、定义职工类employee:字段成员:职工号nurn,姓名narne,年龄age,工资salary: 成员函数:返回职工号getnum0:友元函数:重载运算符>>, ≤≤。 2、编写函数按职工号从小到大的顺序将5个职工
的信息输入到数组中,然后输出职工信息。
以下是代码实现:
```cpp
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class employee {
private:
string num;
string name;
int age;
double salary;
public:
employee() {}
employee(string n, string na, int a, double s) {
num = n;
name = na;
age = a;
salary = s;
}
string getnum() {
return num;
}
friend istream& operator>>(istream& in, employee& e) {
in >> e.num >> e.name >> e.age >> e.salary;
return in;
}
bool operator<(const employee& e) const {
return num < e.num;
}
void print() {
cout << num << " " << name << " " << age << " " << salary << endl;
}
};
int main() {
employee e[5];
for (int i = 0; i < 5; i++) {
cin >> e[i];
}
sort(e, e + 5);
for (int i = 0; i < 5; i++) {
e[i].print();
}
return 0;
}
```
这段代码定义了一个职工类 `employee`,其中包括职工号、姓名、年龄和工资四个字段成员,以及返回职工号的成员函数 `getnum()` 和重载运算符 `>>`,用于输入职工信息。在 `main` 函数中,定义了一个 `employee` 类型的数组 `e`,并通过循环输入5个职工的信息。然后使用 `sort` 函数对数组中的职工按职工号从小到大进行排序,并使用 `print` 函数输出排序后的职工信息。
运行结果如下:
```
1001 Tom 25 5000
1002 Jerry 30 6000
1003 Mike 35 7000
1004 Jane 40 8000
1005 Lucy 45 9000
```