考研算法辅导课总结-持续更新中

2023-11-14

本篇文章适用于考研和复试上机的同学,建议去ACWING买考研算法辅导课同步刷题,上岸的c++选手贼多,我都拿的是Java写的,有些拿c++写的,简单做法和最优做法基本都有,仅供参考!!!在这里插入图片描述

建议根据大标题和题号来刷题

排序和进位制

3375. 成绩排序

import java.util.*;
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[][] arr = new int[n][2];
        String[] s = new String[n];
        for(int i = 0; i < n; i++){
            s[i] = sc.next();
            arr[i][0] = i;
            arr[i][1]= sc.nextInt();
        }
        if(k == 1){
            Arrays.sort(arr,(o1,o2)->{
                return o1[1]-o2[1];
            });   
        }else{
            Arrays.sort(arr,(o1,o2)->{
                return o2[1]-o1[1];
            });   
            
        }
        for(int[] x: arr){
            System.out.print(s[x[0]]+" ");
            System.out.println(x[1]);
        }
        
        
        
    }
}

3376. 成绩排序2

import java.util.*;
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] arr = new int[n][2];
        for(int i = 0; i < n; i++){
            arr[i][0] = sc.nextInt();
            arr[i][1] = sc.nextInt();
        }

            Arrays.sort(arr,(o1,o2)->{
                if(o1[1] != o2[1]){
                    return o1[1] - o2[1];
                }
                return o1[0] - o2[0];
               
            });   
      
        for(int[] x: arr){
            System.out.print(x[0]+" ");
            System.out.println(x[1]);
        }
        
        
        
    }
}

3373. 进制转换

package Acwing;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class AC3373_2 {
    static Deque<Integer> div(Queue<Integer> queue, int b){
        Deque<Integer> tmp = new LinkedList<>();
        int cnt = queue.size();
        int r = 0;
        while(cnt-- != 0){
            r = r * 10 + queue.poll();
            tmp.offer(r/b);
            r %=b;
        }
        while(tmp.size() != 0 && tmp.peek()==0){
            tmp.poll();
        }
        return tmp;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String x = sc.next();

            Deque<Integer> queue = new LinkedList<>();
            for(char c: x.toCharArray()){
                queue.offer(c-'0');
            }

            StringBuilder res = new StringBuilder();
            if("0".equals(x)){
                res.append("0");
            }else{
                while(!queue.isEmpty()){
                    System.out.println(queue.peekLast());
                    res.append(queue.peekLast()%2);
                    queue =  div(queue,2);
                }
            }
            System.out.println(res.reverse());
        }
    }
}

import java.util.*;
import java.math.BigInteger;
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            BigInteger b = sc.nextBigInteger();
            System.out.println(b.toString(2));
        }
    }
}

3374.进制转换2

import java.util.*;
import java.math.BigInteger;
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int m = sc.nextInt();
            int n = sc.nextInt();
            BigInteger b = sc.nextBigInteger(m);
            System.out.println(b.toString(n));
        }
    }
}

import java.util.*;
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        String s = sc.next();
        Deque<Integer> queue = new LinkedList<>();
        for(char c: s.toCharArray()){
            int x = 0;
            if(c>='A'){
                x = c-'A'+10;
            }else{
                x = c-'0';
            }
            queue.offer(x);
        }
        StringBuilder sb = new StringBuilder();
        if("0".equals(s)){
            sb.append("0");
        }else{
            while(!queue.isEmpty()){
                Deque<Integer> tmp = new LinkedList<>();
                int cnt = queue.size();
                int r = 0;
                for(int i = 0; i < cnt; i++){
                    int t = queue.poll();
                    //计算t的十进制
                    t += r * a;
                    //记录余数
                    r= t%b;
                    //记录除数
                    t = t/b;
                    tmp.offer(t);
                    
                }
                while(!tmp.isEmpty() && tmp.peek()== 0){
                    tmp.poll();
                }
                queue=tmp;
                if(r <10){
                    sb.append(r);
                }else{
                    sb.append((char)(r-10+'a'));
                }
            }
            System.out.println(sb.reverse());
            
        }
        
    }
}

链表和日期问题

66.两个链表的第一个公共节点

这个题有两种做法:

链表相加:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
class Solution {
    public ListNode findFirstCommonNode(ListNode headA, ListNode headB) {
        ListNode h1 = headA;
        ListNode h2 = headB;
        while(h1!=h2){
            h1 = h1!=null? h1.next:headB;
            h2 = h2!=null? h2.next:headA;
        }
        return h1;
    }
}
链表相减找后半段:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
class Solution {
    public ListNode findFirstCommonNode(ListNode headA, ListNode headB) {
        ListNode h1 = headA;
        ListNode h2 = headB;

        int len1 = 0;
        int len2 = 0;
        while(h1 != null){
            len1++;
            h1 = h1.next;
        }
         while(h2 != null){
            len2++;
            h2 = h2.next;
        }
        if(len1 <= len2){
            for(int i = 0; i < len2 - len1; i++){
                headB = headB.next;
            }
        }else{
             for(int i = 0; i < len1 - len2; i++){
                headA = headA.next;
            }
        }

        while(headA!=null&&headB!=null){
            if(headA.val==headB.val){
                return headA;
            }
            headA = headA.next;
            headB = headB.next;
        }
        return null;

    }
}

作者:福尔摩DONG
链接:https://www.acwing.com/activity/content/code/content/1414690/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

3756.筛选链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* filterList(ListNode* head) {
        bool st[10001] = {};
        ListNode *dummy = new ListNode(-1);
        ListNode *cur = dummy, *p = new ListNode(-1);
        while(head){
            int val = abs(head->val);
            if(!st[val]){
                cur->next = head;
                p = head;
                cur = cur->next;
                st[val] = true;
            }
            head = head->next;
            
        }
        p->next = NULL;
        return dummy->next;
        
    }
};
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode filterList(ListNode head) {
        HashSet<Integer> set = new HashSet<>();
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy,h1 = new ListNode(-1);
        while(head!=null){
            int x = Math.abs(head.val);
            
            if(!set.contains(x)){
                
                cur.next = head;
                h1 = head;
                cur = cur.next;
                set.add(x);
            } 
            head = head.next;
        }
        h1.next = null;
        return dummy.next;
    }
}

3757.重排链表

ListNode在 = 赋值的时候,传递的是地址。前半段要记得终止,就要是的a->next = null,而在遍历的时候使用的是指针。地址是全局的。相当于链表的最后一个肯定要指向空
 
 /**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public void rearrangedList(ListNode head) {
         //计算链表长度
        ListNode p = head;
        int len = 0;
        while(p!=null){
            len++;
            p = p.next;
        }
        int left = (len+1)/2;
        ListNode a = head;
        for(int i = 0; i < left-1; i++){
            a = a.next;
        }
        ListNode b = a.next;
        a.next = null;
        
        ListNode pre = null;
        while(b!=null){
            ListNode c = b.next;
            b.next = pre;
            pre = b;
            b = c;
        }
        
       
        while(pre!=null){
            ListNode ta = head.next, tb = pre.next;
            head.next = pre;
            pre.next = ta;
            head = ta;
            pre = tb;
        }
    }
}
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void rearrangedList(ListNode* head) {
        ListNode *h = head;
        int len = 0;
        while(h){
            len++;
            h = h->next;
        }
        int left = (len+1)/2;
        ListNode *l = head;
        for(int i = 0; i < left-1; i++){
            l = l->next;
        }
        ListNode *r = l->next;
        l->next = NULL;
        ListNode *pre = NULL;
        while(r){
            ListNode *ne = r ->next;
            r->next = pre;
            pre = r;
            r = ne;
        }
        while(pre){
            ListNode *l1 = head->next;
            ListNode *r1 = pre->next;
            head ->next =pre;
            pre->next = l1;
            head = l1;
            pre = r1;
        }
    }
};

3607 打印日期

一年中的天数为

1,3,5,7,8,10,12为31天,2月的话闰年为29天,平年为28天。

闰年的话分为普通闰年和世纪闰年,4年一闰,百年不闰或400年一闰。

这个比较恶心的是yyyy-mm-dd,

可以用prinf试试,卧槽,printf可以补前导0,绝了,以前不知道,一直特判,人傻了。。。

import java.util.*;
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int[] arr1 = {31,28,31,30,31,30,31,31,30,31,30,31};
        int[] arr2 = {31,29,31,30,31,30,31,31,30,31,30,31};
        while(sc.hasNext()){
            int x = sc.nextInt();
            int y = sc.nextInt();  
           
            if((x%4==0&&x%100!=0)||(x%400==0)){
              
                for(int i = 1; i <= 12; i++){
                    y = y - arr2[i-1];
                    if(y<=0){
                       System.out.printf("%04d-%02d-%02d\n",x,i,(y+arr2[i-1]));
                        break;
                    }
                }
            }else{
                for(int i = 1; i <= 12; i++){
                    y = y - arr1[i-1];
                    if(y<=0){
                       System.out.printf("%04d-%02d-%02d\n",x,i,(y+arr1[i-1]));
                        break;
                    }
                }
            }
        }
    }
}

import java.util.*;
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int[] arr1 = {31,28,31,30,31,30,31,31,30,31,30,31};
        int[] arr2 = {31,29,31,30,31,30,31,31,30,31,30,31};
        while(sc.hasNext()){
            int x = sc.nextInt();
            int y = sc.nextInt();  
            String xx = String.valueOf(x);
                if(xx.length()<4){
                    for(int i = 0; i < 4-xx.length();i++){
                        xx = "0"+xx;
                    }
                }
            if((x%4==0&&x%100!=0)||(x%400==0)){
              
                for(int i = 1; i <= 12; i++){
                    y = y - arr2[i-1];
                    if(y<=0){
                        System.out.println(xx+"-"+(i<10?"0":"")+i+"-"+(arr2[i-1]+y<10?"0":"")+(arr2[i-1]+y));
                        break;
                    }
                }
            }else{
                for(int i = 1; i <= 12; i++){
                    y = y - arr1[i-1];
                    if(y<=0){
                        System.out.println(xx+"-"+(i<10?"0":"")+i+"-"+(arr1[i-1]+y<10?"0":"")+(arr1[i-1]+y));
                        break;
                    }
                }
            }
        }
    }
}

3573.日期累加

import java.util.*;
class Main{
    //日期问题三板斧
    static int[] month = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    static int is_leap(int year){
        if((year %4 == 0&& year %100 !=0)||(year % 400 == 0)){
            return 1;
        }
        return 0;
    }
    static int get_month(int y ,int m){
        if(m == 2){
                //获得月份天数
                return month[m]+is_leap(y);
        }
        return month[m];
    }
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while(n-- != 0){
            int y = sc.nextInt();
            int m = sc.nextInt();
            int d = sc.nextInt();
            int a = sc.nextInt();
            int sum = 0;
            //算总数从一年的开始开始算        
            for(int i = 1; i < m; i++){
                sum +=get_month(y,i);
            }
            
            sum+=d+a;
            //然后按年算
            while(sum > 365+is_leap(y)){
                sum -= 365+is_leap(y);
                y++;
            }
            m = 1;
            //按月算
            while(sum > get_month(y,m)){
                sum -=get_month(y,m);
                m++;
            }
            //最后剩下的就是天数
            System.out.printf("%04d-%02d-%02d\n",y,m,sum);
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

考研算法辅导课总结-持续更新中 的相关文章

随机推荐

  • 服务器的日常运维巡检视频,日常运维检查记录表

    日常运维检查记录表 2页 本资源提供全文预览 点击全文预览即可全文预览 如果喜欢文档就下载吧 查找使用更方便哦 19 90 积分 日常运维检查记录表检查分类检查分类检查对像检查对像检查内容检查内容检查结果检查结果备注备注检查通道检测卡上各部
  • 关于KEIL MDK调试ARM程序不能仿真的问题

    在单片机程序调试过程中 由于程序量小 利用仿真器进行仿真调试方便直观 所以一般经常使用 但是keil经常会出现罢工 无法用仿真器调试的现象 如下图 解决方法也很简单 按照下图设置即可
  • BigDecimal类型加减乘除运算(Java必备知识)

    在现实开发当中经常会遇到这种计算 这里特此整理一下为方便以后学习 希望能帮助到其他的萌新 目录 1 为什么要用BigDecimal计算 2 浮点计算误差产生的原因 3 bigdecimal的初始化 4 bigdecimal的加减乘除 5 除
  • 【深度学习】小概念

    好用小工具 https lutzroeder github io netron 网络架构图可视化工具 liner probe与fine tune liner probe 将预训练的模型冻住 只从里面抽特征 就训练最后fc分类头层 做有监督的
  • 粒子群算法4——粒子群算法与蚁群算法的异同点

    作者 莫石 链接 http www zhihu com question 30326374 answer 59884351 来源 知乎 著作权归作者所有 转载请联系作者获得授权 群体智能算法家族的两个重要成员就是粒子群算法与蚁群算法 基本思
  • ros+arduino学习(六):重构ros_lib库文件

    前言 ros lib是arduino程序和ros连接的库文件 通过使用这些库文件和相关函数 可以在arduino上通过编程使得arduino硬件开ros节点程序 这样arduino硬件就可以与上位机通过话题进行通讯 从而把arduino从传
  • Spring配置构造函数的参数

    Spring配置构造函数的参数 参考 http blog csdn net u013473691 article details 50589021
  • sonar代码检查_代码质量管理工具:SonarQube常见的问题及正确解决方案(一)

    SonarQube 简介 Sonar 是一个用于代码质量管理的开放平台 通过插件机制 Sonar 可以集成不同的测试工具 代码分析工具 以及持续集成工具 与持续集成工具 例如 Hudson Jenkins 等 不同 Sonar 并不是简单地
  • 剑指 Offer 28. 对称的二叉树 -- 递归

    0 题目描述 leetcode原题链接 剑指 Offer 28 对称的二叉树 1 递归解法 对称二叉树定义 对于树中 任意两个对称节点 L L L 和 R R R 一定有
  • 【linux】redhat笔记:redhat入门操作

    登录界面 普通用户 root 一直点next 搜索执行任务 切换任务 鼠标点击 win键或者点击activity 会显示所有任务 控制窗口 最大化窗口 拖动窗口置顶部 取消最大化 拖动回去 分屏 拖动置左右 win 上下左右 控制窗口 要求
  • 算法入门:熄灯问题

    include
  • syn锁整体理解

    目录 前言 一 syn主要实现方式和基本原理 1 1实现原理 1 2syn特征 二 synchronized底层存储 2 1 对象结构 2 2 对象头的组成 2 2 2 class pointer 2 3 Monitor监视器锁 2 3 1
  • 02_fork和vfork的使用

    include
  • Cowrie 部署 SSH 蜜罐

    什么是蜜罐 1 何谓SSH蜜罐 通俗的讲 就是用程序模拟一个SSH端口服务 让黑客以为是真的SSH服务连接上来 然后收集相关信息比如 IP 登录所用的账号 登录上来之后做了什么操作等等 2 蜜罐其实就是一台无人使用但却被严密监控的网络主机
  • vue项目重复点击一个路由会报错如何解决

    在新版本的vue router中 重复点击同一个路由会出现以下报错 这个问题时vue router 3 2 0版本的一个小Bug 方法有很多 比如降低路由版本 不推荐 但是推荐下面这种方式 比较简单靠谱 把这段代码直接粘贴到router i
  • openssl websockets

    1 HTTPS通信的C 实现 知乎 GitHub Bwar Nebula Nebula is a powerful framwork for building highly concurrent distributed and resili
  • 数据结构括号匹配问题 C语言

    数据结构中 括号匹配问题可以说是一个非常经典的问题 问题描述 假设一算术表达式中包括三种括号 圆括号 和 方括号 和 花括号 和 且三种括号可按任意次序嵌套使用 试编写程序判定输入的表达式所含的括号是否正确配对出现 提示 表达式可以存入一个
  • 毕业设计 树莓派口罩佩戴检测系统设计与实现 - 单片机 物联网 机器视觉

    文章目录 0 前言 1 简介 2 主要器件 3 实现效果 4 硬件设计 树莓派4B 5 软件说明 Debian Pi Aarch64 树莓派操作系统 vnc 远程连接树莓派 opencv 摄像头人脸数据采集 人脸数据显示等 6 部分核心代码
  • 如何理解遗传算法中的编码与解码?以二进制编码为例

    文章目录 前言 编码 解码 补充 前言 遗传算法的编码方法各种各样 但二进制串编码方式是最经典的一种 那么它的编码和解码该如何进行呢 或许本博客能给你一个具有参考价值的答案 编码 经典遗传算法中使用 染色体 来代指个体 它由二进制串组成 如
  • 考研算法辅导课总结-持续更新中

    这考研算法辅导课总结 建议根据大标题和题号来刷题 排序和进位制 3375 成绩排序 3376 成绩排序2 3373 进制转换 3374 进制转换2 链表和日期问题 66 两个链表的第一个公共节点 3756 筛选链表 3757 重排链表 36