为丰富同学们的业余文化生活,某高校学生会创办了3个兴趣小组
(以下称A组,B组,C组)。
每个小组的学生名单分别在【A.txt】,【B.txt】和【C.txt】中。
每个文件中存储的是学生的学号。
由于工作需要,我们现在想知道:
既参加了A组,又参加了B组,但是没有参加C组的同学一共有多少人?
请你统计该数字并通过浏览器提交答案。
注意:答案是一个整数,不要提交任何多余的内容。
笨笨有话说:
哇塞!数字好多啊!一眼望过去就能发现相同的,好像没什么指望。
不过,可以排序啊,要是每个文件都是有序的,那就好多了。
歪歪有话说:
排什么序啊,这么几行数字对计算机不是太轻松了吗?
我看着需求怎么和中学学过的集合很像啊…
package eight; import java.io.*; import java.util.ArrayList; import java.util.List; public class xqxz { public static void main(String[] args) { int count=0; File a=new File("data/C.txt"); File a2=new File("data/A.txt"); File a3=new File("data/B.txt"); List list=new ArrayList<String>(); List list2=new ArrayList<String>(); try { InputStreamReader b=new InputStreamReader(new FileInputStream(a)); BufferedReader br=new BufferedReader(b); String str=br.readLine(); while(str != null){ String [] q=str.split(", "); for(String h : q) { list.add(h); } str=br.readLine(); } //------------------------------------ InputStreamReader b2=new InputStreamReader(new FileInputStream(a2)); BufferedReader br2=new BufferedReader(b2); String str2=br2.readLine(); while(str2 != null){ String [] q=str2.split(", "); for(String h : q) { list2.add(h); } str2=br2.readLine(); } //--------------------------------------- InputStreamReader b3=new InputStreamReader(new FileInputStream(a3)); BufferedReader br3=new BufferedReader(b3); String str3=br3.readLine(); while(str3 != null){ String [] q=str3.split(", "); for(String h : q) { if(list2.contains(h) && !list.contains(h)) count++; list2.add(h); } str3=br3.readLine(); } System.out.println(count); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
以后多多交流
看了那么多博客,就你的能看懂
以后多多交流