java 实现类似C++结构体

P1563 玩具谜题:https://www.luogu.com.cn/problem/P1563

import java.util.*;

class Node{
    String name = "";
    int direction = 0;
    Node(int direction, String name){
        this.name = name;
        this.direction = direction;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();//小人
        int m = scan.nextInt();//指令条数
        Node[] node = new Node[n];
        for (int i = 0; i < n; i ++){
            node[i] = new Node(scan.nextInt(), scan.next());
        }

        int step = 0;
        for (int i = 0; i < m; i ++){
            int ans = scan.nextInt();
            int num = scan.nextInt();
            if ((ans == 0 && node[step].direction == 0) || (ans == 1 && node[step].direction == 1))
                step = (step + n - num) % n;
            else if ((ans == 0 && node[step].direction == 1) || (ans == 1 && node[step].direction == 0))
                step = (step + num + n) % n;
        }
        System.out.print(node[step].name);
    }
}

P1068 分数线划定

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i ++)
            map.put(scan.nextInt(), scan.nextInt());
        int t = (int) (m * 1.5);
        List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
        //按分数从大到小排序  若分数一样则按学号从小到大排序
        list.sort(new Comparator<Map.Entry<Integer, Integer>>() {
            @Override
            public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                if (o1.getValue() < o2.getValue())
                    return 1;
                else if (o1.getValue()==o2.getValue())
                    return o1.getKey().compareTo(o2.getKey());
                else
                    return -1;
            }
        });
        int num = list.get(t-1).getValue();
        int count = 0;
        for (int i = 0; i < n; i ++){
            if (list.get(i).getValue() >= num)
                count ++;
        }
        System.out.println(num + " " + count);
        for (int i = 0; i < count; i ++){
            System.out.println(list.get(i).getKey() + " " + list.get(i).getValue());
        }
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值