# 3.12蓝桥算法打卡

``````import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
public static void main(String[] args) {
boolean []bs=new boolean [10000000];
Arrays.fill(bs, true);
int n=bs.length;
int c=0;
for(int i=2;i*i<n;i++)
if(bs[i])
for(int j=i*i;j<n;j+=i)
bs[j]=false;
for(int i=2;i<n;i++){
if(bs[i]) c++;
if(c==100002) {
c=i;
break;
}
}
System.out.println(c);
}
}
``````

``````import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
static int ant = 0;
static int[] list = {1,2,3,4,5,6,7,8,9,10};

public static void main(String[] args) {
dfs(0);
System.out.println(ant);
}

private static void dfs(int i) {
if (i == list.length) {
if (check()) {
ant++;
}
}
for (int j = i; j < list.length; ++j) {
swap(j,i);
dfs(i+1);
swap(j,i);
}
}

private static void swap(int i, int j) {
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}

private static boolean check() {
int n = list.length;
for (int i = 0; i < n-1; i++) {
if (Math.abs(list[i]-list[i+1]) == 1) {
return false;
}
}
return true;
}
``````

...全文
5 回复 打赏 收藏 举报

4.3w+

2023-03-12 22:32

2023-03-12 22:30

刷题！