机试笔记

本文记录了刷《王道论坛考研机试上机指南》的JAVA和C++部分笔记,包括输入输出、字符串、数组、数据结构、排序等关键知识点,还提供了一些有用的资源链接和算法可视化工具。
摘要由CSDN通过智能技术生成

本文是刷《王道论坛考研机试上机指南》的笔记、遇到的一些坑的记录,大部分是JAVA实现,文末有C++的部分笔记,希望可以帮助到你~

一些有用的链接:
算法第四版代码https://github.com/jimmysuncpt/Algorithms
算法第四版代码中文目录https://github.com/jimmysuncpt/Algorithms
官方代码https://algs4.cs.princeton.edu/code/
刷书java版本https://github.com/godfanmiao/JobDu_OJ_Tutorial_Ex_Java
算法可视化https://visualgo.net/zh
算法读书笔记https://www.jianshu.com/u/24d715499bcf

JAVA部分

输入输出

1.输入(Scanner)
    import java.io.*;
    import java.util.*;
    public class InOutDemo {
        public static void main(String args[]){
            Scanner cin = new Scanner(new BufferedInputStream(System.in));
        }
    }
	
2. 读入一个整数
	int n = cin
3. 读入一个字符串
	String s=cin.next();
4. 读入一个浮点数
	double d=cin.nextDouble();
5. 读入一行
	String s1=cin.nextLine();
    如果把next()或者nextInt(),nextDouble() 、 nextFloat()用在nextLine的前面时。nextLine会把前者的结束符“换行符”作为字符串读入,进而不需要从键盘输入字符串nextLine已经转向下一条语句执行
    修正方法:在next()或nextInt()方法使用Enter键之后,填充一个无用的nextLine()
6. 输出
	System.out.println();
	System.out.printf("%d %10.5f\n", a, b); 
7. 浮点数保留几位小数,DecimalFormat类
    0 表示如果位数不足则以 0 填充,# 表示只要有可能就把数字拉上这个位置。这里0指一位数字,#指除0以外的数字。
    DecimalFormat f=new DecimalFormat("#.##%");
    DecimalFormat f=new DecimalFormat(",###.##");
    System.out.println(f.format(d));
8. 大数BigInteger BigDecimal
    存储任意精度的数,运算速度比较慢
    add,substract,multiply,divide,remainder,compareTo()
    divideAndRemainder:a[0]=this / val; a[1]=this % val
    pow,gcd,abs,negate,signum,mod,shiftLeft(this<<n),shiftRight(this>>n),and,or,xor
    BigInteger bi=new BigInteger("2");
    BigInteger bi=new BigInteger("-101",2);
    System.out.println(bi.toString(10));
	toString()会使用科学计数法,toPlainString()直接显示
	stripTrailingZeros()返回数值上等于此小数,移除末尾的0的BigDecimal
9. 进制转换
	String st = Integer.toString(num, base);
	int num = Integer.parseInt(st, base);
10. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

//scanf不能过的一个例子
public class Main {
    public static void main(String[] args) throws IOException {
//        Scanner cin = new Scanner(new BufferedInputStream(System.in));
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line=cin.readLine())!=null) {
            int n = Integer.parseInt(line);

            PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
            for (int i = 0; i < n; i++) {
                line = cin.readLine();
                String[] items = line.split(" ");
                if (Integer.parseInt(items[0]) == 1) {
                    priorityQueue.offer(Integer.parseInt(items[1]));
                } else {
                    System.out.println(priorityQueue.poll());
                }
            }
        }
    }
}

字符串

1. 基本操作
    charAt(),substring()
    ch = st.toCharArray(); // 字符串转换为字符数组.
2. 比较
    if(!this.name.equals(s1.name)){
         return this.name.compareTo(s1.name);
    }
3.StringBuilder比较
	s1.toString().equals(s2.toString())

调用

1. main中调用非静态方法时候,先建立类对象,再调用
	Main e = new Main(); 
    e.dfs(0); 

数组

1. 相对于array,ArrayList特点:
	动态改变大小&#
  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值