矩阵压缩与解压缩

运行界面

在这里插入图片描述

PS:测试时需要自己在目录C:\Users\win10\Desktop 下先建立测试文件。

一、

1.测试对称矩阵的压缩

test1文件的内容:
在这里插入图片描述

界面输入如下:
在这里插入图片描述
下拉组合选择框,选择对称矩阵,点击按钮“压缩”。

运行结果:输出文件1.1.txt文件内容如下:

在这里插入图片描述

上三角矩阵、下三角矩阵、稀疏矩阵的测试方式同上。

代码

//画界面需要的
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
//文件读写需要的
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;



public class yasuo extends JFrame implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4088474481798858796L;
	protected JComboBox<String> juzhentype1;//组合框
	protected JComboBox<String> juzhentype2;//组合框
	private JTextField yuan1field;
	private JTextField yafield;
	private JTextField yuan2field;
	private JTextField jieyafield;
	
	private JButton yabutton;  
    private JButton jiebutton;  
  
  private JPanel leftpanel;   //面板流布局
  private JPanel rightpanel;   
  
  public yasuo()
  {
      super("矩阵压缩软件");
      this.setBounds(500, 400, 800, 350);
      leftpanel=new JPanel();
      rightpanel=new JPanel();
      
      JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftpanel, rightpanel);
      split.setDividerLocation(400);                     //设置水平分隔条的位置
      this.getContentPane().add(split); 
      
      yabutton=new JButton("压缩");
      yabutton.addActionListener(this);
      yabutton.setFont(new Font("宋体",Font.BOLD,20));
      jiebutton=new JButton("解压缩");
      jiebutton.addActionListener(this);
      jiebutton.setFont(new Font("宋体",Font.BOLD,20));
     
      yuan1field= new JTextField(6);
		yuan1field.setHorizontalAlignment(JTextField.LEFT); //设置向左对齐
		yuan1field.setFont(new Font("宋体",Font.BOLD,20));  //设置字体格式
		Label label1=new Label("原文件",Label.LEFT);
	    label1.setFont(new Font("宋体",Font.BOLD,20));
	    
	    yafield= new JTextField(8);
	    yafield.setHorizontalAlignment(JTextField.LEFT); //设置向左对齐
		yafield.setFont(new Font("宋体",Font.BOLD,20));  //设置字体格式
		Label label2=new Label("压缩文件名",Label.LEFT);
	    label2.setFont(new Font("宋体",Font.BOLD,20));
   
	    String type[]= {"对称矩阵","稀疏矩阵","上三角矩阵","下三角矩阵"};
      juzhentype1=new JComboBox<String>(type);
      juzhentype1.addActionListener(this);
      juzhentype2=new JComboBox<String>(type);
      juzhentype2.addActionListener(this);
      
      leftpanel.setLayout(new GridLayout(3,3));
		leftpanel.add(label1);
		leftpanel.add(yuan1field);
		leftpanel.add(label2);
		leftpanel.add(yafield);
      leftpanel.add(juzhentype1);
      leftpanel.add(yabutton);
      
      yuan2field= new JTextField(6);
		yuan2field.setHorizontalAlignment(JTextField.LEFT); //设置向左对齐
		yuan2field.setFont(new Font("宋体",Font.BOLD,20));  //设置字体格式
		Label label3=new Label("原压缩文件",Label.LEFT);
	    label3.setFont(new Font("宋体",Font.BOLD,20));
      
      jieyafield= new JTextField(8);
	    jieyafield.setHorizontalAlignment(JTextField.LEFT); //设置向左对齐
		jieyafield.setFont(new Font("宋体",Font.BOLD,20));  //设置字体格式
		Label label4=new Label("解压缩文件名",Label.LEFT);
	    label4.setFont(new Font("宋体",Font.BOLD,20));
	    
      rightpanel.setLayout(new GridLayout(3,3));
      rightpanel.add(label3);
		rightpanel.add(yuan2field);
		rightpanel.add(label4);
		rightpanel.add(jieyafield);
		rightpanel.add(juzhentype2);
		rightpanel.add(jiebutton);
                     
      //this.setBackground(java.awt.Color.lightGray);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);  
      this.setVisible(true);
  }
  	
	public void actionPerformed(ActionEvent ev) {
		
		//压缩
		if(ev.getSource()==yabutton)
		{
			if(juzhentype1.getSelectedIndex()==0) //对称矩阵
			{
				System.out.println("压缩");
				String yuan1=(yuan1field.getText());
				String ya=(yafield.getText());
				
				//新建文件夹
				File fileout = new File("C:\\Users\\win10\\Desktop", ya+".txt");
				try 
				{
					fileout.createNewFile(); // 创建文件
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
				
				String fileinpath="C:/Users/win10/Desktop/"+yuan1+".txt";
				File filein = new File(fileinpath);
		       

		        InputStreamReader inStream = null;
		        OutputStreamWriter outStream = null;
		        try {
		            inStream = new InputStreamReader(new FileInputStream(filein),
		                    "UTF-8");
		            outStream = new OutputStreamWriter(new FileOutputStream(fileout),
		                    "UTF-8");
		        } catch (UnsupportedEncodingException e) {
		            e.printStackTrace();
		        } catch (FileNotFoundException e) {
		            e.printStackTrace();
		        }

		        BufferedReader reader = new BufferedReader(inStream);
		        BufferedWriter writer = new BufferedWriter(outStream);

		        String result;
		        String numarray[];
		        int n=0;
		        try {
		        	result = reader.readLine();
		        	numarray = result.split(" ");
		        	n=numarray.length;
		        	writer.write(String.valueOf(n));
		        	writer.write(" ");
		        	writer.write(numarray[0]);		 
		        	writer.write(" ");
		        	int i=2;
		            while ((result = reader.readLine()) != null) 
		            {
		            	numarray = result.split(" ");
		            	
		            	for(int j=0;j<i;j++)
		            	{
		            		writer.write(numarray[j]);
				        	writer.write(" ");
		            	}	
		            	i++;
		            }
		            writer.flush(); 
		            reader.close();
		            writer.close();
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            try {
		                if (reader != null) {
		                    reader.close();
		                }
		                if (writer != null) {
		                    writer.close();
		                }
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
		        }
				
			}
			else if(juzhentype1.getSelectedIndex()==1) //稀疏矩阵
			{
				System.out.println("压缩");
				String yuan1=(yuan1field.getText());
				String ya=(yafield.getText());
				
				//新建文件夹
				File fileout = new File("C:\\Users\\win10\\Desktop", ya+".txt");
				try 
				{
					fileout.createNewFile(); // 创建文件
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
				
				String fileinpath="C:/Users/win10/Desktop/"+yuan1+".txt";
				File filein = new File(fileinpath);
		       

		        InputStreamReader inStream = null;
		        OutputStreamWriter outStream = null;
		        try {
		            inStream = new InputStreamReader(new FileInputStream(filein),
		                    "UTF-8");
		            outStream = new OutputStreamWriter(new FileOutputStream(fileout),
		                    "UTF-8");
		        } catch (UnsupportedEncodingException e) {
		            e.printStackTrace();
		        } catch (FileNotFoundException e) {
		            e.printStackTrace();
		        }

		        BufferedReader reader = new BufferedReader(inStream);
		        BufferedWriter writer = new BufferedWriter(outStream);
		        		       
		        String result;
		        String numarray[];
		        LinkedList<String> juz=new LinkedList<String>();
		      
		        int k=0;
		        int n=0,m=0;//n:行,m:列
		        try {
		            while ((result = reader.readLine()) != null) 
		            {
		            	juz.add(result);
		            	n++;
		            }	
		            numarray = juz.getFirst().split(" ");
	            	m=numarray.length;
		        	writer.write(String.valueOf(n));
		        	writer.write(" ");
		        	writer.write(String.valueOf(m));
		        	writer.newLine();
		        	Iterator<String> juzz=juz.descendingIterator();
		        	for(int i=n;i>0;i--)
		        	{
		        		for(int j=0;j<m;j++)
		        		{
		        			if(!numarray[j].equals("0"))
		        			{
		        				writer.write(String.valueOf(i));
		        				writer.write(" ");
		        				writer.write(String.valueOf(j));
		        				writer.write(" ");
		        				writer.write(numarray[j]);
		        				writer.newLine();
		        			}
		        		}
		        		numarray = juzz.next().split(" ");
		        		
		        	}
                  writer.flush(); 
		            reader.close();
		            writer.close();
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            try {
		                if (reader != null) {
		                    reader.close();
		                }
		                if (writer != null) {
		                    writer.close();
		                }
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
		        }
			}
			else if(juzhentype1.getSelectedIndex()==2) //上三角矩阵
			{
				System.out.println("压缩");
				String yuan1=(yuan1field.getText());
				String ya=(yafield.getText());
				
				//新建文件夹
				File fileout = new File("C:\\Users\\win10\\Desktop", ya+".txt");
				try 
				{
					fileout.createNewFile(); // 创建文件
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
				
				String fileinpath="C:/Users/win10/Desktop/"+yuan1+".txt";
				File filein = new File(fileinpath);
		       

		        InputStreamReader inStream = null;
		        OutputStreamWriter outStream = null;
		        try {
		            inStream = new InputStreamReader(new FileInputStream(filein),
		                    "UTF-8");
		            outStream = new OutputStreamWriter(new FileOutputStream(fileout),
		                    "UTF-8");
		        } catch (UnsupportedEncodingException e) {
		            e.printStackTrace();
		        } catch (FileNotFoundException e) {
		            e.printStackTrace();
		        }

		        BufferedReader reader = new BufferedReader(inStream);
		        BufferedWriter writer = new BufferedWriter(outStream);

		        String result;
		        String numarray[];
		        int n=0;
		        try {
		        	result = reader.readLine();
		        	numarray = result.split(" ");
		        	n=numarray.length;
		        	writer.write(String.valueOf(n));
		        	writer.write(" ");
		        	writer.write(result);		 
		        	writer.write(" ");
		        	int i=1;
		            while ((result = reader.readLine()) != null) 
		            {
		            	numarray = result.split(" ");
		            	
		            	for(int j=i;j<n;j++)
		            	{
		            		writer.write(numarray[j]);
				        	writer.write(" ");
		            	}	
		            	i++;
		            }
		            writer.flush(); 
		            reader.close();
		            writer.close();
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            try {
		                if (reader != null) {
		                    reader.close();
		                }
		                if (writer != null) {
		                    writer.close();
		                }
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
		        }
			}
			else if(juzhentype1.getSelectedIndex()==3) //下三角矩阵
			{
				System.out.println("压缩");
				String yuan1=(yuan1field.getText());
				String ya=(yafield.getText());
				
				//新建文件夹
				File fileout = new File("C:\\Users\\win10\\Desktop", ya+".txt");
				try 
				{
					fileout.createNewFile(); // 创建文件
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
				
				String fileinpath="C:/Users/win10/Desktop/"+yuan1+".txt";
				File filein = new File(fileinpath);
		       

		        InputStreamReader inStream = null;
		        OutputStreamWriter outStream = null;
		        try {
		            inStream = new InputStreamReader(new FileInputStream(filein),
		                    "UTF-8");
		            outStream = new OutputStreamWriter(new FileOutputStream(fileout),
		                    "UTF-8");
		        } catch (UnsupportedEncodingException e) {
		            e.printStackTrace();
		        } catch (FileNotFoundException e) {
		            e.printStackTrace();
		        }

		        BufferedReader reader = new BufferedReader(inStream);
		        BufferedWriter writer = new BufferedWriter(outStream);

		        String result;
		        String numarray[];
		        int n=0;
		        try {
		        	result = reader.readLine();
		        	numarray = result.split(" ");
		        	n=numarray.length;
		        	writer.write(String.valueOf(n));
		        	writer.write(" ");
		        	writer.write(numarray[0]);		 
		        	writer.write(" ");
		        	int i=2;
		            while ((result = reader.readLine()) != null) 
		            {
		            	numarray = result.split(" ");
		            	
		            	for(int j=0;j<i;j++)
		            	{
		            		writer.write(numarray[j]);
				        	writer.write(" ");
		            	}	
		            	i++;
		            }
		            writer.flush(); 
		            reader.close();
		            writer.close();
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            try {
		                if (reader != null) {
		                    reader.close();
		                }
		                if (writer != null) {
		                    writer.close();
		                }
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
		        }
			}
			
		}
		
		
		//解压缩
		else if(ev.getSource()==jiebutton)
		{
			if(juzhentype2.getSelectedIndex()==0)  //对称矩阵
			{
				System.out.println("解压缩");
				String yuan2=(yuan2field.getText());
				String jieya=(jieyafield.getText());
				
				//新建文件夹
				File fileout = new File("C:\\Users\\win10\\Desktop", jieya+".txt");
				try 
				{
					fileout.createNewFile(); // 创建文件
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
				
				String fileinpath="C:/Users/win10/Desktop/"+yuan2+".txt";
				File filein = new File(fileinpath);
		       

		        InputStreamReader inStream = null;
		        OutputStreamWriter outStream = null;
		        try {
		            inStream = new InputStreamReader(new FileInputStream(filein),
		                    "UTF-8");
		            outStream = new OutputStreamWriter(new FileOutputStream(fileout),
		                    "UTF-8");
		        } catch (UnsupportedEncodingException e) {
		            e.printStackTrace();
		        } catch (FileNotFoundException e) {
		            e.printStackTrace();
		        }

		        BufferedReader reader = new BufferedReader(inStream);
		        BufferedWriter writer = new BufferedWriter(outStream);

		        String result;
		        String numarray[];
		        int n=0;
		        try {
		        	n=reader.read()-'0';
		        	reader.read();
		            while ((result = reader.readLine()) != null) {
		            	numarray = result.split(" "); //numarray存储的第一行居然是个空格。
		            	
		            	
		            	//writer.write(numarray[0]);
		            	for(int i=1;i<=n;i++)
		            	{
		            		for(int j=1;j<=n;j++)
		            		{
		            			if(i<j)
		            			{
		            				writer.write(numarray[j*(j-1)/2+i-1]);	
		            				writer.write(" ");
		            			}
		            			else if(i>=j)
		            			{
		            				writer.write(numarray[i*(i-1)/2+j-1]);	
		            				writer.write(" ");
		            			}
		            		}
		            				            		       		
		            		writer.newLine();		            		
		            	}
		                writer.flush(); 
		            }
		            reader.close();
		            writer.close();
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            try {
		                if (reader != null) {
		                    reader.close();
		                }
		                if (writer != null) {
		                    writer.close();
		                }
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
		        }
				
			}
			else if(juzhentype2.getSelectedIndex()==1) //稀疏矩阵
			{
				System.out.println("解压缩");
				String yuan2=(yuan2field.getText());
				String jieya=(jieyafield.getText());
				
				//新建文件夹
				File fileout = new File("C:\\Users\\win10\\Desktop", jieya+".txt");
				try 
				{
					fileout.createNewFile(); // 创建文件
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
				
				String fileinpath="C:/Users/win10/Desktop/"+yuan2+".txt";
				File filein = new File(fileinpath);
		        InputStreamReader inStream = null;
		        OutputStreamWriter outStream = null;
		        try {
		            inStream = new InputStreamReader(new FileInputStream(filein),
		                    "UTF-8");
		            outStream = new OutputStreamWriter(new FileOutputStream(fileout),
		                    "UTF-8");//GBK
		        } catch (UnsupportedEncodingException e) {
		            e.printStackTrace();
		        } catch (FileNotFoundException e) {
		            e.printStackTrace();
		        }

		        BufferedReader reader = new BufferedReader(inStream);
		        BufferedWriter writer = new BufferedWriter(outStream);

		        String result;
		        
		        int n=0,m=0;
		        try {
		        	n=reader.read()-'0';
		        	reader.read();
		        	m=reader.read()-'0';
		        	reader.readLine();
		        	//System.out.println(n+","+m);
		        	int[][] numarray;
		        	numarray=new int[3][];
		        	String[][] array=new String[n][m];
		        	for(int x=0;x<n;x++)
		        	{
		        		for(int y=0;y<m;y++)
		        		{
		        			array[x][y]="0";
		        		}
		        	}

		        	
		            while ((result = reader.readLine()) != null) 
		            {
		            	
		            	String resultt[]=result.split(" ");
		            	int a=Integer.parseInt(resultt[0]);
		            	int b=Integer.parseInt(resultt[1]);
		            	String c=resultt[2];
		            	array[a][b]=c;
		            	
		            }
		          
		            for(int a=0;a<n;a++)
		            {
		            	for(int b=0;b<m;b++)
		            	{
		            		writer.write(array[a][b]);
	            			writer.write(" ");
		            	}
		            	writer.newLine();
		            }
		            
		            writer.flush();
		            reader.close();
		            writer.close();
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            try {
		                if (reader != null) {
		                    reader.close();
		                }
		                if (writer != null) {
		                    writer.close();
		                }
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
		        }
				
			}
			else if(juzhentype2.getSelectedIndex()==2) //上三角矩阵
			{
				System.out.println("解压缩");
				String yuan2=(yuan2field.getText());
				String jieya=(jieyafield.getText());
				
				//新建文件夹
				File fileout = new File("C:\\Users\\win10\\Desktop", jieya+".txt");
				try 
				{
					fileout.createNewFile(); // 创建文件
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
				
				String fileinpath="C:/Users/win10/Desktop/"+yuan2+".txt";
				File filein = new File(fileinpath);
		       // File fileOut = new File("C:/Users/win10/Desktop/test2.txt");

		        InputStreamReader inStream = null;
		        OutputStreamWriter outStream = null;
		        try {
		            inStream = new InputStreamReader(new FileInputStream(filein),
		                    "UTF-8");
		            outStream = new OutputStreamWriter(new FileOutputStream(fileout),
		                    "UTF-8");
		        } catch (UnsupportedEncodingException e) {
		            e.printStackTrace();
		        } catch (FileNotFoundException e) {
		            e.printStackTrace();
		        }

		        BufferedReader reader = new BufferedReader(inStream);
		        BufferedWriter writer = new BufferedWriter(outStream);

		        String result;
		        String numarray[];
		        int n=0;
		        try {
		        	n=reader.read()-'0';
		        	reader.read();
		            while ((result = reader.readLine()) != null) {
		            	numarray = result.split(" "); //numarray存储的第一行居然是个空格。
		            	int k=0; //记录numarray的下标。
		            	
		            	//writer.write(numarray[0]);
		            	for(int i=0;i<n;i++)
		            	{
		            		int j=0;
		            		while(j<i)
		            		{
		            			writer.write("0");	
		            			writer.write(" ");
		            			j++;
		            		}
		            		while(i<=j&&j<n)
		            		{		            
		            			writer.write(numarray[k]);		            			
		            			writer.write(" ");
			            		k++;j++;
		            		}		            		
		            				            		
		            		writer.newLine();		            		
		            	}
		                writer.flush(); 
		            }
		            reader.close();
		            writer.close();
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            try {
		                if (reader != null) {
		                    reader.close();
		                }
		                if (writer != null) {
		                    writer.close();
		                }
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
		        }
				
			}
			
			else if(juzhentype2.getSelectedIndex()==3)  //下三角矩阵
			{
				System.out.println("解压缩");
				String yuan2=(yuan2field.getText());
				String jieya=(jieyafield.getText());
				
				//新建文件夹
				File fileout = new File("C:\\Users\\win10\\Desktop", jieya+".txt");
				try 
				{
					fileout.createNewFile(); // 创建文件
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
				
				String fileinpath="C:/Users/win10/Desktop/"+yuan2+".txt";
				File filein = new File(fileinpath);

		        InputStreamReader inStream = null;
		        OutputStreamWriter outStream = null;
		        try {
		            inStream = new InputStreamReader(new FileInputStream(filein),
		                    "UTF-8");
		            outStream = new OutputStreamWriter(new FileOutputStream(fileout),
		                    "UTF-8");
		        } catch (UnsupportedEncodingException e) {
		            e.printStackTrace();
		        } catch (FileNotFoundException e) {
		            e.printStackTrace();
		        }

		        BufferedReader reader = new BufferedReader(inStream);
		        BufferedWriter writer = new BufferedWriter(outStream);

		        String result;
		        String numarray[];
		        int n=0;
		        try {
		        	n=reader.read()-'0';
		        	reader.read();
		            while ((result = reader.readLine()) != null) {
		            	numarray = result.split(" "); //numarray存储的第一行居然是个空格。
		            	int k=0; //记录numarray的下标。
		            	
		            	//writer.write(numarray[0]);
		            	for(int i=0;i<n;i++)
		            	{
		            		int j=0;
		            		while(j<=i)
		            		{		            
		            			writer.write(numarray[k]);		            			
		            			writer.write(" ");
			            		k++;j++;
		            		}		            		
		            		while(j>i&&j<n)
		            		{
		            			writer.write("0");	
		            			writer.write(" ");
		            			j++;
		            		}
		            		
		            		writer.newLine();		            		
		            	}
		                writer.flush(); 
		            }
		            reader.close();
		            writer.close();
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            try {
		                if (reader != null) {
		                    reader.close();
		                }
		                if (writer != null) {
		                    writer.close();
		                }
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
		        }
				
				
		      
			}
		}
		
	}
	
	public static void main(String[] args)
	{
		new yasuo();
	}

}

.思路总结

  1. 整体战略

先设计这个UI界面,考虑哪些内容是需要交互的。
我把整个界面一分为二,分别对应两个功能:压缩和解压缩。
每个功能都需要分别对四种类型的矩阵对象进行操作,因此考虑使用组合框以供用户选择压缩类型,这是第一层交互。
压缩对象(即具体处理那个文件)需要用户自由指定,操作完成后生成的文件名也需要用户指定,因此考虑用文本框以供输入文字,这是第二层交互。
界面设计完成,整体的框架也就可以了。剩下的只需要根据用户选择的功能种类和矩阵类型分成八大块,具体在对动作事件的响应里实现就好啦。
2. 重点对象

四种压缩矩阵里,其实可以只当成两种,即稀疏矩阵和对称矩阵。
稀疏矩阵,考虑用三元组进行存储。
上三角矩阵,下三角矩阵可以当成对称矩阵的简化版,这里重点考虑对对称矩阵的操作。
解压缩算法的核心在于:找到二维矩阵中元素下标 (i , j)与一维数组元素下标 k 之间的对应关系。自拟定不同的已知条件就有了不同时间复杂度的算法:

第一种:时间复杂度 O(n^2)

自拟定 i , j 都是已知的。
(解释一下我这里所谓的自拟定的含义:比如一层循环的遍历,其中的渐变量为x,那么我就叫x是自拟定已知的。因为对于x的遍历,对于循环中的每一次来说,x的值是已知的。也就是说,在我的理解里:对于某一个数的循环遍历,就是把这个原本是变量(有许多取值)的数,拆解为多个可能的常量,循环中的任意一次都是对其中某个可能常量的尝试)
即由k 去对应(i , j)时,分别由 i ,j 组成二重循环遍历,去尝试i , j的值,因此时间复杂度是n的平方。

第二种:时间复杂度O(n)
自拟定i 为已知的。
由于:k=1/2*(2n – i)(i – i) + j
证明如下
在这里插入图片描述
所以:j = k - 1/2*(2n – i)(i – 1)
即由k 去对应(i , j)时,由 i 组成一重循环遍历,去尝试i 的值,计算 j 的值,因此时间复杂度是n。

第三种:时间复杂度O(1)

j = k - 1/2*(2n – i)(i – i)

i=∟(n+3/2) - √n^2+n-2*k+9/4 」

即由k 去对应(i , j)时,直接计算 i,j 值。

公式证明如下:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值