链家笔试编程题题解
1.Light(去重即可)
import java.util.HashSet; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub HashSet<Integer> hSet=new HashSet<>(); Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); for(int i=0;i<n;i++) { int k=scanner.nextInt(); for(int j=0;j<k;j++) { int value=scanner.nextInt(); hSet.add(value); } } System.out.println(hSet.size()); } }
2.***(菲波那切数列)
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner=new Scanner(System.in); int h=2; ArrayList<Integer> arrayList=new ArrayList<>(); arrayList.add(1); while(h<=100000) { arrayList.add(h); int d=arrayList.size(); h=arrayList.get(d-1)+arrayList.get(d-2); } while(scanner.hasNext()) { int n=scanner.nextInt(); int ans=0; for(int i=0;i<n;i++) { if(arrayList.get(i)<=n) ans++; else break; } System.out.println(n-ans); } } }
#笔试题目#