1、1 现在输入n个数字,以逗号,分开;然后可选择升或者降序排序;按提交键就在另一页面显示按什么 排序,结果为, ,提供resetimport java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.Label;import java.awt.TextField;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.a
2、wt.event.WindowEvent;import java.util.Arrays;public class Test1 extends Frameprivate TextField field = null;private TextField xianshi = null;private Button sheng = null;private Button jiang = null;private Button reset = null;public Test1()super();this.setBounds(200, 300, 400, 100);field = new TextFi
3、eld(20);xianshi = new TextField(20);xianshi.setEditable(false);sheng = new Button(升序);jiang = new Button(降序);reset = new Button(reset);this.setLayout(new FlowLayout();this.add(new Label(请输入数字于,号分隔:);this.add(field);this.add(new Label(排序后为:);this.add(xianshi);this.add(sheng);this.add(jiang);this.add(
4、reset);this.addWindowListener(new WindowAdapter() Overridepublic void windowClosing(WindowEvent e) System.exit(1);super.windowClosing(e););sheng.addMouseListener(new MouseAdapter() Overridepublic void mouseClicked(MouseEvent e) super.mouseClicked(e);int list = getValue();StringBuffer xs = new String
5、Buffer();for (int i=0;i=0;i-) xs.append( + listi);xianshi.setText(xs.toString(););reset.addMouseListener(new MouseAdapter() Overridepublic void mouseClicked(MouseEvent e) super.mouseClicked(e);field.setText();xianshi.setText(););this.setVisible(true);private int getValue() String str = field.getText
6、();int list = null;if(str!= null & str.trim().length()0)String strs = str.split(,);list = new intstrs.length;for (int i = 0; i strs.length; i+) listi = Integer.parseInt(strsi);Arrays.sort(list);return list;public void spilt(String values)String strs = values.split(,);for (int i = 0; i (一千零一拾一元整)输出。i
7、mport java.util.HashMap;import java.util.Map;public class Test2 Map map = new HashMap();private static StringBuffer str = new StringBuffer();public Test2() super();map.put(0, 零);map.put(1, 一);map.put(2, 二);map.put(3, 三);map.put(4, 四);map.put(5, 五);map.put(6, 六);map.put(7, 七);map.put(8, 八);map.put(9,
8、 九);map.put(10, 十);map.put(100, 百);map.put(1000, 千);map.put(10000, 万);map.put(100000000, 亿);public void getString(int monery) if(monery / 1000 = 100000)getString(monery/100000000);str.append(map.get(100000000);monery = monery % 100000000;if(monery / 1000 = 10)getString(monery/10000);str.append(map.g
9、et(10000);monery = monery % 10000;int q = monery / 1000;int b = (monery % 1000) / 100;int s = (monery % 100)/ 10;int g = monery % 10 ;if(q 0)str.append(map.get(q)+map.get(1000);if(b 0 )str.append(map.get(b)+map.get(100);if(s 0 & b = 0 & q0)str.append(map.get(0) +map.get(s)+map.get(10);else if(s 0)st
10、r.append(map.get(s)+map.get(10);if(g 0 & s = 0 & q0 )str.append(map.get(0) + map.get(g);else if(g 0)str.append(map.get(g);public void print()System.out.println(str.toString();public static void main(String args) Test2 test2 = new Test2();test2.getString(344565345);test2.print();3、Java 的通信编程,编程题(或问答)
11、,用JAVA SOCKET编程,读服务器几个字符,再写入本地显示?import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import .ServerSocket;import .Socket;public class Test3 public static void main(String
12、args) ServerSocket ss = null;try ss = new ServerSocket(8888);while(true)Socket socket = ss.accept();System.out.println(socket.getInetAddress();BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream();BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out
13、);bw.write(我是服务器端 你说的是 : + br.readLine();bw.close();br.close();socket.close(); catch (IOException e) e.printStackTrace();finallytry ss.close(); catch (IOException e) e.printStackTrace();import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamR
14、eader;import java.io.OutputStreamWriter;import .Socket;import .UnknownHostException;public class Test4 public static void main(String args) Socket socket;try socket = new Socket(127.0.0.1, 8888);BufferedReader br = new BufferedReader(new InputStreamReader(System.in);BufferedWriter bw = new BufferedW
15、riter(new OutputStreamWriter(socket.getOutputStream();bw.write(br.readLine();br.close();bw.close();socket.close(); catch (UnknownHostException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();4、用JAVA实现一种排序,JAVA类实现序列化的方法(二种)? 如在COLLECTION框架中,实现比较要实现什么样的接口?答:使用的是快速排序:public class Test
16、5 public int partition(Integer array,int low,int high)int temp = arraylow;while(low high) while(low temp)high -;sawp(array, low, high);while(low high & arraylow temp) low +;sawp(array, low, high);return low;private void sawp(Integer array, int low, int high) Integer t = arraylow;arraylow = arrayhigh
17、;arrayhigh = t;public void Qsort(Integer array,int low,int high)if(low high)int piv = partition(array,low,high);Qsort(array,0,piv-1);Qsort(array,piv +1,high);public static void main(String args) Integer array = 5,9,4,16,15,24,2,38;new Test5().Qsort(array,0,array.length - 1);for (int i = 0; i array.l
18、ength; i+) System.out.print( + arrayi);public class User implements Comparable private int id ;private String username;private String password;/相应的setter和getter方法省略Overridepublic int compareTo(User o) if(this.id o.getId()return 1;else if(this.id o.getId()return -1;elsereturn 0;import java.util.Compa
19、rator;public class User2 implements Comparator private int id ;private String username;private String password;/相应的setter和getter方法省略Overridepublic int compare(User2 o1, User2 o2) if(o1.getId() o2.getId()return 1;else if(o1.getId() o2.getId()return -1;elsereturn 0;5、编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字
20、节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。public class SplitString private String SplitStr;private int SplitByte;public SplitString(String str, int bytes) this.SplitStr = str;this.SplitByte = bytes;public void SplitIt() int loopCount;loopCount = (SplitStr.leng
21、th() % SplitByte = 0) ? (SplitStr.length() / SplitByte): (SplitStr.length() / SplitByte + 1);for (int i = 1; i = loopCount; i+) if (i = loopCount) System.out.println(SplitStr.substring(i - 1) * SplitByte,SplitStr.length(); else System.out.println(SplitStr.substring(i - 1) * SplitByte,(i * SplitByte)
22、;public static void main(String args) SplitString ss = new SplitString(test中dd文dsaf中男大3443n中国43中国人 0ewldfls=103, 4);ss.SplitIt();6、JAVA多线程编程。 用JAVA写一个多线程程序,如写四个线程,二个加1,二个对一个变量减一,输出。public class Test6 private static int count = 20;public static void main(String args) Test6 test6 = new Test6();Thread
23、add = new Thread(test6.new myThreadAdd();Thread add2 = new Thread(test6.new myThreadAdd();Thread minus = new Thread(test6.new myThreadMinus();Thread minus2 = new Thread(test6.new myThreadMinus();add.start();minus.start();add2.start();minus2.start();class myThreadAdd implements Runnable Overridepubli
24、c void run() while(true)count + ;System.out.println(这是加线程 + Thread.currentThread() + count = + count);class myThreadMinus implements Runnable Overridepublic void run() while(true) count -;System.out.println(这是减线程 + Thread.currentThread() + count = + count);7、算法程序题: 该公司笔试题就1个,要求在10分钟内作完。 题目如下:用1、2、2、
25、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:4不能在第三位,3与5不能相连。解法1:效率不高(太正确的这个输出的是必须是这6个数字组成的,不能少)public class Test7 /* * 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等,要求: * 4 不能在第三位, 3 与 5 不能相连. */public static void main(String a) long start;System.out.println(结果是:);int co
26、unt = 0;for (start = 122345; start = 543221; start+) String s = String.valueOf(start);if (Validate(s) if (s.indexOf(35) = -1) & (s.indexOf(53) = -1)& (s.charAt(2) != 4) System.out.println(s);count+;System.out.println(最后结果共 + count);public static boolean Validate(String l) int a = new int 0, 0, 0, 0,
27、 0 ;for (int i = 0; i 6; i+) if (l.charAt(i) = 1)a0+;if (l.charAt(i) = 2)a1+;if (l.charAt(i) = 3)a2+;if (l.charAt(i) = 4)a3+;if (l.charAt(i) = 5)a4+;if (a0 = 1 & a1 = 2 & a2 = 1 & a3 = 1 & a4 = 1)return true;elsereturn false;解法2:(太正确的这个输出的是必须是这6个数字组成的,不能少)import java.util.Set;import java.util.TreeSe
28、t;public class Test8 public static Set set = new TreeSet(); public static void perm(char n, int beg, int end) if (beg = end) addNumber(String.valueOf(n); else for (int i = beg; i = end; +i) swap(n, beg, i); perm(n, beg + 1, end); swap(n, beg, i); public static void swap(char n, int x, int y) if (x =
29、 y | nx = ny) return; char temp = nx; nx = ny; ny = temp; public static void addNumber(String str) if (str.charAt(2) = 4 | str.contains(35) | str.contains(53) return; set.add(str); public static void main(String args) char number = new char 1, 2, 2, 3, 4, 5 ; perm(number, 0, number.length - 1); Syst
30、em.out.println(set.size(); int cols = 10; for (String s : set) System.out.print(s + ); if (cols- = 1) System.out.println(); cols = 10; 解法3:真正的解法public class Test9 public static void main(String args) boolean third = true;boolean connect1 = true;boolean connect2 = true;boolean connect3 = true;boolean
31、 connect4 = true;for (int i = 1; i = 5; i+) for (int j = 1; j = 5; j+) connect1 = true;if (i = 3 & j = 5) | (i = 5 & j = 3) connect1 = false;for (int k = 1; k = 5; k+) third = true;connect2 = true;if (k = 3 & j = 5) | (k = 5 & j = 3) connect2 = false;if (k = 4) third = false;for (int m = 1; m = 5; m
32、+) connect3 = true;if (k = 3 & m = 5) | (k = 5 & m = 3) connect3 = false;for (int n = 1; n = 5; n+) connect4 = true;if (n = 3 & m = 5) | (n = 5 & m = 3) connect4 = false;if (third & connect1 & connect2 & connect3& connect4) System.out.println(i + + j + + k + + m+ + n + );解法4:基本思路:1 把问题归结为图结构的遍历问题。实际
33、上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是最后对这6个数字的排列组合结果集。2 显然这个结果集还未达到题目的要求。从以下几个方面考虑: 1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。 2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果 3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。/*解题方案是把相邻问题抽象成一个2维数组,用0与1组成,如0,1或者1,0就表示0与1相邻的时候,如下图: 0 1 1
34、 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 解释:0表示不能相邻,1表示可以相邻(当然你掉转也可以) 1,1,2,2.6,6这些当然为0了,自己与自己又怎可能相邻=。= 另外还需要设计一个boolean数组,标识第几个元素可用(即还没被用来排列) */import java.util.*;public class DepthSearch /要排列的字符串数组private String b = 1,2,2,3,4,5;int n = b.length;private String result =;/判断
35、数组中哪个元素还可用与排列的标志位private boolean visit = new booleann;/用于标识相邻元素相通与否的2维数组private int a = new intnn;/用于存放的排列结果的集合private Set set = new TreeSet();/计算有多少个元素被加入到TreeSet里面private int count;/执行main函数public static void main(String args) new DepthSearch().start();private void start()/初始化2维数组,1表示相连,0表示不通for(i
36、nt i=0;in;i+)for(int j=0;jn;j+)if(i=j)aij=0;elseaij=1;/加入不能相邻元素的限制条件,把2维数组对应该元素设置为0,这里是3与5不能相邻(对应第4个元素和第6个元素)a35=0;a53=0;/表示以第1到第6个元素为第1位的情况进行排列(循环6次)for(int i=0;in;i+)this.sort(i);System.out.println(set中元素为+set.size()+个);System.out.println(TreeSet所过滤掉的元素共有:+(count-set.size()+个);System.out.println(所有排列如下:);for(String s:set)System.out.print(s+ , );private void sort(int startIndex)/被拿了出来的元素设置标志为true,表示不可用,false为可用result = result + bstartIndex;visitstartIndex = true;/当长度等于6的时候,即满足一种排列组合,加到TreeSet里面if(result.length()=6)/用if判断:4不能放在第3位if(result.indexOf(4)!=2)