能迅速解决的问题,千万不要拖拉,不然后面根本没有时间思考大题!
2000是否闰年?
是:
答案:125
用你掌握的最好的方法做
public static void main(String[] args) {
// 判断方格是否在圆内
int r = 1000;
int cnt = 0;
for (int x = 1; x <= r; x++) {
for (int y = 1; y <= r; y++) {
if (x * x + y * y <= r * r) {
cnt++;
}
}
}
System.out.println(cnt * 4);
}
答案:3137548
public static void main(String[] args) throws FileNotFoundException {
// 复数幂:(a+bi)*(2+3i)=2*a-3*b+(3*a+2*b)i
BigInteger a = BigInteger.valueOf(2);
BigInteger two = BigInteger.valueOf(2);
BigInteger b = BigInteger.valueOf(3);
BigInteger three = BigInteger.valueOf(3);
BigInteger aa = null;
BigInteger bb = null;
for (int i = 1; i < 123456; i++) {// x次方,两两相乘的次数为1,总共x-1次
aa = a.multiply(two).subtract(b.multiply(three));
bb = a.multiply(three).add(b.multiply(two));
a = aa;
b = bb;
}
String temp = b.toString();
if (b.compareTo(BigInteger.ZERO) >= 0) {
temp = "+" + b.toString();
}
System.setOut(new PrintStream(new File("E:\\蓝桥杯训练\\题目文件\\复数幂.txt")));
System.out.println(a.toString() + temp + "i");
}
答案很长,此处略
代码:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int[] a = new int[N];
int[] b = new int[N];
int[] c = new int[N];
for (int i = 0; i < N; i++) {
a[i] = scanner.nextInt();
}
for (int i = 0; i < N; i++) {
b[i] = scanner.nextInt();
}
for (int i = 0; i < N; i++) {
c[i] = scanner.nextInt();
}
Arrays.sort(a);// 保险起见,排个序
Arrays.sort(b);
Arrays.sort(c);
// 数据太大,暴力过不了,只能选择优化
long sum = 0;// 怕数据太大
for (int i = 0; i < N; i++) {// 遍历b数组
int p = 0;// 去a中找比b[i]小的元素有多少个
while (p < N && a[p] < b[i]) {
p++;
}
int q = 0;// 去c中找比b[i]小于等于的元素有多少个
while (q < N && c[q] <= b[i]) {
q++;
}
sum += p * (N - q);// 比b[i]小的个数*比b[i]大的个数=b[i]的递增三元组
}
System.out.println(sum);
}
!!!!错了
指针p和q 应该写在for循环之外!!!一看就是没debug,我错了,我明天一定好好debug!!
// 数据太大,暴力过不了,只能选择优化
long sum = 0;// 怕数据太大
//犯错:
int p = 0;// 去a中找比b[i]小的元素有多少个
int q = 0;// 去c中找比b[i]小于等于的元素有多少个
//指针应该在外面!!
for (int i = 0; i < N; i++) {// 遍历b数组
while (p < N && a[p] < b[i]) {
p++;
}
while (q < N && c[q] <= b[i]) {
q++;
}
sum += p * (N - q);// 比b[i]小的个数*比b[i]大的个数=b[i]的递增三元组
}
System.out.println(sum);
方法一:根据id排序
自己写的,错了很多细节的地方!不容易!
public class Main8_2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();// N份日志
int D = scanner.nextInt();// 时间段
int K = scanner.nextInt();// k个赞
int[][] a = new int[N][2];
for (int i = 0; i < N; i++) {
a[i][0] = scanner.nextInt();
a[i][1] = scanner.nextInt();
}
// 按照id排序
Arrays.sort(a, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
// TODO Auto-generated method stub
if (o1[1] == o2[1])
return o1[0] - o2[0];
return o1[1] - o2[1];
}
});
for (int i = 0; i < N; i++) {
System.out.println(Arrays.toString(a[i]));
}
HashMap<Integer, Integer> map = new HashMap<>();
TreeSet<Integer> set = new TreeSet<>();// 存放id,不会重复且自动排序
for (int i = 0; i < N; i++) {
int id = a[i][1];
int ts = a[i][0];
int pri = i;
while (i < a.length && a[i][1] == id) {// 统计编号id有多少个
i++;
}
for (int j = pri; j < i; j++) {
if (a[j][0] - ts < D) {// 在[ts,ts+D)时间内
map.put(id, map.getOrDefault(id, 0) + 1);
if (map.get(id) >= K) {
set.add(id);
}
} else {// 超出
if (map.get(id) != null) {
map.put(id, map.get(id) - 1);
}
// 犯错误!!多调试几遍,想三种测试案例!!
pri = pri + 1;
ts = a[pri][0];
j--;
}
}
i--;
}
for (Integer temp : set) {
System.out.println(temp);
}
}
}
方法二:根据时间排序
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();// N份日志
int D = scanner.nextInt();// 时间段
int K = scanner.nextInt();// k个赞
int[][] a = new int[N][2];
for (int i = 0; i < N; i++) {
a[i][0] = scanner.nextInt();// ts时刻
a[i][1] = scanner.nextInt();// 编号id
}
// 按照时间排序
Arrays.sort(a, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
// TODO Auto-generated method stub
return o1[0] - o2[0];
}
});
// 尺取法(双指针法)
HashMap<Integer, Integer> map = new HashMap<>();
TreeSet<Integer> set = new TreeSet<>();
int right = 0;
for (int left = 0; left < N; left++) {
while (right < N && a[right][0] - a[left][0] < D) {// 满足在此区间内的所有id都+1
int id = a[right][1];
map.put(id, map.getOrDefault(id, 0) + 1);
if (map.get(id) >= K)
set.add(id);
right++;
}
// 此时while结束,为不满足条件的情况
// 更新id的值,即将更新i,那么又会从下一个时间a[left][0]开始,判断下一轮的区间
if (map.get(a[left][1]) != null)// id有点赞数才进行-1
map.put(a[left][1], map.get(a[left][1] - 1));
}
}
public class Main9 {
static class Point {
int x, y;
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
static int N;
static int ans = 0;
static char[][] grid;
static boolean[][] flag;
static int[][] dir = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
N = scanner.nextInt();
scanner.nextLine();// 吃掉换行符,debug之后才反应过来!
grid = new char[N][N];// 存信息
flag = new boolean[N][N];
for (int i = 0; i < N; i++) {
grid[i] = scanner.nextLine().toCharArray();
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (grid[i][j] == '#' && !flag[i][j]) {// 是陆地,则进行bfs——一定要判断是否访问过!
flag[i][j] = true;
if (bfs(i, j)) {// 被淹没
ans++;// 统计被淹没的岛屿数量
}
}
}
}
System.out.println(ans);
}
public static boolean bfs(int x, int y) {
Queue<Point> queue = new LinkedList<>();// 存储的是陆地的位置
queue.add(new Point(x, y));// 加入传入的陆地位置
int cntOfBlock = 0;// 统计总陆地数量
int cntOfSubmerge = 0;// 统计淹没的陆地
while (!queue.isEmpty()) {
Point temp = queue.poll();// 我忘了。。。。debug出来的!
x = temp.x;
y = temp.y;
cntOfBlock++;// 进入循环,说明为陆地
boolean isSubmerge = false;// 判断该陆地周围是否有海洋
for (int i = 0; i < 4; i++) {// 上下左右
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (nx < 0 || ny < 0 || nx >= N || ny >= N)// 剪枝
continue;
if (grid[nx][ny] == '.') {
isSubmerge = true;
} else if (grid[nx][ny] == '#' && !flag[nx][ny]) {// 是陆地并且该地没有被访问
flag[nx][ny] = true;// 一定要标记
queue.add(new Point(nx, ny));// 加入队列
}
} // for循环结束判断四周是否有海洋
if (isSubmerge) {
cntOfSubmerge++;
}
}
return cntOfBlock == cntOfSubmerge;// 如果两个相等,则说明被淹没
}
}
end.