2018北邮网研院机试真题

2018北邮网研院机试真题(答案仅供参考)


根据刚机试结束的同学回忆,今年北邮机试题目下,并附上本人解答。(应该有一些细节没考虑,如果发现问题,欢迎留言指正交流
大神就当看个笑话 =。=

Problem A

题目: 类似超市结账,计算购买的商品的总价格。

输入:

第一行为测试数据组数T(0< T <= 10)
每组数据第一行为购买商品的种类n,接下来n行,每行两个数据,第一个为商品价格,第二个为商品数量,价格为实型。

输出:

每一行输出相对应数据的总价值,保留两位小数。

测试数据:
2
2
1.00  2
0.50  2
1
100.0 1
输出:
3.00
100.00

java代码

import java.util.Scanner;
public class Main_01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while(n!=0) {
            float money=0;
            int styles = sc.nextInt();
            for(int i=0;ifloat price = sc.nextFloat();
                int num = sc.nextInt();
                money += price*num; 
            }
            System.out.println(String.format("%.2f", money));
            n--;
        }

    }

}

ProblemB

题目:V字型数列,当且仅当三元组i

输入:

第一行为测试数据组数T
每组数据第一行为该数组的数字个数
接下来一行为数组元素

输出:

输出对应数组中符合v字形数列的个数

测试数据:
2
3
2 1 2
5
2 1 2 1 2
输出:
1
4

java代码

import java.util.Scanner;
public class Main_02 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while(n!=0) {
            int count = 0;
            int num = sc.nextInt();
            int[] a = new int[num];
            for(int i = 0;ifor(int i=1;i1;i++) {
                int x1 = 0, x2 = 0;
                for(int j=0;jif(a[j]>a[i]){
                        x1++;
                    }
                }
                for(int j=i+1;jif(a[j]>a[i]){
                        x2++;
                    }
                }
                count += x1*x2;
            }
            System.out.println(count);
            n--;
        }
    }

}

ProblemC

题目:简单的24点

输入为4个数字,a,b,c,d。若a^b^c^d=24,则称该数组满足24点,其中^可以为+、-、*、/任一个,若不能得到结果24,则称该数组不满足24点。(计算为实型)

输入:

第一行为测试组数t
接下来t行,每一行为四个整数a,b,c,d,测试是否满足24点
1<=a,b,c,d<10000(右区间忘了,反正四个数不为0)

输出

在每一行
若满足,输出YES
不满足,输出NO

java代码

import java.util.*;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Main_03 {
    public static void main(String[] args) throws Exception {
        boolean aim = false;
        double m = 0;
        int y = 0;
        String str = "";
        Object result = 0;
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] flag = {"+", "-", "*", "/"};
        while(n!=0) {
            aim = false;
            int[] a = new int[4];
            for(int i = 0;i<4;i++) {
                a[i] = sc.nextInt();;
            }
            for(int i=0;i<4;i++) {
                for(int j=0;j<4;j++) {
                    for(int k=0;k<4;k++) {
                        str = a[0]+flag[i]+a[1]+flag[j]+a[2]+flag[k]+a[3];
                        result = engine.eval(str);  
                        try {
                            m = (double)result;
                        }catch(Exception e){
                            y = (int)result;
                        }
                        if(y == 24 || (Math.abs(m-24)<0.00001)) {
                            aim = true;
                        }
                    }
                }
            }
            if(aim == true) {
                System.out.println("YES");
            }else {
                System.out.println("NO");
            }   
            n--;
        }

    }

}

ProblemD

题目:最大价值

对于每一个零件都有一个适宜温度区间,[Ri,Ji],当温度t < Ri,零件价值为x,当t>Ji,零件价值为z;当温度适宜,价值为y。且y>x,y>z。此刻,有一恒温箱,可确定温度t。

输入

第一行按顺序分别为 n,x,y,z。0 < n <20000
接下来n行为 每一个零件的适宜温度区间0 < Ri < Ji < 109

输出

确定一个温度值t,在这温度下所有零件的总价值最大,并输出该价值

测试数据
3 1 3 2
1 4
2 5
7 10
输出
7   

java代码

import java.util.*;
public class Main_04 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int x = sc.nextInt();
        int y = sc.nextInt();
        int z = sc.nextInt();
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        int value = Integer.MIN_VALUE;
        int[][] array = new int [n][2];
        for(int i = 0; i < n; i++) {
            array[i][0] = sc.nextInt();
            array[i][1] = sc.nextInt();
            min = Math.min(min, array[i][0]);
            max = Math.max(max, array[i][1]);
        }

        for(int i = min;i <= max; i++) {
            int temp = 0;
            for(int j = 0; j < n; j++) {
                if(i < array[j][0]) {
                    temp += x;
                }else if(i >= array[j][0] && i <= array[j][1]) {
                    temp += y;
                }else {
                    temp += z;
                }
            }
            value = Math.max(value, temp);
        }

        System.out.println(value);
    }

}

版权声明:转载注明 http://blog.csdn.net/birdreamer/article/details/79749068

你可能感兴趣的:(java)