这样做的目的是在给定10 x 10矩阵内的一组坐标时打印出斑点的大小。它是用Java编写的,练习的重点是使用递归在blob中找到每个1。 (通过向上,向下,向左或向右连接到原始坐标的任何1。
Example:
The Blob
0010010010
0100100101
1001001010
0011110101
0111101010
1001010100
0010101101
0101010010
1010100100
0101001000
The Coordinates
1 1
2 3
5 7
The Output
1
10
3
import java.util.*;
public class BlobsRunner
{
public static void main(String[] args)
{
//test the Blob class to make sure
//it works as intended
Blobs bloop = new Blobs(10, 10);
//call both constructors
Scanner reader = new Scanner(System.in);
System.out.println("Row: ");
int r = reader.nextInt();
System.out.println("Colum: ");
int c = reader.nextInt();
bloop.recur(r, c);
bloop.getBlobCount();
//print the newly instantiated Blob
bloop.toString();
//call methods - print out the size of the blob
//bloop.getBlobCount();
}
}
//next file
import java.util.*;
public class Blobs
{
private int[][] mat; //grid of 1s and 0s
private int count;
private int[][] visited;
public Blobs( int rows, int cols )
{
//set count to 0
count = 0;
//point mat at new mat size rows X cols
mat = new int[rows][cols];
//loop through mat
for(int a = 0; a < rows; a ++)
{
for(int b = 0; b< cols; b++)
{
mat[a][b] = (int)Math.random();
}
}
//fill in mat with 1s and 0s
//use Math.random()
visited = new int[rows][cols];
}
public Blobs( int rows, int cols, String s )//what does s mean?
{
//set count to 0
count = 0;
//point mat at new mat size rows X cols
mat = new int[rows][cols];
for(int a = 0; a < rows; a ++)
{
for(int b = 0; b< cols; b++)
{
mat[a][b] = (int)Math.random();
}
}
visited = new int[rows][cols];
//loop through mat
//load in the 1s and 0s from s
}
public void recur(int r, int c)
{
//add a base case
if(mat[r][c]==0)
{
//return;
}
if(visited[r][c] == 5)//figure out, make sure it doesn't count if it does
{
//return;
}
visited[r][c] = 5;
//mark current pos as visited
count++;
//increase count by 1
//add in 4 recursive calls
//UP
if(mat[r+1][c] == 1 && visited[r+1][c] != 5)
{
recur(r+1, c);
}
//DOWN
if(mat[r-1][c] == 1 && visited[r-1][c] != 5)
{
recur(r-1,c);
}
//LEFT
if(mat[r][c-1] == 1 && visited[r][c-1] != 5)
{
recur(r,c-1);
}
//RIGHT
if(mat[r][c+1] == 1 && visited[r][c+1] != 5)
{
recur(r,c+1);
}
}
public int getBlobCount()
{
//return count
return 0;
}
public String toString()
{
//you will need nested loops
//you will need a local string variable
for(int a = 0; a < mat.length; a++)
{
for(int b = 0; b < mat[a].length; b++)
{
System.out.print(mat[a][b]);
}
System.out.println();
}
return "";
}
}
答案 0 :(得分:0)
def add(self, question, answer):
temp = trivia(question, answer)
if not self.head: # first node
self.head = temp
temp.setNext(self.head) # single node, loop to itself
else: # add node
temp.setNext(self.head.getNext()) # shift loop
self.head.setNext(temp) # insert just after head
函数返回0到1之间的双精度值,因此当您尝试将其转换为Math.random()
时,它变为0。因此,所有矩阵均为0。由于在{{ 1}}发挥其打印10x10 0矩阵的作用。
尝试先添加(int)
,然后再添加recur()
,然后再分配随机数import java.util.Random;
答案 1 :(得分:0)
正如 @Fatih Aslan 所指出的,div
在0.0和1.0之间产生一个双精度值,不包括1.0。解析为int时,Java始终会产生值0。
如果您不想使用Math.random()
库,可以将要从java.util.Random
获得的值乘以要生成的最高和最低随机数之间的差加1。如果希望最小的随机数大于0,则将该数字添加到Math.random()
的输出中。
例如,Math.random()
将产生2到10的随机整数,包括2和10。