- Java 也有表示栈的 Stack 类,不过和 C# 的 Stack 不同,Java 的可以像操作一般 List 一样操作 Stack(因为它是通过继承而不是组合的方式使用了Vector,导致它有了不应该有的方法)。
- 不管是哪个 LinkedList,都可以作为栈或(双端)队列使用。
作者: Tozzger
[蓝桥杯] 试题 算法训练 Anagrams问题
// http://lx.lanqiao.cn/problem.page?gpid=T223
Scanner in = new Scanner(System.in);
String a = in.nextLine().toLowerCase();
String b = in.nextLine().toLowerCase();
int[] alp = new int[26];
for (char ch : a.toCharArray())
alp[ch - 'a']++;
for (char ch : b.toCharArray())
alp[ch - 'a']--;
boolean ret = true;
for (int i : alp) {
if (i != 0) {
ret = false;
break;
}
}
System.out.println(ret ? "Y" : "N");
[蓝桥杯] 算法训练 素因子去重
// http://lx.lanqiao.cn/problem.page?gpid=T513
Scanner in = new Scanner(System.in);
long num = in.nextLong();
HashSet<Integer> set = new HashSet<>();
while (num > 1) {
int i = 2;
while (true) {
if (num % i == 0) {
set.add(i);
num /= i;
break;
}
i++;
}
}
int ret = 1;
for (int i : set) {
ret *= i;
}
System.out.println(ret);
[蓝桥杯] 算法训练 审美课
// http://lx.lanqiao.cn/problem.page?gpid=T519
Scanner in = new Scanner(System.in);
int n = in.nextInt();// 学生数
int m = in.nextInt();// 图画数
int pattern = (1 << m) - 1;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(n);
for (int i = 0; i < n; i++) {
int num = 0;
for (int j = 0; j < m; j++) {
num <<= 1;
if (in.nextInt() == 1) {
num |= 1;
}
}
map.merge(num, 1, Integer::sum);
}
int ret = 0;
for (Entry<Integer, Integer> e : map.entrySet()) {
ret += e.getValue() * map.getOrDefault(~e.getKey() & pattern, 0);
}
System.out.println(ret / 2);
[C#] Index 与 Range 的小细节
arr[Index] 等价于 arr[Index.GetOffset(arr.Length)](和平常的 arr[int] 没什么区别)。
arr[Range] 相当于把原来的 arr 的一部分浅拷贝一份。因此,对 arr[Range] 的操作不会影响 arr 本身。
[蓝桥杯] 杨辉三角形
Scanner in = new Scanner(System.in);
int line = in.nextInt();
int[] arr = new int[line];
Arrays.fill(arr, 1);
for (int i = 0; i < line; i++) {
for (int j = i - 1; j > 0; j--) {
arr[j] += arr[j - 1];
}
for (int j = 0; j <= i; j++) {
System.out.printf("%d ", arr[j]);
}
System.out.println();
}
[蓝桥杯] 查找整数(使用散列映射)
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int leng = in.nextInt();
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < leng; i++) {
map.putIfAbsent(in.nextInt(), i + 1);
}
System.out.println(map.getOrDefault(in.nextInt(), -1));
}
[蓝桥杯] 回文数
for(int i = 10; i< 100; i++) {
System.out.printf("%d%02d%n", i, (i % 10 * 10 + i / 10));
}
[蓝桥杯] 字母图形
static Scanner in = new Scanner(System.in);
static String pattern = "ZYXWVUTSRQPONMLKJIHGFEDCBABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] args) {
int line = in.nextInt(), col = in.nextInt();
for(int i=0;i<line;i++) {
System.out.println(pattern.substring(25 - i, 25 - i + col));
}
}
[蓝桥杯] Fibonacci 数列
static int f(int num) {
int fn1 = 1, fn2 = 1;
while(num-- > 2) {
// 原题要求如此,不需要的话把模删掉,再把返回类型换成更大的
int fn = (fn1 + fn2) % 10007;
fn2 = fn1;
fn1 = fn;
}
return fn1;
}