本人英语相对来说比较弱,有些实在不知道用哪个单词,就算查出来等第二遍看的时候还是要查单词是什么意思,所以有些果断用汉字。退一步说,我是查实解决这些问题,并不是什么标准答案,但是可以参考。有什么更好的解法,可以互相交流。欢迎批评指正。
1.进制均值
【题目描述】
尽管是CS专业的学生,小B的数学基础很好并对数值计算有着特别的兴趣,喜欢用计算机程序来解决数学问题,现在,他正在玩一个数值变换的游戏。他发现计算机中经常用不同的进制表示一个数,如十进制数123表达为16进制的时只包含两位是7,11(B),用八进制表示为三位数1,7,3,按不同进制表达时,各个位数的和也不同,如上述例子中十六进制和八进制中各位数的和分别是18和11.
小B感兴趣的时,一个数A如果按2到A-1进制表达时,各个位数之和的均值是多少?
样例输入:
5
3
样例输出
7/3
2/1
JAVA代码
package 京东2017秋招笔试真题; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * 一个数A如果按2到A-1进制表达时,各个位数之和的均值是多少? * @author NullChen */ public class 进制均值 { static int haxSum(int n,int a) { int sum = 0; boolean b = true; while(b) { if(n==0) { b = false; } sum += (n%a); n /= a; } return sum; } public static void main(String[] args) { int a = 1; List<Integer> 分母 = new ArrayList<>(); List<Integer> 分子 = new ArrayList<>(); while(a!=0) { a = (new Scanner(System.in)).nextInt(); int sum = 0; for(int i=2;i<a;i++) { sum += 进制均值.haxSum(a, i); } 分母.add(sum); 分子.add(a-2); } for(int i = 0;i<分母.size()-1;i++) { System.out.println(分母.get(i)+"/"+分子.get(i)); } } } 结果: 6 5 6 0 9/4 7/3 9/4
【存在问题】 结果需要化简,也就是需要约分。
2.集合
【问题描述】
给你两个集合,求{A}+{B}
要求合并后的元素从小到大,并且不重复。(Set集合)
样例输入:
1 2
1
2 3
1 2 3
1 2
1
12
样例输出:
1 2 3
1 2
JAVA代码
package 京东2017秋招笔试真题; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; /** * 给两个集合 A B 求A+B 同一个集合中不会有相同的元素。 * (TreeSet) * @author NullChen */ public class 集合 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int dataA = in.nextInt(); int dataB = in.nextInt(); //dataA dataB 分别为两个集合的长度 Set<Integer> setAB = new TreeSet<>(); int[] A = new int[dataA]; int[] B = new int[dataB]; for (int i = 0; i < A.length; i++) { A[i] = in.nextInt(); setAB.add(A[i]); } for (int i = 0; i < B.length; i++) { B[i] = in.nextInt(); setAB.add(B[i]); } for (Integer i : setAB) { System.out.print(i+" "); } } } 结果输出 1 3 5 2 3 6 2 3 5 6
3.通过考试(重点,做到我这个程度,我感觉GG了,里面有修多的syso,如果看的话去掉注释还可以看到过程)。
【问题描述】
小明要参加一场考试,考试一共有n道题目,小明必须作对至少60%的题目才能才能通过考试。考试结束后,小明估算出每题做对的概率,p1,p2,...,pn。求小明通过考试的概率是多少 ?
输入第一行的个数n(1<=n<=100),表示题目的个数,第二行n的整数,p1,p2...pn,表示小明有pi的概率作对第i 道题目。
样例输入:
4
50 50 50 50
样例输出:
0.31250
Hint
JAVA代码
package 排列组合; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** *从指定的一个长度为m的串中,选取n个数,n<=m,输出有多少种组合。 * @author NullChen *举例: *假设 有 1 2 3 4 道题目 根据60%的原则 那么必须至少道题目才有可能及格。 *概率为 0.6,0.4,0.5,0.8 *那么就涉及道排列组合的方式 *假设作对3道 *1 2 3 做对概率为p1 = 0.6*0.4*0.5*(1-0.8) *1 2 4 做对概率为p2 = 0.6*0.4*(1-0.5)*0.8 *1 3 4 做对概率为p3 = 0.6*(1-0.4)*0.5*0.8 *2 3 4 做对概率为p4 = (1-0.6)*0.4*0.5*0.8 *px = p1+p2+p3+p4 *假设做对4道 *1 2 3 4 做对概率为py = 0.6*0.4*0.5*0.8 * *那么做对的概率为 p = px+py; * * - ---- - - -- - - --思路 - -- - - - - - - -- * 求解每种排列组合的概率 采用4个list集合 * list 源数据 不能随便改变 * listCopy 复制list的副本 在此基础上进行计算 * tmpArr 排列组合的集合 也就是做对题目的概率乘积 * listCopy-tmpArr 剩下的集合元素 肯定是做错的 也就是 1-i(做对的概率) */ public class Array { public static double sum = 0d; public static double xj = 1d; private static ArrayList<Integer> tmpArr = new ArrayList<>(); private static List<Integer> list = new ArrayList<>(); private static List<Double> bit = new ArrayList<>(); //m为 list的长度 n为 选取的个数 list为目标串 //从index开始 选取K个值 从arr中 /** * index k arr[1,2,3,4] tmpArr * 第一轮循环的递归 * 0 3 [1] * 1 2 [1,2] * 2 1 [1,2,3] * //当 k = 1时,结束遍历! 打印出来 然后执行 清除最后一个值,再次循环。 * 3 1 [1,2,4] * 第二轮循环的递归 * 1 2 * 2 1 * //当 k = 1时,结束遍历! 打印出来 然后执行 清除最后一个值,再次循环。 * * @param index * @param k * @param arr */ public Array(List<Integer> list,List<Double> bit) { // TODO Auto-generated constructor stub this.list = list; this.bit = bit; } public static void combine(int index,int k,int []arr) { if(k == 1){ for (int i = index; i < arr.length; i++) { //给 tmpArr.add(arr[i]); //System.out.println("创建新的副本:listCopy"); List<Integer> listCopy = new ArrayList<>(list); listCopy.removeAll(tmpArr); for (int j = 0; j < tmpArr.size(); j++) { xj*=bit.get(tmpArr.get(j)-1); } // System.out.println("xj:"+xj); for (int j = 0; j < listCopy.size(); j++) { xj*=(1-bit.get(list.get(j)-1)); } // System.out.println("xj:"+xj); sum+=xj; xj=1d; //System.out.print(tmpArr.toString()+" "+sum); tmpArr.remove((Object)arr[i]); } }else if(k > 1){ for (int i = index; i <= arr.length - k; i++) { tmpArr.add(arr[i]); combine(i + 1,k - 1, arr); tmpArr.remove((Object)arr[i]); } }else{ return; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); //bit 表示每到题目做对的概率 double[] bit = new double[num]; //com对每道题目进行编号 使得概率相等的题目 区分开来 int [] com = new int[num]; /** * 例如: * com[] = {1,2,3,4} * bit[] = {0.5,0.5,0.5,0.5} * 第一道题目的概率为0.5 第二道题目的概率为 0.5 * 如果从4到题目中选出 三道 某一种排列组合是这样的{1 3 4} * 则取出bit中对应位置的元素进行计算 */ for (int i = 0; i < num; i++) { bit[i] = in.nextDouble(); com[i] = i+1; } List<Integer> list = new ArrayList<>(); List<Double> myBit = new ArrayList<>(); for (int i = 0; i < com.length; i++) { list.add(com[i]); myBit.add(bit[i]); } Array a = new Array(list,myBit); double mustRight = 4*0.6; //must 表示必须做对这么道题目才可能及格 int must = (int) (mustRight%6.0==0 ? mustRight:mustRight+1); for (int i = must; i < 5; i++) { a.combine(0, i, com); } System.out.println(String.format("%.5f", sum)); } } 结果: 4 0.5 0.5 0.5 0.5 0.31250
4.异或
【问题描述】求解二进制异或后的二十进制数
package 京东2017秋招笔试真题; import java.util.Scanner; /** * 1000 异或 1000 为 0 * @author NullChen * */ public class 异或者 { static int function(int[] arrayA,int[] arrayB) { for (int i = 0; i < arrayB.length; i++) { if(arrayA[i] == arrayB[i]) { arrayA[i] = 0; }else { arrayA[i] =1; } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arrayA.length; i++) { sb.append(arrayA[i]); } return Integer.valueOf(sb.toString(),2); } public static void main(String[] args) { /** * 既然是定长 那么就用数组 */ Scanner in = new Scanner(System.in); int num = in.nextInt(); int[] arrayA = new int[num]; int[] arrayB = new int[num]; System.out.println("Input First:"); for (int i = 0; i <num ; i++) { int it = in.nextInt(); if(it == 0 || it == 1) { arrayA[i] = it; } } System.out.println("Input Second:"); for (int i = 0; i <num ; i++) { int it = in.nextInt(); if(it == 0 || it == 1) { arrayB[i] = it; } } int result = 异或者.function(arrayA,arrayB); System.out.println(result); } }