1.使用递归算法,求1+2+3..100=?
public static int jisuan(int x) { System.out.println("x = " + x); if (x == 1) { return 1; } else { return x + jisuan(x - 1); } }
2.使用递归算法,第一个人10,第二个比第一个大2,求第八个人?
public static int computeAge(int n) { if (n == 1) { return 10; } else { return computeAge(n - 1) + 2; } }
3.有ab两个文件,a 以换行符分割,b以换行或者空格分割,把ab两个文件交替合并到c文件中去.
public static void main(String[] args) throws Exception { File file_a = new File("E:\idea\guanchazhe\src\file\a.txt"); File file_b = new File("E:\idea\guanchazhe\src\file\b.txt"); InputStreamReader isr_a = new InputStreamReader(new FileInputStream(file_a), "UTF-8"); BufferedReader read_a = new BufferedReader(isr_a); InputStreamReader isr_b = new InputStreamReader(new FileInputStream(file_b), "UTF-8"); BufferedReader read_b = new BufferedReader(isr_b); List<String> list_a = new ArrayList<>(); List<String> list_b_temp = new ArrayList<>(); List<String> list_b = new ArrayList<>(); List<String> list_c = new ArrayList<>(); String line; while ((line = read_a.readLine()) != null) { list_a.add(line); } while ((line = read_b.readLine()) != null) { list_b_temp.add(line); } for (String str : list_b_temp) { String[] split = str.split(" "); for (String str_sp : split) { list_b.add(str_sp); } } int size_a = list_a.size(); int size_b = list_b.size(); for (int i = 0; i < (size_a > size_b ? size_a : size_b); i++) { if (i < size_a) { list_c.add(list_a.get(i)); } if (i < size_b) { list_c.add(list_b.get(i)); } } BufferedWriter out_c = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:\idea\guanchazhe\src\file\c.txt", true))); for (String str : list_c) { out_c.write(str); out_c.newLine(); } out_c.flush(); read_a.close(); out_c.close(); }
图书馆有AB两个在线在职,user1,user2订阅A,user3,user4,user1订阅B,使用订阅发布模式使AB在跟新时候,通知1234.
public interface ILibrary { void addObservers(IObserver observers ,String type);//添加观察者 void deleteObservers(IObserver observers,String type);//删除观察者 void notifyAllObservers(String msg,String type);//通知所有的观察者 } public interface IObserver { void update(String msg); } public class Library implements ILibrary { private List<IObserver> listA = new ArrayList<>(); private List<IObserver> listB = new ArrayList<>(); @Override public void addObservers(IObserver observers, String type) { if ("A".equals(type)) { listA.add(observers); } else if ("B".equals(type)) { listB.add(observers); } else { } } @Override public void deleteObservers(IObserver observers, String type) { if ("A".equals(type)) { listA.remove(observers); } else if ("B".equals(type)) { listA.remove(observers); } else { } } @Override public void notifyAllObservers(String msg, String type) { if ("A".equals(type)) { for (IObserver observer : listA) { observer.update(msg); } } else if ("B".equals(type)) { for (IObserver observer : listB) { observer.update(msg); } } else { } } } public class UserA implements IObserver { @Override public void update(String msg) { System.out.println("用户A收到消息:"+msg); } } public class UserB implements IObserver { @Override public void update(String msg) { System.out.println("用户B收到消息:"+msg); } } public class UserC implements IObserver { @Override public void update(String msg) { System.out.println("用户C收到消息:"+msg); } } public class UserD implements IObserver { @Override public void update(String msg) { System.out.println("用户D收到消息:"+msg); } } public static void main(String[] args) { Library library=new Library(); IObserver userA = new UserA(); IObserver userB = new UserB(); IObserver userC = new UserC(); IObserver userD = new UserD(); library.addObservers(userA,"A"); library.addObservers(userB,"A"); library.addObservers(userC,"B"); library.addObservers(userD,"B"); library.addObservers(userA,"B"); library.notifyAllObservers("我发布了新的杂志A","A"); library.notifyAllObservers("我发布了新的杂志B","B"); }