- T1
Description:
将LANQIAO中的字母重新排列,可以得到不同的单词,
如LANQIAO、AAILNOQ等,注意这7个字母都要被用上,单词不一定有具体的英文意义。
请问,总共能排列如多少个不同的单词。
答案:2520
解题思路: A(7,7)/ 2
- T2
Description:
一个包含有2019个结点的无向连通图,最少包含多少条边?
答案:2018
- T3
Description:
在计算机存储中,12.5MB是多少字节?
答案:1.3107200
解题思路:12.5*
1024*
1024
- T4
Description:
由1对括号,可以组成一种合法括号序列:()。
由2对括号,可以组成两种合法括号序列:()()、(())。
由4对括号组成的合法括号序列一共有多少种?
答案:14
卡特兰数列
长度为4 ()()()()
长度为3 (())()() 、()(())() 、()()(())
长度为2 ()((())) 、 ((()))() 、(()())() 、()(()()) 、(())(())、
长度为1 (((()))) 、 (()()()) 、(()(())) 、((())()) 、((()()))
- T6
Description:
给定一个单词,请使用凯撒密码将这个单词加密。
凯撒密码是一种替换加密的技术,单词中的所有字母都在字母表上向后偏 移3位后被替换成密文。
即a变为d,b变为e,…,w变为z,x变为a,y变为b,z变为c。
例如,lanqiao会变成odqtldr。
输入格式
输入一行,包含一个单词,单词中只包含小写英文字母。
输出格式
输出一行,表示加密后的密文。
样例输入
lanqiao
样例输出
odqtldr
评测用例规模与约定
对于所有评测用例,单词中的字母个数不超过100。
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String str = cin.nextLine();
String ans = "";
char temp;
for (int i = 0; i < str.length(); i++) {
temp = str.charAt(i);
if (temp == 'x' || temp == 'y' || temp == 'z'){
ans += (char)(temp - 'z' + 99);
}else {
ans += (char)(temp + 3);
}
}
System.out.println(ans);
}
- T6
Description:
给定三个整数 a, b, c,如果一个整数既不是 a 的整数倍也不是 b 的整数倍还不是 c 的整数倍,则这个数称为反倍数。
请问在 1 至 n 中有多少个反倍数。
输入格式
输入的第一行包含一个整数 n。
第二行包含三个整数 a, b, c,相邻两个数之间用一个空格分隔。
输出格式
输出一行包含一个整数,表示答案。
样例输入
30
2 3 6
样例输出
10
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int a = cin.nextInt();
int b = cin.nextInt();
int c = cin.nextInt();
int ans = 0;
for (int i = 1;i <= n;i++){
if (i % a != 0 && i % b != 0 && i % c != 0)
ans++;
}
System.out.println(ans);
}
- T7
Description:
对于一个 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。
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();//行
int m = cin.nextInt();//列
int r = cin.nextInt();
int c = cin.nextInt();
System.out.println(spiralOrder(n, m, r-1, c-1));
}
public static int spiralOrder(int n, int m, int r, int c) {
int[][] num = new int[n][m];
int row = 0, col = 0;
num[row][col] = 1;
int upBound = 0;
int rightBound = num[0].length - 1;
int leftBound = 0;
int downBound = num.length - 1;
int temp = 1;
while (true) {
for (int i = leftBound; i <= rightBound; i++)
num[upBound][i] = temp++;
if (++upBound > downBound) break;
for (int i = upBound; i <= downBound; i++)
num[i][rightBound] = temp++;
if (--rightBound < leftBound) break;
for (int i = rightBound; i >= leftBound; --i)
num[downBound][i] = temp++;
if (--downBound < upBound) break;
for (int i = downBound; i >= upBound; --i)
num[i][leftBound] = temp++;
if (++leftBound > rightBound) break;
}
return num[r][c];
}
- T8
static int mod = 10000;
static int m,n;
static int[] arr;
static int[][] buff;
static int ans;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
m = cin.nextInt();
n = cin.nextInt();
arr = new int[m+1];
buff = new int[m][2];
ans = 0;
dfs(1);
System.out.println(ans%mod);
}
//思路一,暴力dfs,能得出50%的结果
private static void dfs(int index){
if (index > m){
ans++;
return;
}
if (index % 2 == 0){
for (int i = arr[index-1]-1 ; i >= 1;i--){//偶数位比前一项小
arr[index] = i;
dfs(index+1);
}
}else {
for (int i = arr[index-1]+1 ; i <= n ; i++){//奇数位比前一项大
arr[index] = i;
dfs(index+1);
}
}
}
/**
*思路二:留个坑,有空来补
*/
- T9
Description:
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。
static int[][] villages;
static double[][] distance;
static ArrayList<Integer> list = new ArrayList<>();
static ArrayList<Integer> remain = new ArrayList<>();
static int num;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
num = cin.nextInt();
villages = new int[num][3];
distance = new double[num][num];
for (int i = 0; i < num; i++) {
villages[i][0] = cin.nextInt();
villages[i][1] = cin.nextInt();
villages[i][2] = cin.nextInt();
}
//求到各村庄之间装电缆的价格
for (int i = 0;i < num;i++){// O(n*(n-1)/2)
for (int j = i+1;j < num;j++){
distance[i][j] = Math.sqrt((villages[i][0]-villages[j][0])*(villages[i][0]-villages[j][0])+
(villages[i][1]-villages[j][1])*(villages[i][1]-villages[j][1]))+
(villages[i][2]-villages[j][2])*(villages[i][2]-villages[j][2]);//两个村庄之间的坐标距离加上高度差的平方
}
}
for (int i = 1;i < num;i++)
remain.add(i);
double sum = 0;
list.add(0);
double[] res;
for (int i = 1;i < num;i++){
res = getMinCost();
sum += res[0];
list.add((int)res[1]);
remain.remove(new Integer((int) res[1]));
}
System.out.printf("%.2f",sum);
}
private static double[] getMinCost(){
Iterator<Integer> iterator = list.iterator();//marked
int pos1,pos2;
double[] res = new double[2];
res[0] = Double.MAX_VALUE;
while (iterator.hasNext()){
pos1 = iterator.next();
Iterator<Integer> remainIter = remain.iterator();//remain
while (remainIter.hasNext()){
pos2 = remainIter.next();
if (pos1 < pos2) {
if (distance[pos1][pos2] < res[0]){
res[0] = distance[pos1][pos2];
res[1] = pos2;
}
}else {
if (distance[pos2][pos1] < res[0]){
res[0] = distance[pos2][pos1];
res[1] = pos2;
}
}
}
}
return res;
}
- T10
Description:
小明和朋友们一起去郊外植树,他们带了一些在自己实验室精心研究出的小树苗。
小明和朋友们一共有 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。
static int numOfTrees;
static int[][] tree;
static boolean[] marked;
static boolean[][] conflict;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
numOfTrees = cin.nextInt();
tree = new int[numOfTrees][3];
conflict = new boolean[numOfTrees][numOfTrees];
for (int i = 0; i < numOfTrees; i++) {
tree[i][0] = cin.nextInt();
tree[i][1] = cin.nextInt();
tree[i][2] = cin.nextInt();
}
boolean isConf;
for (int i = 0; i < numOfTrees; i++) {
for (int j = i+1; j < numOfTrees; j++) {
isConf = (tree[i][0]-tree[j][0])*(tree[i][0]-tree[j][0])+(tree[i][1]-tree[j][1])*(tree[i][1]-tree[j][1])
< (tree[i][2]+tree[j][2])*(tree[i][2]+tree[j][2]);
conflict[i][j] = isConf;
conflict[j][i] = isConf;
}
}
int max = -1;
marked = new boolean[numOfTrees];
for (int i = 0; i < numOfTrees; i++) {
marked[i] = true;
max = Math.max(dfs(i),max);
marked[i] = false;
}
cin.close();
System.out.println(max/2);
}
private static int dfs(int index){
int maxSum = 0;
for (int i = 0; i < numOfTrees; i++) {
if (i != index && marked[i] == false && conflict[index][i] == false){
marked[i] = true;
maxSum = Math.max(dfs(i),maxSum);
marked[i] = false;
}
}
return maxSum+tree[index][2]*tree[index][2];
}