凯撒密码
问题描述 给定一个单词,请使用凯撒密码将这个单词加密。 凯撒密码是一种替换加密的技术,单词中的所有字母都在字母表上向后偏移3位后被替换成密文。即a变为d,b变为e,...,w变为z,x变为a,y变为b,z变为c。 例如,lanqiao会变成odqtldr。 输入格式 输入一行,包含一个单词,单词中只包含小写英文字母。 输出格式 输出一行,表示加密后的密文。 样例输入 lanqiao 样例输出 odqtldr 评测用例规模与约定 对于所有评测用例,单词中的字母个数不超过100。
public void first() {
Scanner sc = new Scanner(System.in);
String s = sc.next();
String res = "";
for (int i = 0; i < s.length(); i++) {
char now =s.charAt(i);
if (now>='x'){
if (now=='x') now = 'a';
else if (now=='y') now = 'b';
else if (now=='z') now = 'c';
res = res + now;
continue;
}
res = res + (char)(now+3);
}
System.out.println(res);
}
反倍数
问题描述 给定三个整数 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。
public void second() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int count=0;
for (int i = 1; i <=n; i++) {
if(i%a!=0&&i%b!=0&&i%c!=0){
count++;
}
}
System.out.println(count);
}
螺旋矩阵
对于一个 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 void third() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();//矩阵行列
int r = sc.nextInt();
int c = sc.nextInt();//要求的行列
int res = m;
int x = 1, y = m;
int turn = 1;//0123代表右下左上
int right = x;
//根据目前的位置和方向,来判断是否退出循环
while (true) {
if (turn == 0) {
if (x==r) break;
y = y + m - 1;//更新坐标
res = res + m - 1;//更新当前结果
m--;//更新下一个距离
turn++;
} else if (turn == 1) {
if (y==c) break;
x = x + n - 1;
res = res + n - 1;
n--;
turn++;
} else if (turn == 2) {
if (x==r) break;
y = y - m + 1;
res = res + m - 1;
m--;
turn++;
} else {
if (y==c) break;
x = x - n + 1;
res = res + n - 1;
turn=0;
n--;
}
}
//处理结果
if (x == r) {
res = turn == 0 ? res + c - y : res + y - c;
}else {
res = turn == 1 ? res + r - x : res + x - r;
}
System.out.println(res);
}
摆动序列
// 问题描述
// 如果一个序列的奇数项都比前一项大,偶数项都比前一项小,则称为一个摆动序列。即 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。
// 如果一个序列的奇数项都比前一项大,偶数项都比前一项小,则称为一个摆动序列。即 a[2i]<a[2i-1], a[2i+1]>a[2i]。
// 小明想知道,长度为 m,每个数都是 1 到 n 之间的正整数的摆动序列一共有多少个。
// 输入格式
// 输入一行包含两个整数 m,n。
// 输出格式
// 输出一个整数,表示答案。答案可能很大,请输出答案除以10000的余数。
// 样例输入
//3 4
// 样例输出
//14
文章引用:蓝桥杯模拟赛 摆动序列 题目+题解
public void four() {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int[][] dp = new int[m + 2][n + 2];
for (int i = 1; i <= n; i++) {
dp[1][i] = n - i + 1;//初始化下一行可以选择的值的数目
}
for (int i = 2; i <= m; i++)
if ((i & 1) == 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;
}
}
int result = (m & 1) == 1 ? dp[m][1] : dp[m][n];
System.out.println(result);
}
村庄通电
问题描述 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。
public void six() {
class village{
int x;
int y;
int height;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public village(int x, int y, int height) {
this.x = x;
this.y = y;
this.height = height;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();//村庄的数量
village[] villages = new village[n];
double cost[][] = new double[n][n];
for (int i = 0; i < n; i++) {
villages[i] = new village(sc.nextInt(), sc.nextInt(), sc.nextInt());
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
cost[i][j] = Math.sqrt((villages[i].x - villages[j].x) * (villages[i].x - villages[j].x) +
(villages[i].y - villages[j].y) * (villages[i].y - villages[j].y)) +
(villages[i].height - villages[j].height)*(villages[i].height - villages[j].height);
}
}
int[] key = new int[n];
boolean[] right = new boolean[n];
dfs(n, 0, cost, key, right);
System.out.println(String.format("%.2f",sum));
}
double sum = Integer.MAX_VALUE;
public double dfs(int n, int curr, double[][] cost, int[] key,boolean[]right) {
if (n == curr) {
double temp=0;
for (int i = 0; i < n - 1; i++) {
temp =cost[key[i]][key[i + 1]]==0? temp + cost[key[i+1]][key[i]]:temp + cost[key[i]][key[i + 1]];
}
sum=Math.min(sum, temp);
return sum;
}else {
for (int i = 0; i < n; i++) {
if (right[i])continue;
if (!right[i]) {
key[curr] = i;
right[i]=true;
}
dfs(n, curr + 1, cost, key, right);
right[i]=false;
}
return sum;
}
}
}