出一個正整數N和長度L,找出一段長度大於等於L的連續非負整數,他們的和恰好為N。答案可能有多個,我我們需要找出長度最小的那個。
例如 N = 18 L = 2:
5 + 6 + 7 = 18
3 + 4 + 5 + 6 = 18
都是滿足要求的,但是我們輸出更短的 5 6 7
輸入描述:
輸入數據包括一行: 兩個正整數N(1 ≤ N ≤ 1000000000),L(2 ≤ L ≤ 100)
輸出描述:
從小到大輸出這段連續非負整數,以空格分隔,行末無空格。如果沒有這樣的序列或者找出的序列長度大於100,則輸出No
示例1
輸入
18 2
輸出
5 6 7

從L到100之間找出滿足條件的a1,如果有,則輸出a1,否則輸出No
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while ( in .hasNext()) {
int N = in .nextInt();
int L = in .nextInt();
boolean flag = false;
for (int i = L; i <= 100; i++) {
if ((2 * N + i - i * i) % (2 * i) == 0) {
flag = true;
int a1 = (2 * N + i - i * i) / (2 * i);
if (a1 < 0) continue;
for (int j = 0; j < i - 1; j++) {
int a = a1 + j;
System.out.print(a + " ");
}
System.out.println(a1 + i - 1);
break;
}
}
if (flag == false)
System.out.println("No");
}
}
}
