`

ACM题解

阅读更多
北大poj1664,问题:将m个相同的苹果放入n个相同的盘子中,有多少放法
package pkuACM;

//本题是很简单的递推。
//①最少的盘子放了一个,这样每个盘子至少一个,n个盘子先放上n个,剩下的m-n个可以随便放
//②最少的盘子没有放,这样剩下的n-1个盘子还是随便放m个
import java.util.Scanner;
public class Main1664 {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);
		int num = in.nextInt();
		int i = 0;
		int N, M;
		StringBuffer sb = new StringBuffer();
		while (i < num) {

			M = in.nextInt();
			N = in.nextInt();

			sb.append(cal(M, N) + "\n");
			i++;
		}
		System.out.print(sb);

	}

	private static int cal(int m, int n) {
		if (m < 0)
			return 0;
		if (m == 0 || n == 1)
			return 1;
		return cal(m - n, n) + cal(m, n - 1);

	}

}


求最短非子序列长度,即长度小于这个长度的任意元素组成的都是原序列的子序列(不一定连续)
package pkuACM;

import java.util.Scanner;
public class Main1989 {
	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int K = in.nextInt();
		int i=0;
		int current;
		int[] f = new int[K+1];
		int count=0;
		int num =0;
		while(i<N){
			current = in.nextInt();
			if(f[current]==0){ //f[current]记录的是current出现的次数
				f[current]=1;count++;
			}
			if(count==K){
				count=0;
				num++;
				for(int j=1;j<=K;j++){
					f[j]=0;
				}
			}
			i++;
			
		}
		System.out.println(num+1);
		
		
	}

}


问题3:
Description

N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.

Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.

Given this data, tell FJ the exact ordering of the cows.

Input

* Line 1: A single integer, N

* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.

Output

* Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.
Sample Input

5
1
2
1
0

Sample Output

2
4
5
3
1

package pkuACM;

import java.util.Scanner;

public class Main2182 { // lost cows,只知道牛的前面有多少小于它的,即当前位置的数代表前面的数有多少小于当前数字的,不对????

	public static void main(String[] args) {
		
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int i=1;
		int[] lock = new int[N+1];
		int[] num = new int[N+1];
		int j;
		for(;i<N;i++){
			num[i]=in.nextInt();
		}
		
		for(i=N-1;i>0;i--){
			for(j=1;j<N;j++){
				if(j<=num[i]+1&&lock[j]==1)num[i]++;
				lock[num[i]+1]=1;
			}
		}
		
		for(j=0,i=1;i<=N;i++){
			if(lock[i]==1)j+=i;
			System.out.println((1+N)*N/2-j);
		}
		
		for(i=1;i<N;i++){
			System.out.println(num[i]+1);
		}
	}
}


简单的tcp 套接字编程
服务器端,要指定端口号,便于客户端连接时指定服务器的端口号,建立连接
package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	public static void main(String[] args){
		new Server();
	}
	
	private ServerSocket server;
	private Socket s;
	private BufferedReader in;
	private PrintWriter out;
	public Server(){
		try {
			server = new ServerSocket(8888);
			while(true){
				s = server.accept();
				String remoteIP = s.getInetAddress().getHostAddress();
				String remotePort = ":"+s.getLocalPort();
				System.out.println("a client come in !IP:"+remoteIP+remotePort);
				in = new BufferedReader(new InputStreamReader(s.getInputStream())); //使用reader可以读取字符数据
				String line = in.readLine();
				System.out.println(" client send is :"+line);
				
				out = new PrintWriter(s.getOutputStream(),true);
				out.println("your message has received");
				out.close();
				in.close();
				s.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}


客户端:
package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
	private Socket s;
	private BufferedReader in;
	private PrintWriter out;

	public static void main(String[] args) {
		new Client();
	}

	public Client() {

		try {
			s = new Socket("127.0.0.1", 8888);
			
			BufferedReader line = new BufferedReader(new InputStreamReader(
					System.in));
			out = new PrintWriter(s.getOutputStream(), true);
			out.println(line.readLine());

			in = new BufferedReader(new InputStreamReader(s.getInputStream()));
			out.println(in.readLine());
			out.close();
			in.close();
			s.close();

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}



对象序列化:
package xunLieHua;

import java.io.Serializable;

public class Person implements Serializable {

	String name;
	House preferHouse;
	
	public Person(String name, House preferHouse) {
		this.name = name;
		this.preferHouse = preferHouse;
	}

	public String toString(){
		return this.name+"["+super.toString()+"]"+this.preferHouse;
	}
}


package xunLieHua;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;

public class MyWorld {

	public static void main(String[] args) {

		House h = new House();
		Vector man = new Vector();
		
		man.add(new Person("zhou",h));
		man.add(new Person("jun",h));
		man.add(new Person("mei",h));
		
		System.out.println("man:  "+man);  //输出1
		
		
		try {
			ByteArrayOutputStream buf1 = new ByteArrayOutputStream();
			ObjectOutputStream o1 = new ObjectOutputStream(buf1);
			
			o1.writeObject(man);
			o1.writeObject(man);
			ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
			ObjectOutputStream o2 = new ObjectOutputStream(buf2);
			o2.writeObject(man);
			
			
			ObjectInputStream in1 = new ObjectInputStream(new 	ByteArrayInputStream(buf1.toByteArray()));
			
			ObjectInputStream in2 = new ObjectInputStream(new 	ByteArrayInputStream(buf2.toByteArray()));
			
			Vector man1 = (Vector) in1.readObject();
			Vector man2 = (Vector) in1.readObject();
			Vector man3 = (Vector) in2.readObject();
			
			System.out.println("man1: "+man1);  //输出2
			System.out.println("man2: "+man2);  //输出3
			//输出2,和3相同
			
			System.out.println("man3: "+man3);  //输出4
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}

package xunLieHua;

import java.io.Serializable;

public class House implements Serializable {

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics