[AtCode 4104] Small and Large Integers

题目链接:https://abc093.contest.atcoder.jp/tasks/abc093_b?lang=en

这个题虽然很水,但是还是很容易踩坑,比如我,直接想到了[b,a]之间的长度和k的比较,其实不对,用介于(k+2k)/2之间的数验证一下就不对,考虑到对称性,应该是和2k进行比较。

做题还是应该沉心静气,不可眼高手低。

AC代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long a = scanner.nextLong();
        long b = scanner.nextLong();
        long k = scanner.nextLong();
        if(b-a+1 <= 2*k) {
            for(long i = a;i <= b;i++) {
                System.out.println(i);
            }
        } else {
            long i,j;
            for(i = a;i < k+a;i++) {
                System.out.println(i);
            }
            for(j = b-k+1;j <= b;j++) {
                System.out.println(j);
            }
        }
    }
}
//这是错误代码:
//用数据 4 7 3验证即可知道结果不对
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long a = scanner.nextLong();
        long b = scanner.nextLong();
        long k = scanner.nextLong();
        if(b-a+1 <= k) {
            for(long i = a;i <= b;i++) {
                System.out.println(i);
            }
        } else {
            long i,j;
            for(i = a;i < k+a;i++) {
                System.out.println(i);
            }
            for(j = b-k+1;j <b;j++) {
                if(j == i-1)
                    continue;
                System.out.println(j);
            }
            System.out.print(b);
        }
    }
}
上一篇:19.3.2 [LeetCode 99] Recover Binary Search Tree


下一篇:LeetCode——1438. 绝对差不超过限制的最长连续子数组(Longest Continuous Subarray With Absolute Diff...)[中等]——分析及代码(Java)