Java面試系列17-程式設計題-讀取伺服器字元、實現序列化、計數器、1000階乘、n出列問題等
阿新 • • 發佈:2022-05-04
Server端程式:
package test; import java.net.*; import java.io.*; public class Server{ private ServerSocket ss; private Socket socket; private BufferedReader in; private PrintWriter out; public Server() { try { ss=new ServerSocket(10000); while(true) { socket = ss.accept(); String RemoteIP = socket.getInetAddress().getHostAddress(); String RemotePort = ":"+socket.getLocalPort(); System.out.println("A client come in!IP:"+RemoteIP+RemotePort); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line = in.readLine(); System.out.println("Cleint send is :" + line); out = new PrintWriter(socket.getOutputStream(),true); out.println("Your Message Received!"); out.close(); in.close(); socket.close(); } }catch (IOException e) { out.println("wrong"); } } public static void main(String[] args) { new Server(); } }
Client端程式:
package test; import java.io.*; import java.net.*; public class Client { Socket socket; BufferedReader in; PrintWriter out; public Client() { try { System.out.println("Try to Connect to 127.0.0.1:10000"); socket = new Socket("127.0.0.1",10000); System.out.println("The Server Connected!"); System.out.println("Please enter some Character:"); BufferedReader line = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(socket.getOutputStream(),true); out.println(line.readLine()); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); System.out.println(in.readLine()); out.close(); in.close(); socket.close(); }catch(IOException e) { out.println("Wrong"); } } public static void main(String[] args) { new Client(); } }
二、用JAVA實現一種排序,JAVA類實現序列化的方法(二種)? 如在COLLECTION框架中,實現比較要實現什麼樣的介面?
用插入法進行排序程式碼如下:
package test; import java.util.*; class InsertSort { ArrayList al; public InsertSort(int num,int mod) { al = new ArrayList(num); Random rand = new Random(); System.out.println("The ArrayList Sort Before:"); for (int i=0;i<num ;i++) { al.add(new Integer(Math.abs(rand.nextInt()) % mod + 1)); System.out.println("al["+i+"]="+al.get(i)); } } public void SortIt() { Integer tempInt; int MaxSize=1; for(int i=1;i<al.size();i++) { tempInt = (Integer)al.remove(i); if(tempInt.intValue()>=((Integer)al.get(MaxSize-1)).intValue()) { al.add(MaxSize,tempInt); MaxSize++; System.out.println(al.toString()); } else { for (int j=0;j<MaxSize ;j++ ) { if (((Integer)al.get(j)).intValue()>=tempInt.intValue()) { al.add(j,tempInt); MaxSize++; System.out.println(al.toString()); break; } } } } System.out.println("The ArrayList Sort After:"); for(int i=0;i<al.size();i++) { System.out.println("al["+i+"]="+al.get(i)); } } public static void main(String[] args) { InsertSort is = new InsertSort(10,100); is.SortIt(); } }
三、檔案讀寫,實現一個計數器
public int getNum(){
int i = -1;
try{
String stri="";
BufferedReader in = new BufferedReader(new FileReader(f));
while((stri=in.readLine())!=null){
i = Integer.parseInt(stri.trim());
}
in.close();
}catch(Exception e){
e.printStackTrace();
}
return i;
}
public void setNum(){
int i = getNum();
i++;
try{
PrintWriter out=new PrintWriter(new BufferedWriter(new
FileWriter(f,false)));
out.write(String.valueOf(i));
//可能是編碼的原因,如果直接寫入int的話,將出現java編碼和windows編碼的混亂,因此此處寫入的是String
out.close() ;
}catch(Exception e){
e.printStackTrace();
}
}
四、程式設計題:設有n個人依圍成一圈,從第1個人開始報數,數到第m個人出列,然後從出列的下一個人開始報數,數到第m個人又出列,…,如此反覆到所有的人全部出列為止。設n個人的編號分別為1,2,…,n,打印出出列的順序;要求用java實現。(Core Java)
程式碼如下:
package test;
public class CountGame {
private static boolean same(int[] p,int l,int n){
for(int i=0;i<l;i++){
if(p[i]==n){
return true;
}
}
return false;
}
public static void play(int playerNum, int step){
int[] p=new int[playerNum];
int counter = 1;
while(true){
if(counter > playerNum*step){
break;
}
for(int i=1;i<playerNum+1;i++){
while(true){
if(same(p,playerNum,i)==false) break;
else i=i+1;
}
if(i > playerNum)break;
if(counter%step==0){
System.out.print(i + " ");
p[counter/step-1]=i;
}
counter+=1;
}
}
System.out.println();
}
public static void main(String[] args) {
play(10, 7);
}
}
五、寫一個方法1000的階乘。(C++)
(瞭解一下,變態公司會問C的問題)
C++的程式碼實現如下:
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
class longint {
private:
vector<int> iv;
public:
longint(void) { iv.push_back(1); }
longint& multiply(const int &);
friend ostream& operator<<(ostream &, const longint &);
};
ostream& operator<<(ostream &os, const longint &v) {
vector<int>::const_reverse_iterator iv_iter = v.iv.rbegin();
os << *iv_iter++;
for ( ; iv_iter < v.iv.rend(); ++iv_iter) {
os << setfill('0') << setw(4) << *iv_iter;
}
return os;
}
longint& longint::multiply(const int &rv) {
vector<int>::iterator iv_iter = iv.begin();
int overflow = 0, product = 0;
for ( ; iv_iter < iv.end(); ++iv_iter) {
product = (*iv_iter) * rv;
product += overflow;
overflow = 0;
if (product > 10000) {
overflow = product / 10000;
product -= overflow * 10000;
}
*iv_iter = product;
}
if (0 != overflow) {
iv.push_back(overflow);
}
return *this;
}
int main(int argc, char **argv) {
longint result;
int l = 0;
if(argc==1){
cout << "like: multiply 1000" << endl;
exit(0);
}
sscanf(argv[1], "%d", &l);
for (int i = 2; i <= l; ++i) {
result.multiply(i);
}
cout << result << endl;
return 0;