题目链接:
这个题虽然很水,但是还是很容易踩坑,比如我,直接想到了[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