#include
#include
#include<math.h>
#include
using namespace std;
class Solution {
public:
vectorsortedSquares(vector& A)
{
int tmp = 0;
int len = A.size();
for (int i = 0;i < len;i++)
{
A[i] = pow(A[i], 2);
}
sort(A.begin(), A.end());//完全为了节省时间和内存
return A;
}
};
int main()
{
int n = 0;
cout << "please Enter the lenth of the array: ";
cin >> n;
vectorarr(n);
cout << "please input the element of the array: “;
for (int& a : arr)
{
cin >> a;
}
Solution s;
s.sortedSquares(arr);
for (int a : arr)
{
cout << a<<” ";
}
return 0;
}
本文展示了一个使用C++编写的程序,该程序接收用户输入的一组整数,计算每个整数的平方,并将结果排序后输出。涉及的C++知识点包括向量的使用、数组操作、数学函数调用及标准库函数sort的使用。
5562

被折叠的 条评论
为什么被折叠?



