洛谷【算法1-1】模拟与高精度题单

第三题:玩具谜题
题目:一圈玩具小人有的朝内有的朝外,给定许多指令【往我的哪个方向(左或者右) 走多少位】,因为小人朝向不同,所以有4种情况

在这里插入图片描述
分析:
(1)判断时有四种情况:人朝里向左,人朝里向右,人朝外向左,人朝外向右,而对于一个圈而言,如果不好分析,可以将其平铺成线,也就是变成数组形式,再向左和右各延拓一个周期;
(2)人朝里向左和人朝外向右是一样的,在数组上都是往左移动,反之就是向右移动;如果向左移动,可能会到负数,也就是(curSit - count) < 0, 这时可以加上一个小人总数(周期),再对周期进行取模,如果(curSit - count) < 0,加上一个周期后必定会在数组范围之内,对周期取模则还是自己,如果超过了数组范围,也就是说往右移动,count是大于零的,这时加上一个周期,对周期取模,结果还是对的!

import java.util.LinkedList;

import java.util.Queue;
import java.util.Scanner;

public class Code03_WheresToy2 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
        String[] conf = scan.nextLine().split(" ");
        int people = Integer.parseInt(conf[0]);
        int orderNum = Integer.parseInt(conf[1]);


        SmallPeople[] smallPeople = new SmallPeople[people];

        for (int i = 0; i < people; i++) {
    
    
            String[] peopleString = scan.nextLine().split(" ");
            smallPeople[i] = new SmallPeople(Integer.parseInt(peopleString[0]), peopleString[1]);
        }

        Order[] orders = new Order[orderNum];
        Queue<Order> orders1 = new LinkedList<>();
        for (int i = 0; i < orderNum; i++) {
    
    
            String[] order = scan.nextLine().split(" ");
            orders[i] = new Order(Integer.parseInt(order[0]), Integer.parseInt(order[1]));
            orders1.add(new Order(Integer.parseInt(order[0]), Integer.parseInt(order[1])));
        }


        int curSit = 0;

        int N = orders1.size();
        for (int i = 0; i < N; i++) {
    
    
            Order cur = orders1.poll();
            int dir = 0;
            int count = 0;
            if (cur != null) {
    
    
                dir = cur.dir;
                count = cur.count;
                curSit = (curSit + people + count) % people;
            }
        }


        System.out.println(smallPeople[curSit].name);
    }


    public static class SmallPeople {
    
    
        public int dir;
        public String name;

        public SmallPeople(int dir, String name) {
    
    
            this.dir = dir;
            this.name = name;
        }
    }

    public static class Order {
    
    
        public int dir;
        public int count;

        public Order(int dir, int count) {
    
    
            this.dir = dir;
            this.count = count;
        }
    }
}

四五六、大数问题
java憋说话,吻我~
BigInteger可解决全部大数问题,只不过加减乘除麻烦一些,
在这里插入图片描述
另外还有幂运算pow

七、n*n矩阵的a * a部分矩阵旋转
对于顺时针旋转九十度【2 2 1 0】也就是第二行,第二列,3宽度,顺时针
在这里插入图片描述
考虑到旋转前第一行和旋转后最后一列相等,可以做如下操作:
(1)对旋转前的数组进行遍历,(x, y, r)分别是它的行列号,2 * r + 1是要操作数组的宽度,因为x、y是行列号,所以要各自减1,成为数组下标,i从x - r开始,到x + r结束,j从y - r开始,到y + r结束;
(2)用临时数组存储旋转后的那一部分矩阵,x1从x - r开始,y1从y + r开始,也就是从旋转数组的右上角开始;y1不变,x1++,也就是沿着y1这列向下遍历;遍历完这列之后,x1回到x - r,y1–;把它赋值给原矩阵即可;逆时针也一样的道理;

import java.util.Scanner;

public class Code07_Scarlet {
    
    

    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
        int nSize = scan.nextInt();
        int times = scan.nextInt();

        int[][] matrix = new int[nSize][nSize];
        int count = 1;

        for (int i = 0; i < nSize; i++) {
    
    
            for (int j = 0; j < nSize; j++) {
    
    
                matrix[i][j] = count++;
            }
        }


        for (int i = 0; i < times; i++) {
    
    
            int x = scan.nextInt();
            int y = scan.nextInt();
            int r = scan.nextInt();
            int z = scan.nextInt();

            x -= 1;
            y -= 1;
            if (z == 0) {
    
    
                int[][] tempArr = new int[nSize][nSize];
                int x1 = x - r, y1 = y + r;
                for (int q = x - r; q <= x + r; q++) {
    
    
                    for (int j = y - r; j <= y + r; j++) {
    
    
                        tempArr[x1][y1] = matrix[q][j];
                        x1++;
                    }
                    x1 = x - r;
                    y1--;
                }

                for (int q = x - r; q <= x + r; q++) {
    
    
                    for (int j = y - r; j <= y + r; j++) {
    
    
                        matrix[q][j] = tempArr[q][j];
                    }
                }


            } else {
    
    
                int[][] tempArr = new int[nSize][nSize];
                int x1 = x + r, y1 = y - r;
                for (int q = x - r; q <= x + r; q++){
    
    
                    for (int j = y - r; j <= y + r; j++){
    
    
                        tempArr[x1][y1] = matrix[q][j];
                        x1--;
                    }
                    x1 = x + r;
                    y1++;
                }

                for (int q = x - r; q <= x + r; q++) {
    
    
                    for (int j = y - r; j <= y + r; j++) {
    
    
                        matrix[q][j] = tempArr[q][j];
                    }
                }

            }
        }

        for (int[] ints : matrix) {
    
    
            for (int anInt : ints) {
    
    
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
    }



}

八、新版石头剪刀布
在这里插入图片描述
在这里插入图片描述
创建一个查分表,查分表的含义是:对某个结点而言,甲出的该行的元素,乙出的该列的元素,如果结点为1,则甲加一分;
遍历出拳次数,对于甲的得分,构成为用i在甲的数组中找寻该次甲应该出什么,如果超过了甲的周期,因为i始终大于0,所以直接让i % 甲周期,这是横坐标,也就是找到了甲该出什么,纵坐标即为用同样的方法找到乙该出什么;横纵坐标放入查分表中,将查询后的结果加入甲的得分,该是0就是0,该是1就是1;乙的得分也一样;

九、农夫找牛牛
在10 * 10的矩阵中,有障碍物,又农夫和牛,一开始他们都向北移动,撞到障碍物时,会用这一次的时间顺时针旋转90度,且不得超过边界,当他们相遇时,返回步数,如果不相遇,返回0

(1)设置11 * 11矩阵,边界设上障碍物;
(2)当牛和农夫在同时以各自的同样的方向第二次到达某个地点时,可判断他们死循环了,把每次的这些相关的值算成一个简易的哈希,如果第二次出现同样的情况(产生哈希碰撞),则判死循环;

十、打印多项式
就是坑多!慢慢来,这道题完全没有技术含量的~

Supongo que te gusta

Origin blog.csdn.net/dgytjhe/article/details/121156905
Recomendado
Clasificación