2018网易校招笔试题——web前端开发

来源:互联网 发布:阿里云备案号申请 编辑:程序博客网 时间:2024/06/04 08:17

周六同学参加网易校招笔试,她投的前端开发,我也顺便看了两眼,题目并不算很难。第二道编程还和京东校招笔试题一样(虽然我没做出来....)。

我只记得第一题...

然而当时并没有帮我同学调出来.....但是刚才只用了十多分钟就做完了......

所以其实校招笔试题并不难

题目内容大概如下:

有两台机器,第一个机器:投入x枚硬币,可以得到2*x+1枚硬币

第二个机器;投入x枚硬币,可以得到2*x+2枚硬币。

投入0枚硬币也可以转换。

现在小明没有硬币,他想得到n枚硬币,要求设计一个最合适的投币方案,。

用例输入:

n = 100//这里我随便给个值

用例输出:

211212


现在时间比较紧,我直接假设n的取值范围在(0,2147483648]

直接上代码

public class Demo1{private static List<Integer> list = new ArrayList<>();public static void main(String[] args) {Scanner scan = new Scanner(System.in);System.out.println("请输入需要多少枚币:");int n = scan.nextInt();int count = 0;//判断奇偶,若为奇数调用func1(),若为偶数调用func2()while (n > 0){if(n % 2 == 1){n = func1(n);}else {n = func2(n);}count++;}int[] arr = new int[count];for(int i = 0;i < count;i++){arr[i] = list.get(i);}for(int j = count - 1;j >= 0;j--){System.out.print(arr[j]);}}//machine1private static int func1(int n){list.add(1);return (n-1)/2;}//machine2private static int func2(int n){list.add(2);return n/2-1;}}
大概思路:根据需要多少枚硬币来逆向推算,再将使用的机器号保存于集合中,再反向遍历

原创粉丝点击