与图形中的传统矩阵乘法相比,矩阵乘法有什么优点?x=x∗cos(angle)−y∗sin(angle)使用&旋转平面上的点y=x∗sin(angle)+y∗cos(angle)而不是乘以巨大的 3x3 矩阵不是更快吗?
LeetCode 的任务。给定一个二进制数组,有必要在其中找到一个元素之和较大的子数组,并返回相同的和。
def wealth_cust(accounts):
largest_acc = 0
for account in accounts:
current_account = sum(account)
if current_account > largest_acc:
largest_acc = current_account
return largest_acc
似乎一切都应该像这样工作,但是 LeetCode 返回错误
NameError: global name 'Solution' is not defined
ret = Solution().maximumWealth(param_1)
Line 29 in _driver (Solution.py)
_driver()
Line 39 in <module> (Solution.py)
我第一次接触 LeetCode,可能暂时还不太明白如何使用它。
Arden Arden
Asked:
2022-09-29 01:11:35 +0000 UTC
再会。我是一名初学者 Java 程序员,我正在制作一个类似于数组计算器的程序。问题是错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at Main.input_matrix(Main.java:43)
at Main.main(Main.java:17)
我知道这意味着什么,问题是我无法理解它为什么会发生(方法:input_matrix())这是代码:
import java.util.Scanner;
public class Main {
public static int[][][] matrix_1 = new int[1][3][3];
public static int[][] AnswerArray = new int[3][2];
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
input_matrix();
System.out.println("Multiply matrix");
draw_3Array();
Logic();
draw_Answer_Array();
System.out.println("--------------");
sort_in_row();
sort_in_column();
replace_elements_rows_and_columns();
System.out.println("Answer after sort");
System.out.println("--------------");
draw_Answer_Array();
long endTime = System.currentTimeMillis();
System.out.println("Total execution time: " + (endTime - startTime) + "ms");
}
public static int[] input_matrix() {
Scanner count_of_rows = new Scanner(System.in);
Scanner count_of_columns = new Scanner(System.in);
Scanner count_of_elements_in_row = new Scanner(System.in);
int i = count_of_rows.nextInt();
int j = count_of_columns.nextInt();
int k = count_of_elements_in_row.nextInt();
int[][][] matrix_1 = new int[i][j][k];
System.out.println(matrix_1[i][j][k]);
return null;
}
public static void draw_3Array() {
for (int i = 0; i < matrix_1.length; i++) {
for (int j = 0; j < matrix_1[i].length; j++) {
for (int k = 0; k < matrix_1[i][j].length; k++) {
if (j == 0 && k == 0 || j == 1 && k == 0 || j == 2 && k == 0) {
System.out.print("|");
}
System.out.print(matrix_1[i][j][k] + " ");
if (k == 2 || i == 1 && k == 1) {
System.out.println("|");
}
}
}
}
}
public static void draw_Answer_Array() {
for (int i = 0; i < AnswerArray.length; i++) {
for (int j = 0; j < AnswerArray[i].length; j++) {
if (i == 0 && j == 0 || i == 1 && j == 0 || i == 2 && j == 0) {
System.out.print("|");
}
System.out.print(AnswerArray[i][j] + " ");
if (i == 0 && j == 1 || i == 1 && j == 1 || i == 2 && j == 1) {
System.out.println("|");
}
}
}
}
public static void Logic() {
//Перемножение
System.out.println("Answer");
}
public static void sort_in_row() {
//Sort by choosing
boolean isSorted_row = false;
int buf;
while (!isSorted_row) {
isSorted_row = true;
for (int i = 0; i < AnswerArray.length; i++) {
for (int j = 0; j < AnswerArray[i].length - 1; j++) {
if (AnswerArray[i][j] >= AnswerArray[i][j + 1]) {
isSorted_row = false;
buf = AnswerArray[i][j];
AnswerArray[i][j] = AnswerArray[i][j + 1];
AnswerArray[i][j + 1] = buf;
//System.out.println("|" + AnswerArray[i][j] + "|" + AnswerArray[i][j+1] + "|");
}
}
}
}
}
public static void sort_in_column() {
boolean isSorted_column = false;
int buf;
while (!isSorted_column) {
isSorted_column = true;
for (int i = 0; i < AnswerArray.length - 1; i++) {
for (int j = 0; j < AnswerArray[i].length; j++) {
if (AnswerArray[i][j] > AnswerArray[i + 1][j]) {
isSorted_column = false;
buf = AnswerArray[i][j];
AnswerArray[i][j] = AnswerArray[i + 1][j];
AnswerArray[i + 1][j] = buf;
//System.out.println("|" + AnswerArray[i][j] + "|" + AnswerArray[i][j+1] + "|");
}
}
}
}
}
public static void replace_elements_rows_and_columns() {
int help_variable_1 = 0;
for (int i = 0; i < AnswerArray.length - 1; i++) {
for (int j = 0; j < AnswerArray[i].length - 1; j++) {
if (AnswerArray[i][j + 1] > AnswerArray[i + 1][j]) {
help_variable_1 = AnswerArray[i + 1][j];
AnswerArray[i + 1][j] = AnswerArray[i][j + 1];
AnswerArray[i][j + 1] = help_variable_1;
}
}
}
}
}
提前感谢您的回复。祝大家拥有美好的一天。
Vladislav Klimenko
Asked:
2022-08-18 14:53:15 +0000 UTC
给定两个整数矩阵 A 和 B。矩阵 A 由 N 行和 M 列组成,矩阵 B 由 M 行和 P 列组成。需要计算这些矩阵 A B 的乘积 输入 输入文件 INPUT.TXT 的第一行包含三个自然数 N、M 和 P。下面是矩阵 A 和 B 的描述。矩阵 A 由 N 行每个 M 个整数。矩阵 B 由 M 行 P 个数组成。矩阵由空行相互分隔。输入中所有数字的绝对值不超过 100。输出 在输出文件 OUTPUT.TXT 中输出 A B乘积的矩阵。
我的代码:
n, m, p = map(int, input().split())
X = [list(map(int, input().split())) for i in range(n)]
input()
Y = [list(map(int, input().split())) for j in range(m)]
result = [[sum(a * b for a, b in zip(X_row, Y_col)) for Y_col in zip(*Y)] for X_row in X]
for i in range(m):
for j in range(p):
print(result[i][j], end=' ')
print()
输入测试数据:维度 2 2 3
矩阵 A
2 3
-十四
矩阵 B
2 -3 4
3 1 0
结论:
13 -3 8
10 7 -4
我找不到不同大小的矩阵的解决方案,2x2 x 2x3
Danil Ovsyannikov
Asked:
2022-08-27 00:39:15 +0000 UTC
需要实现一个将矩阵乘以向量的函数。矩阵是二维数组,向量是一维的。我已经实现了点乘和矩阵乘法的函数,这是我的代码:
#include <stdio.h>
double dot( double *x, double *y, int n )
{
double res = 0.0;
int i;
for (i = 0; i < n; i++)
{
res += x[i] * y[i];
}
return res;
}
void mxv( double **m, double *v, double *res, int rows, int cols )
{
int i;
for (i = 0; i < rows; i++)
{
res[i] = dot(m[i], v, cols);
}
}
int main()
{
double m[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
double v[3] = {1,2,3};
double res[3] = {0};
mxv((double**)m, v, res, 3, 3);
printf("%f \n", res[0]);
printf("%f \n", res[1]);
printf("%f \n", res[2]);
return 0;
}
结果,出现分段错误。显然,这是由于不正确的指针传递造成的。已经尝试使用以下选项
mxv(m, v, res, 3, 3);
错误仍然发生。告诉我我做错了什么,如何解决它。