Python3/C++/Go/JavaScript 配置
大约 4 分钟
Python3/C++/Go/JavaScript 配置
Python3
python-3.10.1-amd64.exe
: https://www.python.org/downloads/
$ python -V
Python 3.10.1
参考链接
C++
x86_64-12.2.0-release-posix-seh-rt_v10-rev1.7z
: https://github.com/niXman/mingw-builds-binaries/releases
参考链接
- https://www.mingw-w64.org/downloads/#mingw-builds
- Stackoverflow: mingw-w64 threads: posix vs win32
- Stackoverflow: What is the difference between MinGW SEH and MinGW SJLJ?
$ gcc --version
gcc (x86_64-posix-seh-rev1, Built by MinGW-W64 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ --version
gcc (x86_64-posix-seh-rev1, Built by MinGW-W64 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gdb --version
gcc (x86_64-posix-seh-rev1, Built by MinGW-W64 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// 小顶堆
priority_queue<int, vector<int>, greater<int>> q;
// 大顶堆(默认)
priority_queue<int, vector<int>, less<int>> q;
Go
go1.20.windows-amd64.msi
: https://golang.google.cn/dl/
$ go version
go version go1.20 windows/amd64
export GO111MODULE=on
export GOPROXY=https://repo.huaweicloud.com/repository/goproxy/
export GONOSUMDB=*
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go get -v golang.org/x/tools/gopls
go env -w GO111MODULE=off
gotests
gomodifytags
impl
goplay
dlv
staticcheck
gopls
go-outline
go install -v golang.org/x/tools/gopls@latest
参考链接
JavaScript
node-v18.14.0-x64.msi
: https://nodejs.org/en/
$ node -v
v18.14.0
$ npm -v
9.3.1
$ yarn -v
1.22.19
语言差异
浮点数精度
Java 太 sb 了。。。
Java
public class abc279d {
public static void main(String[] args) {
double d = 8772053214538.5976562500;
System.out.printf("%.10f", d);
}
}
// OUTPUT: 8772053214538.5980000000
C++
#include <iostream>
int main()
{
double d = 8772053214538.5976562500;
printf("%.10f", d);
}
// OUTPUT: 8772053214538.5976562500
Python3
d = 8772053214538.5976562500
print('%.10f' % d)
# OUTPUT: 8772053214538.5976562500
Golang
package main
import "fmt"
func main() {
d := 8772053214538.5976562500
fmt.Printf("%.10F", d)
}
// OUTPUT: 8772053214538.5976562500
JavaScript
var d = 8772053214538.59765625;
console.log(d.toFixed(10));
// OUTPUT: 8772053214538.5976562500
运算符优先级
注意到 Golang 移位运算符的优先级大于加减。
Golang
Unary operators have the highest precedence. As the ++ and -- operators form statements, not expressions, they fall outside the operator hierarchy. As a consequence, statement *p++
is the same as (*p)++
.
There are five precedence levels for binary operators. Multiplication operators bind strongest, followed by addition operators, comparison operators, && (logical AND), and finally || (logical OR):
Precedence Operator
5 * / % << >> & &^
4 + - | ^
3 == != < <= > >=
2 &&
1 ||
Java
Operators Precedence
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
二分查找(CF1837F、CF1610E)
C++
a[i] = lower_bound(xs.begin(), xs.end(), make_pair(a[i], i)) - xs.begin();
Java
- 第一个值大于 x。
- 第一个值等于 x,第二个值大于等于 y。
a[i] = lowerBound(xs, new int[]{a[i], i});
// 第一个值大于 x
// 第一个值等于 x,第二个值大于等于y。
private static int lowerBound(int[][] pairs, int[] pair) {
int left = 0;
int right = pairs.length;
while (left < right) {
int mid = left + (right - left) / 2;
// 边界二分 F, F,..., F, [T, T,..., T]
// ----------------------^
if (pairs[mid][0] > pair[0] || pairs[mid][0] == pair[0] && pairs[mid][1] >= pair[1]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
Golang
j += 1 + sort.SearchInts(a[j+1:], a[j]*2-x)
Java
j += 1 + searchInts(a, j + 1, n, a[j] * 2 - a[i]);
// Params:
// a – the array to be searched
// fromIndex – the index of the first element (inclusive) to be searched
// toIndex – the index of the last element (exclusive) to be searched
// key – the value to be searched for
private static int searchInts(int[] nums, int fromIndex, int toIndex, int key) {
int left = 0;
int right = toIndex - fromIndex;
while (left < right) {
int mid = left + (right - left) / 2;
// 边界二分 F, F,..., F, [T, T,..., T]
// ----------------------^
if (nums[fromIndex + mid] >= key) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
排序(CF1826E)
C++
sort(a.begin(), a.end());
Java
- 第一维度排序完,排序第二维度,依此类推。
static int[][] a;
Arrays.sort(a, (o1, o2) -> {
int i = 0;
while (i < m && o1[i] == o2[i]) i++;
if (i == m) return 0;
return Integer.compare(o1[i], o2[i]);
});
Python 的 for/while-else 语法
break
以后便不在执行 else
子句。java 等价写法是 break label
。
for-else:
class Solution755 {
public int[] pourWater(int[] heights, int volume, int k) {
while (volume-- > 0) {
label:
{
for (int d = -1; d <= 1; d += 2) {
int i = k, best = k;
while (0 <= i + d && i + d < heights.length && heights[i + d] <= heights[i]) {
if (heights[i + d] < heights[i]) best = i + d;
i += d;
}
if (heights[best] < heights[k]) {
heights[best]++;
break label;
}
}
heights[k]++;
}
}
return heights;
}
}
while-else:
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.List;
import java.util.stream.Collectors;
public class Solution2751 {
public List<Integer> survivedRobotsHealths(int[] positions, int[] healths, String directions) {
int n = positions.length;
Node[] nodes = new Node[n];
for (int i = 0; i < n; i++) {
nodes[i] = new Node(i, positions[i], healths[i], directions.charAt(i) == 'R' ? 1 : -1);
}
Arrays.sort(nodes, Comparator.comparingInt(o -> o.pos));
List<Node> ans = new ArrayList<>();
Deque<Node> stack = new ArrayDeque<>();
for (Node node : nodes) {
if (node.dir == 1) {
stack.push(node);
continue;
}
// 向左
// Python while-else 语法
label:
{
while (!stack.isEmpty()) {
if (node.health < stack.peek().health) {
// 栈顶健康度更大
stack.peek().health--;
break label;
} else if (node.health == stack.peek().health) {
// 健康度一样大
stack.pop();
break label;
} else {
// 向左的更大
node.health--;
stack.pop();
}
}
ans.add(node);
}
}
ans.addAll(stack);
ans.sort(Comparator.comparingInt(o -> o.id));
return ans.stream().map(o -> o.health).collect(Collectors.toList());
}
private static class Node {
int id, pos, health, dir;
public Node(int id, int pos, int health, int dir) {
this.id = id;
this.pos = pos;
this.health = health;
this.dir = dir;
}
}
}
(全文完)