填空题
问题描述
在计算机存储中,15.125GB是多少MB?
Answer:15488
15.125 × 1024 = 15488
问题描述
1200000有多少个约数(只计算正约数)。
Answer:96
直接暴力遍历得出结果填上去
问题描述
一棵包含有2019个结点的二叉树,最多包含多少个叶结点?
Answer:1010
计算完最后一层后要记得加上一层的叶子节点!!!
问题描述
在1至2019中,有多少个数的数位中包含数字9?
注意,有的数中的数位中包含多个9,这个数只算一次。例如,1999这个数包含数字9,在计算只是算一个数。
Answer:544
excel 序列生成1到2019,然后筛选
编程题
问题描述
在数列 a[1], a[2], …, a[n] 中,如果对于下标 i, j, k 满足 0<i<j<k<n+1 且 a[i]<a[j]<a[k],则称 a[i], a[j], a[k] 为一组递增三元组,a[j]为递增三元组的中心。
给定一个数列,请问数列中有多少个元素可能是递增三元组的中心。
输入格式
输入的第一行包含一个整数 n。
第二行包含 n 个整数 a[1], a[2], …, a[n],相邻的整数间用空格分隔,表示给定的数列。
输出格式
输出一行包含一个整数,表示答案。
样例输入
5
1 2 5 3 5
样例输出
2
样例说明
a[2] 和 a[4] 可能是三元组的中心。
评测用例规模与约定
对于 50% 的评测用例,2 <= n <= 100,0 <= 数列中的数 <= 1000。
对于所有评测用例,2 <= n <= 1000,0 <= 数列中的数 <= 10000。
大致思路是从前往后遍历
2个全局变量min和max,分别表示当前最小值和最大值的下标
每次先和min比较,arr[min]>=arr[current],直接返回false,否则更新min
接着和max比较(注意max要比current大),如果当前的最大值不符合要求,则向后面寻找,找到了符合要求的值,则更新最大值并记录下标,返回true。找不到,返回false。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = reader.nextInt();
}
int count = 0;
for (int i = 1; i < arr.length - 1; i++) {
if (check(arr, i)) {
count++;
}
}
System.out.println(count);
}
static int min = 0;
static int max = 0;
static boolean check(int[] arr, int index) {
for (int i = max; i < arr.length; i++) {
if (arr[min] >= arr[index]) {
min = index;
return false;
}
if (index < i && arr[index] < arr[i]) {
max = i;
return true;
}
}
return false;
}
}
问题描述
一个正整数如果任何一个数位不大于右边相邻的数位,则称为一个数位递增的数,例如1135是一个数位递增的数,而1024不是一个数位递增的数。
给定正整数 n,请问在整数 1 至 n 中有多少个数位递增的数?
输入格式
输入的第一行包含一个整数 n。
输出格式
输出一行包含一个整数,表示答案。
样例输入
30
样例输出
26
评测用例规模与约定
对于 40% 的评测用例,1 <= n <= 1000。
对于 80% 的评测用例,1 <= n <= 100000。
对于所有评测用例,1 <= n <= 1000000。
没有想到很好的方法,简单的从后往前一位一位判断
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int count = 0;
for (int i = 1; i <= n; i++) {
if (check(i)) {
count++;
}
}
System.out.println(count);
}
static boolean check(int num) {
int pre = num % 10;
int current = num / 10;
while (current > 0) {
int tmp = current % 10;
if (tmp > pre) {
return false;
}
pre = tmp;
current /= 10;
}
return true;
}
}
问题描述
小明对类似于 hello 这种单词非常感兴趣,这种单词可以正好分为四段,第一段由一个或多个辅音字母组成,第二段由一个或多个元音字母组成,第三段由一个或多个辅音字母组成,第四段由一个或多个元音字母组成。
给定一个单词,请判断这个单词是否也是这种单词,如果是请输出yes,否则请输出no。
元音字母包括 a, e, i, o, u,共五个,其他均为辅音字母。
输入格式
输入一行,包含一个单词,单词中只包含小写英文字母。
输出格式
输出答案,或者为yes,或者为no。
样例输入
lanqiao
样例输出
yes
样例输入
world
样例输出
no
评测用例规模与约定
对于所有评测用例,单词中的字母个数不超过100。
直接使用正则表达式,100个字母,不算长
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String str = reader.next();
if(str.matches("[^aeiou]+[aeiou]+[^aeiou]+[aeiou]+")){
System.out.println("yes");
}else{
System.out.println("no");
}
}
}
问题描述
小明想知道,满足以下条件的正整数序列的数量:
1. 第一项为 n;
2. 第二项不超过 n;
3. 从第三项开始,每一项小于前两项的差的绝对值。
请计算,对于给定的 n,有多少种满足条件的序列。
输入格式
输入一行包含一个整数 n。
输出格式
输出一个整数,表示答案。答案可能很大,请输出答案除以10000的余数。
样例输入
4
样例输出
7
样例说明
以下是满足条件的序列:
4 1
4 1 1
4 1 2
4 2
4 2 1
4 3
4 4
评测用例规模与约定
对于 20% 的评测用例,1 <= n <= 5;
对于 50% 的评测用例,1 <= n <= 10;
对于 80% 的评测用例,1 <= n <= 100;
对于所有评测用例,1 <= n <= 1000。
直接暴力搜索显然会超时,但是我没有找出规律,只好简单的优化了一下
昨天在网上看到一种方式,打表,即直接把答案写在程序里,用数组存储,这样时间复杂度就o(1)了…
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
record = new int[n][n];
int sum = 0;
for (int i = 1; i <= n; i++) {
sum+= fun(n, i);
sum %= 10000;
}
System.out.println(sum);
}
static int[][] record;
static int fun(int a, int b) {
if (record[a - 1][b - 1] != 0) {
return record[a - 1][b - 1];
}
int tmp = Math.abs(a - b);
int sum = 1;
for (int i = 1; i < tmp; i++) {
sum += fun(b, i);
sum %= 10000;
}
record[a-1][b-1] = sum;
return sum;
}
}
问题描述
小明有一块空地,他将这块空地划分为 n 行 m 列的小块,每行和每列的长度都为 1。
小明选了其中的一些小块空地,种上了草,其他小块仍然保持是空地。
这些草长得很快,每个月,草都会向外长出一些,如果一个小块种了草,则它将向自己的上、下、左、右四小块空地扩展,这四小块空地都将变为有草的小块。
请告诉小明,k 个月后空地上哪些地方有草。
输入格式
输入的第一行包含两个整数 n, m。
接下来 n 行,每行包含 m 个字母,表示初始的空地状态,字母之间没有空格。如果为小数点,表示为空地,如果字母为 g,表示种了草。
接下来包含一个整数 k。
输出格式
输出 n 行,每行包含 m 个字母,表示 k 个月后空地的状态。如果为小数点,表示为空地,如果字母为 g,表示长了草。
样例输入
4 5
.g…
…
…g…
…
2
样例输出
gggg.
gggg.
ggggg
.ggg.
评测用例规模与约定
对于 30% 的评测用例,2 <= n, m <= 20。
对于 70% 的评测用例,2 <= n, m <= 100。
对于所有评测用例,2 <= n, m <= 1000,1 <= k <= 1000。
一个一个月慢慢来,简单说就是暴力
昨天在网络上吸取的经验,还是自己练习太少,这题使用过队列只考虑长了草的空地比起遍历整个二维数组要快。
import java.util.Scanner;
// 原先的思路
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int m = reader.nextInt();
char[][] map = new char[n][];
for (int i = 0; i < map.length; i++) {
map[i] = reader.next().toCharArray();
}
int k = reader.nextInt();
while (k-- > 0) {
map = fun(map);
}
for (int i = 0; i < map.length; i++) {
System.out.println(map[i]);
}
}
static char[][] fun(char[][] old) {
char[][] next = new char[old.length][old[0].length];
int[] x = { 1, -1, 0, 0 };
int[] y = { 0, 0, 1, -1 };
char g = 'g';
for (int i = 0; i < next.length; i++) {
for (int j = 0; j < next[i].length; j++) {
if (old[i][j] == g) {
next[i][j] = g;
for (int k = 0; k < y.length; k++) {
try {
next[i + x[k]][j + y[k]] = g;
} catch (Exception e) {
continue;// 可能越界,直接用异常处理,省去判断
}
}
} else if (next[i][j] != g) {
next[i][j] = '.';
}
}
}
return next;
}
}
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
// 学习的思路
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int m = reader.nextInt();
Queue<Node> grass = new LinkedList<Node>();
map = new char[n][];
for (int i = 0; i < map.length; i++) {
map[i] = reader.next().toCharArray();
for (int j = 0; j < map[i].length; j++) {
if (map[i][j] == g) {
Node node = new Node(i, j);
grass.add(node);
}
}
}
int k = reader.nextInt();
while (k-- > 0) {
grass = fun(grass);
}
for (int i = 0; i < map.length; i++) {
System.out.println(map[i]);
}
}
static char[][] map;
static char g = 'g';
static Queue<Node> fun(Queue<Node> grass) {
Queue<Node> next = new LinkedList<Node>();
int[] x = { 1, -1, 0, 0 };
int[] y = { 0, 0, 1, -1 };
while (!grass.isEmpty()) {
Node node = grass.poll();
if (map[node.x][node.y] == g) {
for (int i = 0; i < y.length; i++) {
int tmpX = node.x + x[i];
int tmpY = node.y + y[i];
try {
map[tmpX][tmpY] = g;
} catch (Exception e) {
continue; // 可能越界,直接用异常处理,省去判断
}
Node nextNode = new Node(tmpX, tmpY);
next.add(nextNode);
}
}
}
return next;
}
}
class Node {
int x;
int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
问题描述
小明要组织一台晚会,总共准备了 n 个节目。然后晚会的时间有限他只能最终选择其中的 m 个节目。
这 n 个节目是按照小明设想的顺序给定的,顺序不能改变。
小明发现,观众对于晚上的喜欢程度与前几个节目的好看程度有非常大的关系,他希望选出的第一个节目尽可能好看,在此前提下希望第二个节目尽可能好看,依次类推。
小明给每个节目定义了一个好看值,请你帮助小明选择出 m 个节目,满足他的要求。
输入格式
输入的第一行包含两个整数 n, m ,表示节目的数量和要选择的数量。
第二行包含 n 个整数,依次为每个节目的好看值。
输出格式
输出一行包含 m 个整数,为选出的节目的好看值。
样例输入
5 3
3 1 2 5 4
样例输出
3 5 4
样例说明
选择了第1, 4, 5个节目。
评测用例规模与约定
对于 30% 的评测用例,1 <= n <= 20;
对于 60% 的评测用例,1 <= n <= 100;
对于所有评测用例,1 <= n <= 100000,0 <= 节目的好看值 <= 100000。
JAVA
建立一个类
class Node{ int index; int data}
先对data属性降序排序,然后取前m个元素,按index升序排序
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int m = reader.nextInt();
Node[] arr = new Node[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = new Node();
arr[i].index = i;
arr[i].data = reader.nextInt();
}
Arrays.sort(arr, new Comparator<Node>() {
@Override
public int compare(Node arg0, Node arg1) {
return arg1.data - arg0.data;
}
});
Arrays.sort(arr, 0, m, new Comparator<Node>() {
@Override
public int compare(Node arg0, Node arg1) {
return arg0.index - arg1.index;
}
});
for (int i = 0; i < m-1; i++) {
System.out.print(arr[i].data+" ");
}
System.out.println(arr[m-1].data);
}
}
class Node {
int index;
int data;
}