ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

笔试题(2021.7.25拼夕夕)

2021-07-25 22:04:29  阅读:208  来源: 互联网

标签:25 2021.7 ++ System int isEmpty 拼夕夕 sc stack


笔试题

两道拼夕夕的笔试题,也是帮同学做的,那个扑克牌的题,出的奇奇怪怪的。。。

扑克牌开火车

题目描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

思路

刚开始我认为收走的牌还能出,所以就一直错
然后同学告诉我收走的牌不能出,然后能把示例过了,但是最后没来得及提交

思路就是模拟
两个人的牌用队列表示,放在桌子上的牌用栈表示
然后模拟这个过程,因为收走的牌不会再放下来,所以最后两个人手里的牌肯定都会为空。具体实现起来细节很多
可能代码有点啰嗦,但是大致是这个意思

import java.util.*;
public class Main{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        Queue<Integer> queue1 = new LinkedList<>();
        Queue<Integer> queue2 = new LinkedList<>();
        for(int i = 0; i < N; i++){
            queue1.offer(sc.nextInt());
        }
        for(int i = 0; i < N; i++){
            queue2.offer(sc.nextInt());
        }
        Stack<Integer> stack = new Stack<>();
        int count1 = 0;
        int count2 = 0;

        while(!queue1.isEmpty() || !queue2.isEmpty()) {
            if(!queue1.isEmpty()){
                int a = queue1.poll();

                if (stack.indexOf(a) == -1) {
                    stack.push(a);
                } else {
                    while (!stack.isEmpty() && stack.indexOf(a) != -1) {
                        count1++;
                        while (stack.peek() != a) {
                            stack.pop();
                            count1++;
                        }
                        stack.pop();
                        count1++;
                        if (queue1.isEmpty()) {
                            break;
                        }
                        a = queue1.poll();
                        stack.push(a);
                    }
                }
            }

            if(!queue2.isEmpty()) {
                int b = queue2.poll();
                if (stack.indexOf(b) == -1) {
                    stack.push(b);
                } else {
                    

                    while (!stack.isEmpty() && stack.indexOf(b) != -1) {

                        count2++;
                        while (stack.peek() != b) {
                            stack.pop();
                            count2++;
                        }
                        stack.pop();
                        count2++;
                        if (queue2.isEmpty()) {
                            break;
                        }
                        b = queue2.poll();
                        stack.push(b);
                    }

                }
            }
        }
        while(!stack.isEmpty()){
            int t = stack.pop();
            if(t % 2 == 1){
                count1++;
            }else{
                count2++;
            }
        }
        System.out.println(count1);
        System.out.println(count2);

    }
}

无限数字集合

题目描述

在这里插入图片描述
在这里插入图片描述

思路

这个比较简单,需要注意b 和 c 为0的情况
不知道这样写会不会超时,因为看到数据范围挺大的

import java.util.*;
public class Main{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int i = 0; i < T; i++){
            long a = sc.nextInt();
            long b = sc.nextInt();
            long c = sc.nextInt();
            long q = sc.nextInt();

            if(b == 0 && c == 0){
                if(q == a)
                    System.out.println(1);
                else{
                    System.out.println(0);
                }
                continue;
            }
            if(b == 0 || c == 0){
                long t = b == 0 ? c : b;
                if((q - a) / t * t == (q - a))
                    System.out.println(1);
                else{
                    System.out.println(0);
                }
                continue;
            }

            boolean flag = false;
            long max = (q - a + b - 1) / b;
            for(int j = 0; j <= max; j++){
                if(q - a >= j * b && (q - a - j * b) / c * c == (q - a - j * b)) {
                    System.out.println(1);
                    flag = true;
                    break;
                }
            }
            if(!flag)
                System.out.println(0);
        }
    }
}

标签:25,2021.7,++,System,int,isEmpty,拼夕夕,sc,stack
来源: https://blog.csdn.net/windyjcy/article/details/119088314

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有