其他分享
首页 > 其他分享> > [AtCode 4104] Small and Large Integers

[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);
        }
    }
}

标签:scanner,nextLong,long,System,Large,Small,AtCode,public,Scanner
来源: https://www.cnblogs.com/youpeng/p/10545310.html