我试图学习C ++中的存储桶排序算法,发现以下代码:
// C++ program to sort an array using bucket sort
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// Function to sort arr[] of size n using bucket sort
void bucketSort(float arr[], int n)
{
// 1) Create n empty buckets
vector<float> b[n];
// 2) Put array elements in different buckets
for (int i = 0; i < n; i++)
{
int bi = n * arr[i]; // Index in bucket
b[bi].push_back(arr[i]);
}
// 3) Sort individual …Run Code Online (Sandbox Code Playgroud) 我必须编写一个程序(不使用向量,也不使用结构或OOP)来计算两个多项式的和与乘积。这是我的代码:
//Task 4.19 & 4.20
#include <iostream>
#include <cmath>
double* definePoly(char& symbol, char& coef, int& k)
{
std::cout << "deg(" << symbol << "(x)) = ";
std::cin >> k;
symbol++;
double* a = new double[k];
for (int i = k; i >= 0; i--)
{
std::cout << coef << "_{" << i << "} = ";
std::cin >> a[i];
}
coef++;
return a;
}
void printPoly(int n, double* P, char& symbol)
{
symbol--;
std::cout << symbol << "(x)=";
for (int …Run Code Online (Sandbox Code Playgroud)