zoukankan      html  css  js  c++  java
  • 求笛卡尔积

    import java.util.Scanner;
    
    
    public class Cartesian {
    	public HList list = new HList();
    	public void create() {
    		System.out.print("表的行数和列数: ");
    		Scanner sc = new Scanner(System.in);
    		list.row = sc.nextInt();
    		list.col = sc.nextInt();
    		DList r = new DList();
    		for(int i=0; i<list.row; i++) {
    			System.out.print("第"+(i+1)+"行: ");
    			DList d = new DList();
    			for(int j=0; j<list.col; j++) {
    				d.data[j] = sc.nextInt();	
    			}
    			if(list.next == null) {
    				list.next = d;
    			} else {
    				r.next = d;
    			}
    			r = d;
    		}
    		r.next = null;
    		
    	}
    	
    	public void display() {
    		DList p = list.next;
    		while(p != null) {
    			for(int i=0; i<list.col; i++) {
    				System.out.print(p.data[i]+" ");
    				
    			}
    			System.out.println();
    			p = p.next;
    		}
    	}
    	
    	public void link(Cartesian h1, Cartesian h2) {
    		list.row = 0;
    		list.col = h1.list.col + h2.list.col;
    		DList p = h1.list.next;
    		DList q, r = null;
    		while(p != null) {
    			q = h2.list.next;
    			while(q != null) {
    				DList d = new DList();
    				for(int i=0; i<h1.list.col; i++) {
    					d.data[i] = p.data[i];
    				}
    				for(int i=0; i<h2.list.col; i++) {
    					d.data[i+h1.list.col] = q.data[i];
    				}
    				list.row++;
    				if(list.next == null) {
    					list.next = d;
    				} else {
    					r.next = d;
    				}
    				r = d;
    				q = q.next;
    			}
    			p = p.next;
    		}
    		r.next = null;
    	}
    	
    	public static void main(String[] args) {
    		Cartesian h1 = new Cartesian();
    		System.out.println("表 1:");
    		h1.create();
    		h1.display();
    		Cartesian h2 = new Cartesian();
    		System.out.println("表 2:");
    		h2.create();
    		h2.display();
    		Cartesian h = new Cartesian();
    		h.link(h1, h2);
    		System.out.println("连接结果为:");
    		h.display();
    
    	}
    
    }
    
    class DList {
    	public int[] data = new int[10];
    	public DList next;
    }
    
    class HList {
    	public int row, col;
    	public DList next;
    	
    }
    


    结果:


    表 1:
    表的行数和列数: 3 3
    第1行: 1 2 3
    第2行: 2 3 3
    第3行: 1 1 1
    1 2 3 
    2 3 3 
    1 1 1 
    表 2:
    表的行数和列数: 3 2 
    第1行: 3  5
    第2行: 1 6
    第3行: 3 4
    3 5 
    1 6 
    3 4 
    连接结果为:
    1 2 3 3 5 
    1 2 3 1 6 
    1 2 3 3 4 
    2 3 3 3 5 
    2 3 3 1 6 
    2 3 3 3 4 
    1 1 1 3 5 
    1 1 1 1 6 
    1 1 1 3 4 
    

  • 相关阅读:
    基础之实战猜年龄游戏
    基本运算符与if while详解:
    while循环练习:
    常量与格式化输出练习
    Flask基础(05)-->路由的基本定义
    Flask基础(04)-->相关配置参数
    Flask基础(03)-->创建第一个Flask程序
    Flask基础(02)-->搭建Flask项目虚拟环境
    Flask基础(01)-->Flask框架介绍
    Flask实战第61天:帖子板块过滤显示
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6958211.html
Copyright © 2011-2022 走看看