好吧,我正在处理数独求解算法和生成,但坚持相当简单的任务.我已经检查过,一个数字是否真的适合行位和列方式.但它让我疯狂的是块检查,即数字是否真的适合3x3块.
它必须足够简单,但我无法真正达到解决方案.简而言之,我想知道矩阵中的位置所属的3x3块.以下是一些断言案例.块号,行号和列号没有索引从0开始.
assert("x( 0, 8 ) === 2");
assert("x( 8, 8 ) === 8");
assert("x( 3, 3 ) === 4");
assert("x( 3, 7 ) === 5");
assert("x( 7, 1 ) === 6");
Run Code Online (Sandbox Code Playgroud)
x( i , j )返回块号,其中i= row和j= col.
我有一个matrix.txt文件,其中有一个以这种方式编写的矩阵:
1 2 3
4 5 6
7 8 9
Run Code Online (Sandbox Code Playgroud)
我需要编写一个小程序,将该文件作为输入,并以与.txt文件相同的方式打印此矩阵.
这意味着当"./a.out matrix.txt"的输出必须与我的.txt文件完全相同时:
1 2 3
4 5 6
7 8 9
Run Code Online (Sandbox Code Playgroud)
我的问题是我所能做的就是这个功能:
void printMatrice(matrice) {
int x = 0;
int y = 0;
for(x = 0 ; x < numberOfLines ; x++) {
printf(" (");
for(y = 0 ; y < numberOfColumns ; y++){
printf("%d ", matrix[x][y]);
}
printf(")\n");
}
}
Run Code Online (Sandbox Code Playgroud)
但这根本不好.
有人有想法吗?
谢谢
我的代码中某处出现错误,我想我正在进入无限循环.基本上我得到一个矩阵和两个索引,i,j,我需要计算[i] [j]周围有多少邻居的值为1或2.
这是我的代码:
int number_of_living_neighbors(matrix mat,int i, int j, int n,int m)
{
int counter=0,row_index=0,column_index=0;
for(row_index=i-1;row_index<=i+1;row_index++)
{
for(column_index=j-1;column_index=j+1;column_index++)
{
if((row_index>=0)&&(row_index<n)&&(column_index>=0)&&(column_index<m))
{
if((mat[row_index][column_index]==1)||(mat[row_index][column_index]==2))
counter++;
if((row_index==i)&&(column_index==j)&&(mat[i][j]==1))
counter--;
}
}
}
printf("The number of living neighbors is %d", counter);
return counter;
}
Run Code Online (Sandbox Code Playgroud)
它什么都不打印.mat是我得到的矩阵,i,j是指针,n是行数,m是列数.
可能重复:
将R中的所有0值替换为NA
离开这个问题.R中是否有类似的功能,x[is.na(x)] <- 0除了它会将矩阵中的每个零都改变为NA?
我是Matlab的新手,我正在尝试创建一个m-by-n矩阵,其中包含指定范围内的数字(即介于-1和1之间).
是否有一个等价函数rand(m, n),我可以自己指定范围,或者我需要明确地创建一堆随机数(如在本答案中描述的那样)并从中创建一个矩阵?
任何指向相关文档等的指针都非常感谢.
我研究了两种不同的方法来为矩阵的元素分配内存
方法n.1
int** matrix = new int*[rows];
for (int i = 0; i < rows; ++i)
matrix[i] = new int[cols];
Run Code Online (Sandbox Code Playgroud)
方法n.2
int** matrix = new int*[rows];
if (rows)
{
matrix[0] = new int[rows * cols];
for (int i = 1; i < rows; ++i)
matrix[i] = matrix[0] + i * cols;
}
Run Code Online (Sandbox Code Playgroud)
我可以弄清楚方法n.1做了什么,但我无法弄清楚究竟应该在方法n.2中做什么if子句(我会在没有它的情况下实现它,并且它不起作用,使用if子句,它...)
编辑:这是一个显示我的问题的代码.为什么加载需要这么长时间(~30秒)?
Codepad拒绝显示输出(超时),所以如果你想运行它,只需自己编译即可.
另外,为什么一旦程序启动就不执行cout <<语句?
您好(请原谅我的英文),我对使用矩阵乘法的python有一个很大的疑问,我创建了一个列表列表并乘以一个缩放矩阵,这就是我所做的,我不能通过索引执行乘法运算问题,我用纸和笔检查它是否有效,我做了一些不好的事情来容纳索引,或者我从一开始就错误地容纳矩阵了?
def main():
if len(sys.argv) > 1:
v = int(sys.argv[1])
else:
print "error python exe:"
print "\tpython <programa.py> <num_vertices>"
A = []
for i in range(v):
A.append([0]*2)
for i in range(v):
for j in range(2):
A[i][j] = input("v: ")
print A
Escala(A)
def Escala(A):
print "Escala"
sx = input("Sx: ")
sy = input("Sy: ")
S = [(sx,0),(0,sy)]
print S
M = mult(S,A)
print M
def mult(m1,m2):
M = zero(len(m1),len(m2[0]))
for i in range(len(m2)):
for j in range(len(m2[0])):
for k in …Run Code Online (Sandbox Code Playgroud) 我有以下代码.它是Matrix类的构造函数.主线程只调用构造函数,然后打印矩阵(我为它创建了一个方法).
public class Matrix{
float [][] mainMatrix;
int rows;
int columns;
public Matrix(){
System.out.printf("\nInput the number of rows \n") ;
String rowR = new Scanner(System.in).next().replace(" ", "").replace("\n", "").toString();
rows = Integer.parseInt(rowR);
System.out.printf("\n Input the number of columns \n");
String columnR = new Scanner(System.in).next().replace(" ", "").replace("\n", "").toString();
columns = Integer.parseInt(columnR);
System.out.printf("Input the rows, seperated by a \" \". Close the row with a \"]\"");
System.out.println("Warning!!! Inproper input will cause the program to crash!");
for(int r = 0; r < rows; r++){ …Run Code Online (Sandbox Code Playgroud) 如何在具有特定条件的矩阵中查找值.例如,
a=[-3.14,2.12,-5,3,6,7];
b=find(a>0)
Run Code Online (Sandbox Code Playgroud)
这将返回具有"> 0"条件的矩阵的索引,即b = 2 4 5 6.
我们是否有任何解决方案可以在该条件下找到矩阵中的实际值,例如返回b = 2.12 3 6 7?
为什么我在我的代码中得到错误 - 我创建了两个方法,randomGen生成一个随机数,matrixGen用随机数创建矩阵.我得到不兼容的类型错误.如果有人可以请我指出正确的方向我可能做错了什么..我还在学习阶段..继承我的代码:
import java.util.Random;
public class sparse{
static int matrix [][] = new int[6][6];
public static int randomGen(){
int rA;
Random r = new Random();
rA = r.nextInt(100);
return rA;
}
public static int[][] matrixGen(){
for(int i=0; matrix[i].length < i; i++){
for(int j=0; matrix[j].length <j; j++){
matrix[i] = matrix[i].randomGen();
matrix[j] = matrix[j].randomGen();
}
}
return matrix[i][j];
}
public static void main(String args[]){
new sparse();
}
}
Run Code Online (Sandbox Code Playgroud)