The sequence sums out a positive integer N and a length L, finds a continuous non-negative integer whose length is greater than or equal to L, and their sum is exactly N. There may be multiple answers. We need to find the one with the smallest length.

Find a positive integer N and length L, find a continuous non-negative integer with a length greater than or equal to L, and their sum is exactly N. There may be multiple answers. We need to find the one with the smallest length.
For example, N = 18 L = 2:
5 + 6 + 7 = 18
3 + 4 + 5 + 6 = 18
are all satisfied, but we output a shorter 5 6 7

Input description: The
input data includes one line: two positive integers N (1 ≤ N ≤ 1000000000), L (2 ≤ L ≤ 100)

Output description:
output this continuous non-negative integer from small to large, separated by spaces, and no spaces at the end of the line. If there is no such sequence or the length of the sequence found is greater than 100, then output No
example 1
input
18 2
output
5 6 7

 

 

Find the a1 that satisfies the condition from L to 100, if there is, then output a1, otherwise output 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");
}
}
}

Guess you like

Origin www.cnblogs.com/wen-/p/12671396.html