【发布时间】:2019-04-26 19:10:59
【问题描述】:
我输入一个数组,代码应该将该数组乘以(x^2 + 2*A*X + A^A)。
例如,如果A = 1,数组1, 3, 3, 1 应该有答案1, 5, 10, 10, 5, 1。我试图编写代码,但我不知道我应该如何在 C++ 中乘以多项式。
代码
#include "pch.h"
#include <iostream>
int main()
{
int a, n;
std::cout << "Enter the Power n: "; //Power decides the size of array.
std::cin >> n;
std::cout << "Enter the number A: ";
std::cin >> a;
int arr_size = n + 1;
int *array = new int[2 * arr_size];
std::cout << "Enter the array P(X): ";
for (int i = 0; i < arr_size; i++)
{
std::cin >> array[i];
}
for (int i = 0; i < arr_size; i++)
std::cout << array[i] << " ";
const int x = 1;
for (int i = 0; i < arr_size; i++) {
array[i] = array[i] * x ^ 2;
array[i] = array[i] * (2 * a*x);
array[i] = array[i] * (a * a);
std::cout << array[i] << " ";
}
return 0;
}
【问题讨论】:
-
只需将 std::transform() 与函子一起使用即可。
-
^运算符是按位或,而不是求幂。只需将x与自身相乘即可。当然,Aᴬ不等于A * A。 -
哦,请阅读how to ask good questions 和this question checklist。然后请编辑您的问题以实际包含一个问题。目前还不清楚您的问题是什么,您实际上在问什么。
-
我问清楚,多项式如何相乘?有什么好暧昧的?
-
@Someprogrammerdude 这是按位异或,可能是自动更正更改?
标签: c++ arrays c++11 matrix-multiplication vector-multiplication