京东2016在线编程题

来源:互联网 发布:mysql压缩包如何安装 编辑:程序博客网 时间:2024/05/17 20:28
第一题:
package jd;import java.util.Scanner;/** * Created by fhqplzj on 16-4-13. */public class Election {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        while (scanner.hasNext()) {            int n = scanner.nextInt();            int[] a = new int[n];            for (int i = 0; i < n; i++) {                a[i] = scanner.nextInt();            }            int maxValue;            int maxIndex = -1;            int count = 0;            while (maxIndex != 0) {                maxValue = Integer.MIN_VALUE;                for (int i = 0; i < n; i++) {                    if (a[i] >= maxValue) {                        maxValue = a[i];                        maxIndex = i;                    }                }                if (maxIndex != 0) {                    a[maxIndex]--;                    a[0]++;                    count++;                }            }            System.out.println("count = " + count);        }    }}


第二题:

package jd;import java.util.HashSet;import java.util.Scanner;import java.util.Set;/** * Created by fhqplzj on 16-4-13. */public class TicTacToe {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        while (scanner.hasNext()) {            int[][] a = new int[3][3];            for (int i = 0; i < 3; i++) {                String line = scanner.next();                for (int j = 0; j < line.length(); j++) {                    if (line.charAt(j) == 'x') a[i][j] = 1;                    if (line.charAt(j) == 'o') a[i][j] = 2;                }            }            Set set = new HashSet<>();            int diag1 = 1, diag2 = 1;            int num1 = 0, num2 = 0;            for (int i = 0; i < 3; i++) {                diag1 *= a[i][i];                diag2 *= a[i][2 - i];                int row = 1;                int col = 1;                for (int j = 0; j < 3; j++) {                    row *= a[i][j];                    col *= a[j][i];                    if (a[i][j] == 1) num1++;                    if (a[i][j] == 2) num2++;                }                set.add(row);                set.add(col);            }            set.add(diag1);            set.add(diag2);            //开始判定            if (num1 == num2 || num1 - num2 == 1) {                if (set.contains(1) && set.contains(8)) {                    System.out.println("x");                } else if (set.contains(1)) {                    System.out.println("1 won");                } else if (set.contains(8)) {                    System.out.println("2 won");                } else {                    if (num1 + num2 == 9) {                        System.out.println("draw");                    } else if (num1 == num2) {                        System.out.println("1");                    } else {                        System.out.println("2");                    }                }            } else {                System.out.println("x");            }        }    }}

第三题:

package jd;import java.util.Collections;import java.util.LinkedList;import java.util.List;import java.util.Scanner;import java.util.regex.Pattern;/** * Created by fhqplzj on 16-4-13. */class Row {    int price;    int count;    public Row(int price, int count) {        this.price = price;        this.count = count;    }}public class Stock {    static void fun(List list) {        if (list.size() > 1) {            int i = 0, j = 1;            while (true) {                while (j < list.size() && list.get(i).price == list.get(j).price) {                    list.get(i).count += list.get(j).count;                    list.remove(j);                }                if (j < list.size()) {                    i++;                    j++;                } else {                    break;                }            }        }    }    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int n;        int s;        n = scanner.nextInt();        s = scanner.nextInt();        List buy = new LinkedList<>();        List sell = new LinkedList<>();        for (int i = 0; i < n; i++) {            String type = scanner.next(Pattern.compile("[BS]"));            int price = scanner.nextInt();            int count = scanner.nextInt();            if (type.equals("B")) buy.add(new Row(price, count));            if (type.equals("S")) sell.add(new Row(price, count));        }        Collections.sort(buy, ((o1, o2) -> o2.price - o1.price));        Collections.sort(sell, ((o1, o2) -> o1.price - o2.price));        fun(buy);        fun(sell);        for (int i = 0; i < Math.min(s, buy.size()); i++) {            System.out.println("B " + buy.get(i).price + " " + buy.get(i).count);        }        for (int i = 0; i < Math.min(s, sell.size()); i++) {            System.out.println("S " + sell.get(i).price + " " + sell.get(i).count);        }    }}


0 0
原创粉丝点击