import java.util.Random;
/**
* Created by Administrator on 2017/9/16.
*/
public class PrintTest {
static int M,N;
static int MN[][] ;
public void print(int startX,int startY){
int col = startY ;
int row = startX;
int rowLength = M - (startY)*2;
int colLength = N - (startX)*2;
for(int i= col;i<N-startX;i++){
System.out.print(MN[row][i]+" ");
col =i;
}
row ++ ;
if(rowLength ==1){
return;
}
for(int i= row ;i<M-startY;i++){
System.out.print(MN[i][col]+" ");
row = i;
}
col--;
if(colLength ==1){
return;
}
for (int i = col; i >= startY; i--) {
System.out.print(MN[row][i] + " ");
col = i;
}
row--;
for(int i= row ;i>startX;i--){
System.out.print(MN[i][col]+" ");
row = i;
}
col ++;
if(rowLength >2 && colLength >2){ //数组已不可再分解
print(row,col);
}
}
public void start() {
print(0,0);
}
public void ready(){
if(M < 1 || N <1)
return;
MN = new int[M][N];
Random random = new Random();
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
int value = random.nextInt(10);
MN[i][j] = value;
System.out.print(value+" ");
}
System.out.println("");
}
System.out.println("print:");
start();
System.out.println("");
System.out.println("--------------------------");
}
public static void main(String args[]){
PrintTest test = new PrintTest();
PrintTest.M = 5;
PrintTest.N = 1;
test.ready();
PrintTest.M = 1;
PrintTest.N = 5;
test.ready();
PrintTest.M = 2;
PrintTest.N = 5;
test.ready();
PrintTest.M = 5;
PrintTest.N = 2;
test.ready();
PrintTest.M = 3;
PrintTest.N = 3;
test.ready();
PrintTest.M = 6;
PrintTest.N = 6;
test.ready();
PrintTest.M = 3;
PrintTest.N = 6;
test.ready();
}
}
import java.util.Arrays;
import java.util.Random;
/**
* Created by Administrator on 2017/9/16.
*/
public class MergeTest {
static class Node{
public Node next;
public int value;
Node(int v){
this.value = v;
}
}
Node mergeLink;//new sort link
Node currentNode; //link current
Node generate(int length){
Random random = new Random();
Node[] nodes = new Node[length];
for(int i= 0;i < nodes.length;i++) {
nodes[i] = new Node(random.nextInt(20));
}
Arrays.sort(nodes,(o1,o2)-> o1.value - o2.value);
for(int i= 0;i < nodes.length-1;i++){
Node currentNode = nodes[i];
Node nextNode = nodes[i+1];
currentNode.next = nextNode;
}
return nodes[0];
}
void sortedMerge(Node nodeA,Node nodeB){
while (nodeA != null && nodeB != null){
while (nodeB != null){
Node nextNode ;
boolean isNextA = false;
if(nodeA.value > nodeB.value){ //A > B
nextNode = new Node(nodeB.value);
nodeB = nodeB.next;
}else{ //A <=B
nextNode = new Node(nodeA.value);
nodeA = nodeA.next;
isNextA = true;
}
appendLink(nextNode);
if(isNextA){
break;
}
}
}
if(nodeA != null){
appendLink(nodeA);
}
if(nodeB != null){
appendLink(nodeB);
}
}
void appendLink(Node nextNode){
if(mergeLink == null){
mergeLink = nextNode;
}else if(mergeLink.next == null){
currentNode = nextNode;
mergeLink.next = currentNode;
} else {
currentNode.next = nextNode;
currentNode = nextNode;
}
}
public static void main(String args[]){
MergeTest test = new MergeTest();
Node nodeA = test.generate(7);
Node nodeB = test.generate(7);
print("nodeA",nodeA);
print("nodeB",nodeB);
test.sortedMerge(nodeA,nodeB);
print("mergeNode",test.mergeLink);
}
static void print(String tip,Node node){
System.out.print(tip+":");
int size = 0;
while (node != null){
System.out.print(node.value+" ");
node = node.next;
size++;
}
System.out.println(" size :"+size);
}
}
PrintTest &&mergeTest
点赞
收藏