package 蓝桥算法训练__普及组.Day8;
import java.util.Scanner;
public class T1 {
static int n;
static int[] a = new int[1010];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
}
int ans = (int) 1e9;// ans用来存答案
int temp = 0;// temp用来存开每条门时 牛需要走的距离和
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
// 开第i条门 属于该门的牛不用加步数
if (i != j) {
if (j > i){
temp += a[j] * (j - i);
} else {
temp += a[j] * (n - i + j);
}
}
}
ans = Math.min(ans, temp);
// System.out.println(ans + " " + i);
temp = 0;
}
System.out.println(ans);
}
}
![](https://img-blog.csdnimg.cn/img_convert/ddea16ee81eeb38596556ec8302bc256.png)
第二题:32. 调整数组顺序使奇数位于偶数前面 - AcWing题库
package 蓝桥算法训练__普及组.Day8;
import java.util.Scanner;
/**
* @author snippet
* @data 2023-02-12
* 32. 调整数组顺序使奇数位于偶数前面 - AcWing题库
*/
public class T2 {
public static void reOrderArray(int[] array) {
int left = 0, right = array.length-1;
while (left < right) {
// 奇数
while (left < right && array[left] % 2 == 1) left++;
// 偶数
while (left < right && array[right] % 2 == 0) right--;
if (left < right) {
int temp = array[left];
array[left] = array[right];
array[right] = temp;
}
}
}
// 数据测试
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
reOrderArray(a);
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
}
![](https://img-blog.csdnimg.cn/img_convert/9e593f3499ed0ed17bc4a448204727d5.png)
package 蓝桥算法训练__普及组.Day8;
import java.util.Scanner;
import java.util.Stack;
/**
* @author snippet
* @data 2023-02-12
* 1347. 双重回文 - AcWing题库
*/
public class T3 {
/**
* 判断在2-10进制中是否回文
* @param num
* @param bs
* @return
*/
static boolean check(int num, int bs) {
int t = num;
// 栈用来存bs进制下的数字num
Stack<Integer> stack = new Stack<>();
while (t > 0) {
stack.push(t % bs);
t /= bs;
}
// 判断是否回文
while (stack.size() > 0) {
if (stack.peek() != num % bs) return false;
stack.pop();
num /= bs;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();// 需要求n个数
int m = sc.nextInt();// 从数字m开始
int count = 0;
m++;
while (true) {
if (count == n) {
break;
}
// 这个temp用来记录数字m在2-10进制中满足回文的个数
int temp = 0;
for (int i = 2; i <= 10; i++) {
if (check(m, i)) temp++;
}
// 满足两个及以上进制的数则直接输出
if (temp >= 2) {
System.out.println(m);
count++;
}
m++;
}
}
}
![](https://img-blog.csdnimg.cn/img_convert/4e96bef6ddc27816bf962980171ef539.png)
package 蓝桥算法训练__普及组.Day8;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author snippet
* @data 2023-02-12
* 1945. 奶牛棒球 - AcWing题库
*/
// 双指针
public class T4 {
static int n,ans;
static int[] a = new int[1010];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a, 1, n+1);
// 需要满足l1<l2 && l2<=2*l1
for (int i = 1; i <= n-2; i++) {
for (int j = i+1; j <= n-1; j++) {
// 表示前两头牛的距离
int l1 = a[j] - a[i];
// 左指针用来指到不满足后两头牛距离>前两头牛距离的极点
// 也就是后两头牛距离>前两头牛距离的起点
int left = j+1;
// 右指针指向满足后两头牛的距离<=前两头牛距离的两倍
// 也就是后两头牛的距离<=前两头牛距离的两倍的终点
int right = j+1;
while (left <= n && a[left] - a[j] < l1) left++;
while (right <= n && a[right] - a[j] <= 2 * l1) right++;
// [左指针起点,右指针终点)
ans += right - left;
//System.out.println(left+"+"+right);
}
}
System.out.println(ans);
}
}
![](https://img-blog.csdnimg.cn/img_convert/11a539142cba974aff5900b93881709c.png)