liaoguoyin / lanqiao Goto Github PK
View Code? Open in Web Editor NEWPAT、蓝桥杯 Java 题解集
Home Page: http://lx.lanqiao.cn/ https://pintia.cn/
PAT、蓝桥杯 Java 题解集
Home Page: http://lx.lanqiao.cn/ https://pintia.cn/
如果博主用 idea 的话, 推荐在提交时(git commit)勾选格式化代码选项.
这样代码更好看一些, 对于已经存在的代码, 可以在左边文件导航中选择一个文件夹, 然后按 ctrl+alt+L, 对单个文件的自动格式化同理.
注释推荐写在方法前或者类名前, 这样更好阅读.
A3 中提到的的 printf, 来自于 c语言 的 printf 函数(stdio.h), java 的 printf 语法与其一致.
println 意思是 print line, 真实输出的内容不一定是 \n. 而是由变量 "line.separator" 得到的.
在 *nix 操作系统下为 \n, NT 下为 \r\n.
可以使用此代码获取运行时系统的行分隔符
System.getProperty("line.separator")
而在 jdk 源码中(openjdk), 是这样获取的
lineSeparator = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
此问题意味着, 手动输出的换行符可能是不正确的.
或许命令行里显示是正确的, 但是 System.out 可以被导向文件描述符, 另外具体的操作系统也可以重定向输出输出, 这意味着程序的输出日志可以被外部控制而写入到文件中. 在具体的操作系统中, 手动输出的换行符可能与操作系统不匹配, 而导致文件的换行显示错误.
因此在任何代码中都不要企图输出 \n 来期待换行, 虽然大部分时候这是正确的. 而应该使用 println 或者 通过 System.getProperty("line.separator")
得到换行符.
与 c语言 的 printf 不同的是, 由于 java 没有 c语言 那样细化的变量类型, 例如 unsigned int, long long 等比较变态的类型, java 对于 long/Long int/Integer short/Short 等整数的输出统一用 %d. 即没有 %ld 之类的东西.
A4 问题中, 斐波那契数列的输出, 不需要建立数组, 因为不需要存储整个数列.
只需要声明三个变量 a,b,c, 然后滑动此窗口, 并逐个输出即可, 例如.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
long[] window = new long[]{1, 1, 0};
//处理 n 小于 3 的情况
for (int i = 1; i <= 2; i++) {
if (i != 1) System.out.print(" ");
System.out.print(window[i - 1]);
if (i == n) return;
}
//此处的 i 为自然序号
for (int i = 3; i <= n; i++) {
//第三个数等于第一个数加第二个数
window[2] = window[0] + window[1];
System.out.print(" " + window[2]);
//数组整体往左偏移一格, 也可以用循环队列实现
window[0] = window[1];
window[1] = window[2];
}
}
}
实际上的斐波那契数列在第 93 还是第 94 个就超过 long 的上限了, 所以要用 BigInteger, 比较复杂.
有关于变量命名法, 通常情况下, 使用 x1 格式的变量名辨识度是非常低的, 尤其当代码很长时, 人脑是很难记得所有上文声明过的变量的.
因此通常使用 idea 自动命名,
例如输入
Scanner s
此时 idea 就会提示 scanner, 按一下回车, 变量名就取好了.
自动命名就是把变量类型的第一个字母改成小写.
自动命名可以用变量名本身标示变量类型, 可以很方便的阅读代码而不需要在变量声明处和下文之间来回查看.
自动命名只用于该变量没有特殊的含义或者整个函数里只有它是这个类型的. 例如此处的 Scanner, 是一个用完即丢的变量, 不需要为他起名, 因此直接使用自动命名.
一些地方的自动命名有其他规则, 例如循环中, 在 idea 中输入 fori
按下回车, 将产生如下代码
for (int i = 0; i < ; i++) {
}
循环变量依次按惯例取名为(多重循环)为 i, j, k.
对于有含义的变量名, jdk 自身代码的命名法为匈牙利命名法, 具体比较复杂, 给几个简单的例子.
JButton mutedBlueLoginButton = new JButton()
良好的代码可阅读性是优秀代码的开端.
所以, 博主来跟我学 kotlin
吧, 鸡巴公司出品, 必属精品.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.