第一题
问题描述
在计算机存储中,12.5MB是多少字节?
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
12.5*1024*1024=13,107,200
第二题
问题描述
由1对括号,可以组成一种合法括号序列:()。
由2对括号,可以组成两种合法括号序列:()()、(())。
由4对括号组成的合法括号序列一共有多少种?
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
列举: ()()()()
(())()() ()()(()) ()(())() (())(())
(()())() ()(()()) ((()))() ()((()))
(()()()) (()(())) ((())()) ((()())) (((())))
第三题
问题描述
将LANQIAO中的字母重新排列,可以得到不同的单词,如LANQIAO、AAILNOQ等,注意这7个字母都要被用上,单词不一定有具体的英文意义。
请问,总共能排列如多少个不同的单词。
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
7!/2 = 2520
第四题
问题描述
一个包含有2019个结点的无向连通图,最少包含多少条边?
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
2019-1=2018
最少:n-1 最多:n*(n-1)/2
第五题
问题描述
给定三个整数 a, b, c,如果一个整数既不是 a 的整数倍也不是 b 的整数倍还不是 c 的整数倍,则这个数称为反倍数。
请问在 1 至 n 中有多少个反倍数。
输入格式
输入的第一行包含一个整数 n。
第二行包含三个整数 a, b, c,相邻两个数之间用一个空格分隔。
输出格式
输出一行包含一个整数,表示答案。
样例输入
30
2 3 6
样例输出
10
样例说明
以下这些数满足要求:1, 5, 7, 11, 13, 17, 19, 23, 25, 29。
评测用例规模与约定
对于 40% 的评测用例,1 <= n <= 10000。
对于 80% 的评测用例,1 <= n <= 100000。
对于所有评测用例,1 <= n <= 1000000,1 <= a <= n,1 <= b <= n,1 <= c <= n。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
boolean[] vis = new boolean[1000000 + 5];// false,1-n
if (a == 1 || b == 1 || c == 1) {
System.out.println(0);
return;
}
//打表了,好像也不至于,没怎么细究
//int limit = n / Math.min(Math.min(a, b), c) + 1;//优化,缩小循环范围
for (int i = 1; i <= n; i++) {
if (i * a <= n) {
vis[i * a] = true;
}
if (i * b <= n) {
vis[i * b] = true;
}
if (i * c <= n) {
vis[i * c] = true;
}
}
int count = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
count++;
}
}
System.out.println(count);
return;
}
}
第六题
问题描述
给定一个单词,请使用凯撒密码将这个单词加密。
凯撒密码是一种替换加密的技术,单词中的所有字母都在字母表上向后偏移3位后被替换成密文。即a变为d,b变为e,…,w变为z,x变为a,y变为b,z变为c。
例如,lanqiao会变成odqtldr。
输入格式
输入一行,包含一个单词,单词中只包含小写英文字母。
输出格式
输出一行,表示加密后的密文。
样例输入
lanqiao
样例输出
odqtldr
评测用例规模与约定
对于所有评测用例,单词中的字母个数不超过100。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
char[] strTemp = str.toCharArray();
for (int i = 0; i < strTemp.length; i++) {
//懒,还不用动脑筋
if (strTemp[i] == 'x') {
strTemp[i] = 'a';
continue;
}
if (strTemp[i] == 'y') {
strTemp[i] = 'b';
continue;
}
if (strTemp[i] == 'z') {
strTemp[i] = 'c';
continue;
}
strTemp[i] = (char) ((int) strTemp[i] + 3);
}
System.out.println(String.valueOf(strTemp));
return;
}
}
第七题
问题描述
如果一个序列的奇数项都比前一项大,偶数项都比前一项小,则称为一个摆动序列。即 a[2i]<a[2i-1], a[2i+1]>a[2i]。
小明想知道,长度为 m,每个数都是 1 到 n 之间的正整数的摆动序列一共有多少个。
输入格式
输入一行包含两个整数 m,n。
输出格式
输出一个整数,表示答案。答案可能很大,请输出答案除以10000的余数。
样例输入
3 4
样例输出
14
样例说明
以下是符合要求的摆动序列:
2 1 2
2 1 3
2 1 4
3 1 2
3 1 3
3 1 4
3 2 3
3 2 4
4 1 2
4 1 3
4 1 4
4 2 3
4 2 4
4 3 4
评测用例规模与约定
对于 20% 的评测用例,1 <= n, m <= 5;
对于 50% 的评测用例,1 <= n, m <= 10;
对于 80% 的评测用例,1 <= n, m <= 100;
对于所有评测用例,1 <= n, m <= 1000。
1.递归虽然直观好理解不容易出错,但是只能拿到很少的分,后面的数据耗时是指数级的。
2.普通递归,到9,9还很快,到10,10就超时了,差不多50%样例分,还有可能得不到。
3.记忆性递归,优化后,以空间换时间,差不多数据为460,460——480,480,一定可以拿到80%样例分,感觉就还可以了。
4.dp(动态规划),轻轻松松过样例,但是不好理解,实现起来比较巧妙,100%样例翻10倍在时间上也只比一秒多一点。
import java.util.Scanner;
public class Main {
static int m;
static int n;
static Scanner scanner;
static long count;
static final int MOD = 10000;
static long[][] mem = new long[1001][1001];
public static void main(String[] args) {
scanner = new Scanner(System.in);
m = scanner.nextInt();
n = scanner.nextInt();
count = 0;
long ago = System.currentTimeMillis();
for (int i = 2; i <= n; i++) {
count = (count + dfs(i, 1)) % MOD;
}
System.out.println(count);
System.err.println(System.currentTimeMillis() - ago);
}
static long dfs(int pre, int index) {
long ans = 0;
index++;
if (index > m) {
return 1;
}
if (mem[pre][index] != 0) {
return mem[pre][index];
}
if (index % 2 == 0) {
for (int i = 1; i < pre; i++) {
ans = (ans + dfs(i, index)) % MOD;
}
}
if (index % 2 == 1) {
for (int i = pre + 1; i <= n; i++) {
ans = (ans + dfs(i, index)) % MOD;
}
}
mem[pre][index] = ans;
return ans;
}
}
1.第一行:dp[1][i] = n - i + 1
初始化,为下一行可以选择的数值个数
2.偶数行:dp[i][j] = dp[i - 1][j + 1] + dp[i][j - 1]
比前一项小,可以选择的数值比前一项少一项,相当于选择小于等于前一项的方案数量,并且加上之前已经计算过的值
3.奇数行:dp[i][j] = dp[i - 1][j - 1] + dp[i][j + 1]
比前一项大,选择大于等于前一项的方案数量
4.答案,二维数组第一个属性为序列长度,第二个属性为数量,奇数长度为左数第一个,偶数长度为右数第一个(看代码循环体)
第一行 1 2 3 4
第二行 0 1 1 2 1 2 3
第三行 0 2 3 4 2 3 4 3 4 2 3 4 3 4 4
...
运行结果:
4 4
4 3 2 1
3 5 6 6 //5 = 3 + 2
14 14 11 6 //11 = 6 + 5 14 = 11 + 3
14 25 31 31 //25 = 11 + 14
31
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[][] dp = new int[1004][1004];
int m, n;
Scanner scanner = new Scanner(System.in);
m = scanner.nextInt();
n = scanner.nextInt();
//long ago = System.currentTimeMillis();
for (int i = 1; i <= n; i++) {
dp[1][i] = n - i + 1;
}
for (int i = 2; i <= m; i++) {
if (i % 2 == 1) {
for (int j = n; j >= 1; j--) {
dp[i][j] = (dp[i - 1][j - 1] + dp[i][j + 1]) % 10000;
}
} else {
for (int j = 1; j <= n; j++) {
dp[i][j] = (dp[i - 1][j + 1] + dp[i][j - 1]) % 10000;
}
}
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(dp[i][j] + "\t");
}
System.out.println();
}
System.out.println((m % 2 == 1 ? dp[m][1] : dp[m][n]));
//System.err.println(System.currentTimeMillis() - ago);
return;
}
}
第八题
问题描述
对于一个 n 行 m 列的表格,我们可以使用螺旋的方式给表格依次填上正整数,我们称填好的表格为一个螺旋矩阵。
例如,一个 4 行 5 列的螺旋矩阵如下:
1 2 3 4 5
14 15 16 17 6
13 20 19 18 7
12 11 10 9 8
输入格式
输入的第一行包含两个整数 n, m,分别表示螺旋矩阵的行数和列数。
第二行包含两个整数 r, c,表示要求的行号和列号。
输出格式
输出一个整数,表示螺旋矩阵中第 r 行第 c 列的元素的值。
样例输入
4 5
2 2
样例输出
15
评测用例规模与约定
对于 30% 的评测用例,2 <= n, m <= 20。
对于 70% 的评测用例,2 <= n, m <= 100。
对于所有评测用例,2 <= n, m <= 1000,1 <= r <= n,1 <= c <= m。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int x = scanner.nextInt();
int y = scanner.nextInt();
int[][] vis = new int[1005][1005];
int num = 1;
int i = 0;
int j = 0;
vis[i][j] = num;
long ago = System.currentTimeMillis();
//不建议循环判断的时候进行运算,看似简洁了但是更耗时,可以添加一个变量存放该值
while (num < (n * m)) {
while (j + 1 < m && vis[i][j + 1] == 0) {
num++;
vis[i][++j] = num;
}
while (i + 1 < n && vis[i + 1][j] == 0) {
num++;
vis[++i][j] = num;
}
while (j - 1 >= 0 && vis[i][j - 1] == 0) {
num++;
vis[i][--j] = num;
}
while (i - 1 >= 0 && vis[i - 1][j] == 0) {
num++;
vis[--i][j] = num;
}
}
System.out.println(vis[x - 1][y - 1]);
// for (int k = 0; k < n; k++) {
// for (int k2 = 0; k2 < m; k2++) {
// System.out.print(vis[k][k2]+"\t");
// }System.out.println();
// }
System.err.println(System.currentTimeMillis() - ago);
return;
}
}
第九题
问题描述
2015年,全中国实现了户户通电。作为一名电力建设者,小明正在帮助一带一路上的国家通电。
这一次,小明要帮助 n 个村庄通电,其中 1 号村庄正好可以建立一个发电站,所发的电足够所有村庄使用。
现在,这 n 个村庄之间都没有电线相连,小明主要要做的是架设电线连接这些村庄,使得所有村庄都直接或间接的与发电站相通。
小明测量了所有村庄的位置(坐标)和高度,如果要连接两个村庄,小明需要花费两个村庄之间的坐标距离加上高度差的平方,形式化描述为坐标为 (x_1, y_1) 高度为 h_1 的村庄与坐标为 (x_2, y_2) 高度为 h_2 的村庄之间连接的费用为
sqrt((x_1-x_2)(x_1-x_2)+(y_1-y_2)(y_1-y_2))+(h_1-h_2)*(h_1-h_2)。
在上式中 sqrt 表示取括号内的平方根。请注意括号的位置,高度的计算方式与横纵坐标的计算方式不同。
由于经费有限,请帮助小明计算他至少要花费多少费用才能使这 n 个村庄都通电。
输入格式
输入的第一行包含一个整数 n ,表示村庄的数量。
接下来 n 行,每个三个整数 x, y, h,分别表示一个村庄的横、纵坐标和高度,其中第一个村庄可以建立发电站。
输出格式
输出一行,包含一个实数,四舍五入保留 2 位小数,表示答案。
样例输入
4
1 1 3
9 9 7
8 8 6
4 5 4
样例输出
17.41
评测用例规模与约定
对于 30% 的评测用例,1 <= n <= 10;
对于 60% 的评测用例,1 <= n <= 100;
对于所有评测用例,1 <= n <= 1000,0 <= x, y, h <= 10000。
1.最小生成树问题,主要有Kruskal算法,Prim算法。
2.这个题目有起点,建议用"加点法"的Prim,而不用"加边法"的Kruskal
import java.util.Scanner;
public class Main {
static int maxn = 1000 + 5;
static int n;
static double[][] a = new double[maxn][maxn];// 构造加权无向图
static double[] d = new double[maxn];// 部分费用
static double ans;// 计算总费用
static boolean[] visit = new boolean[maxn];// 标记是否已经在生成树中
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
for (int i = 0; i <= n; i++) {// 初始化
for (int j = 0; j <= n; j++) {
a[i][j] = Double.MAX_VALUE;
}
d[i] = Double.MAX_VALUE;
}
int[][] xyh = new int[maxn][3 + 5];
for (int i = 1; i <= n; i++) {// 读取数据
for (int j = 1; j <= 3; j++) {
xyh[i][j] = scanner.nextInt();
}
}
for (int i = 1; i <= n - 1; i++) {// 构造加权无向图
for (int j = i + 1; j <= n; j++) {
a[i][j] = a[j][i] = Math.sqrt(Math.pow((xyh[i][1] - xyh[j][1]),
2) + Math.pow((xyh[i][2] - xyh[j][2]), 2))
+ Math.pow((xyh[i][3] - xyh[j][3]), 2);
}
}
Prim();
for (int i = 2; i <= n; i++) {
ans += d[i];
}
System.out.println(String.format("%.2f", ans));
return;
}
public static void Prim() {
d[1] = 0;
// 最小生成树一定是n-1条边构成的连通图,贪婪算法,每次都寻找局部最优,需要数学验证
for (int i = 1; i < n; i++) {
int x = 0;
for (int j = 1; j <= n; j++) {
// 不能选择已经被标记,否则可能形成环路
if (!visit[j] && (x == 0 || d[j] < d[x])) {
x = j;
}
}
visit[x] = true;
// 选择当前节点和那些还未被标记的点之间的最小权值
for (int y = 1; y <= n; y++) {
if (!visit[y]) {
d[y] = Math.min(d[y], a[x][y]);
}
}
}
}
}
第十题
问题描述
小明和朋友们一起去郊外植树,他们带了一些在自己实验室精心研究出的小树苗。
小明和朋友们一共有 n 个人,他们经过精心挑选,在一块空地上每个人挑选了一个适合植树的位置,总共 n 个。他们准备把自己带的树苗都植下去。
然而,他们遇到了一个困难:有的树苗比较大,而有的位置挨太近,导致两棵树植下去后会撞在一起。
他们将树看成一个圆,圆心在他们找的位置上。如果两棵树对应的圆相交,这两棵树就不适合同时植下(相切不受影响),称为两棵树冲突。
小明和朋友们决定先合计合计,只将其中的一部分树植下去,保证没有互相冲突的树。他们同时希望这些树所能覆盖的面积和(圆面积和)最大。
输入格式
输入的第一行包含一个整数 n ,表示人数,即准备植树的位置数。
接下来 n 行,每行三个整数 x, y, r,表示一棵树在空地上的横、纵坐标和半径。
输出格式
输出一行包含一个整数,表示在不冲突下可以植树的面积和。由于每棵树的面积都是圆周率的整数倍,请输出答案除以圆周率后的值(应当是一个整数)。
样例输入
6
1 1 2
1 4 2
1 7 2
4 1 2
4 4 2
4 7 2
样例输出
12
评测用例规模与约定
对于 30% 的评测用例,1 <= n <= 10;
对于 60% 的评测用例,1 <= n <= 20;
对于所有评测用例,1 <= n <= 30,0 <= x, y <= 1000,1 <= r <= 1000。
解法待定,先给一种
import java.util.Scanner;
public class Main {
static final int MAXN = 1000 + 5;
static int n;
static int[] x = new int[MAXN];
static int[] y = new int[MAXN];
static int[] r = new int[MAXN];
static boolean[] vis = new boolean[MAXN];
static int ans;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
for (int i = 0; i < n; i++) {
x[i] = scanner.nextInt();
y[i] = scanner.nextInt();
r[i] = scanner.nextInt();
}
ans = 0;
dfs(0, 0);
System.out.println(ans);
return;
}
public static void dfs(int step, int sum) {
if (step == n) {
ans = Math.max(ans, sum);
return;
}
for (int i = 0; i < n; i++) {
if (!vis[i]) {
int tmp = r[i];
if (!cal(i))//如果冲突就不种
r[i] = 0;
vis[i] = true;
dfs(step + 1, sum + r[i] * r[i]);
vis[i] = false;
r[i] = tmp;
}
}
}
public static boolean cal(int i) {
for (int j = 0; j < n; j++) {
if (i != j && vis[j]) {
int dis = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j])
* (y[i] - y[j]) - (r[i] + r[j]) * (r[i] + r[j]);
if (dis < 0)
return false;
}
}
return true;
}
}