例如 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
18 2
5 6 7
30 13
No
无法由非负整数构成
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scan=new Scanner(System.in); int N=scan.nextInt(); int L=scan.nextInt(); int first; int last; for(;L<=100;L++){ first=((2*N/L)-L+1)/2; last=first+L-1; if(((first+last)*L)/2==N){ for(int i=0;i<L-1;i++){ System.out.print(first+" "); first++; } System.out.print(last); return; } } System.out.print("No"); return; } }java版本
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int N= input.nextInt(); int L= input.nextInt(); int b; int a; int flag=0; for(int i=L;i<101;i++){ a= N/i-(i-1)/2; b=i*a; for(int k=0;k<i;k++){ b=b+k;} if(b==N){ for(int j=0;j<i;j++){ if(j<i-1){ int c=a+j; flag=1; System.out.print(c+" "); } else if(j==i-1){ int c=a+j; flag=1; System.out.print(c); } } break; } if(i==100 && flag==0){ System.out.print("No"); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner input = new Scanner(System.in); int N = input.nextInt(); int L = input.nextInt(); show(N,L); } public static int show(int N,int L){ int i = L; while(i <= 100){ int a = (2*N - i*(i-1))/(2*i); if((2*N - i*(i-1))%(2*i) == 0 && a >= 0){ for(int j = 0;j < i-1;j++){ System.out.print(a + " "); a = a + 1; } System.out.print(a); return 0; } i = i + 1; } if(i > 100){ System.out.print("No"); return 0; } return 0; } }
package mypack_5; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Demo2 { public static void main(String[] args) { //就是不喜欢把函数直接写在主方法里面...^^^... function(); } public static void function() { Scanner scan = new Scanner(System.in); while (scan.hasNext()) { int N = scan.nextInt(); int L = scan.nextInt(); boolean flag = false; for (int i = L; i <= 100; i++) { if ((2 * N + i - i * i)>=0 &&(2 * N + i - i * i) % (2 * i) == 0) { flag = true; int first = (2 * N + i - i * i) / (2 * i); for (int j = 0; j < i - 1; j++) { int next = first + j; System.out.print(next + " "); } System.out.print(first + i - 1); break; } } if (flag == false) System.out.println("No"); } } } 也是使用了等差数列公式,但是对前面的代码进行了一点小小的改进。让代码更加健壮。 改进之处就是在第一个if判断里面加入了(2 * N + i - i * i)>=0 这个条件。 如果不加这个条件,如果你自己在本地测试的时候输入N=2 , L=2 ,那么结果会输出-1 0 1 2 不知道为啥如果不加上这个判断条件,线上的用例都也可以通过...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = 0; int L = 0; if (scanner.hasNextInt()) { N = scanner.nextInt(); } if (scanner.hasNextInt()) { L = scanner.nextInt(); } int a = 0; double t = 0; for (int i = L; i <= 100; i++) { t = N / (i * 1.0) - (i * 1.0 - 1) / 2; a = (int) t; if ((double) a == t) { for (int j = 0; j < i; j++, a++) { System.out.print(a); if (j < i - 1) { System.out.print(" "); } } break; } if (a < 0 || i == 100) { System.out.println("No"); break; } } } }
运行时间:65ms
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); int l=scanner.nextInt(); boolean findResult=false; if(n>=l-1){ for(int i=l;i<=Math.min(100,n+1);i++){ if(i%2==1 && n%i==0){ System.out.print(n/i-i/2); for(int j=1;j<i;j++){ int temp=n/i-i/2+j; System.out.print(" "+temp); } findResult=true; break; }else if(i%2==0 && n%i!=0 && (n*2)%i==0){ System.out.print(n/i-i/2+1); for(int j=1;j<i;j++){ int temp=n/i-i/2+1+j; System.out.print(" "+temp); } findResult=true; break; } } } if(!findResult){ System.out.print("No"); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] nl = sc.nextLine().split(" "); int n = Integer.parseInt(nl[0]); int l = Integer.parseInt(nl[1]); int num = 0; for(int i = 1; i < l; ++i) num += i; int base = -1; while(l < 101){ if(num <= n && (n - num) % l == 0){ base = (n - num) / l; break; } num += l++; } if(base == -1) System.out.println("No"); else{ System.out.print(base); for(int i = 1; i < l; ++i){ System.out.print(' '); System.out.print(base + i); } System.out.println(); } sc.close(); } }
import java.util.Scanner;
public class XulieSum {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int l=s.nextInt();
for(int i=l;i<=100;i++) {
if((n*2)%i==0)//判断首项加末项除以项数是否为整数
{
int m=(n*2/i)-(i-1);//两个首项的和
int k=m/2;//首项
if(m%2==0) {
for(int j=0;j<i-1;j++) {
System.out.print(k+++" ");
}
System.out.println(k);
return ;
}else {
continue;
}
}else{
continue;
}
}
System.out.println("No");
}
}
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int L = sc.nextInt(); for (int i = L; i <= 100; i ++) { if ((2 * N + i - i * i) % (2 * i) == 0) { int a1 = (2 * N + i - i * i) / (2 * i); for (int j = 0; j < i-1; j ++) { System.out.print(a1 + j + " "); } System.out.println(a1 + i - 1); return; } } System.out.println("No"); } }
可以知道,我的算法只需要在线性时间复杂度内就可以有效解出(给自己赞一个) ``` public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int L = in.nextInt();
int charge = 0;
boolean flag = false;
// ?List<Integer> temp = new ArrayList<Integer>();
//get the mod of N
int len = L;
for(; len <= Math.sqrt(2*N)+1 && len <= 100; ++len){
if(N*2 % len == 0){
charge = 2*N/len;
charge = charge + 1 - len;
if(charge >= 0 && charge % 2 == 0){
charge /= 2;
flag = true;
break;
}
}
}
int sum = 0;
if(flag){
for(int j = 0; j < len-1; ++j){
sum += charge+j;
System.out.print(charge+j + " ");
}
System.out.println(charge+len-1);
}else{
System.out.println("No");
}
in.close();
}
} ```
import java.util.Scanner; /* * 题目需要找出一段长度大于等于L的连续非负整数,使得其和等于N。L要尽可能小。 * 考虑是连续非负整数,所以其和我们能用中位数来表示,分两种情况: * 情况一,长度为奇数的情况: * 此时中位数一定是整数,N = 中位数 x L * 情况二,长度为偶数的情况: * 此时中位数肯定是xx.5的形式,N = xx.5 * L * 所以我们从长度L开始枚举,至100为止,分奇偶讨论。 */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), l = in.nextInt(); in.close(); int bg = -1, ed = -1; for (int i = l; i <= 100; ++i) { // 奇数,中位数一定是整数 if (i % 2 == 1 && n % i == 0) { int mid = n / i; bg = mid - (i - 1) / 2; ed = mid + (i - 1) / 2; if (bg >= 0) // 答案要合法,即需要是非负整数 break; } // 偶数,中位数一定是0.5形式 if (i % 2 == 0 && (double)n / i - n / i == 0.5f) { int mid = n / i; bg = mid - i / 2 + 1; ed = mid + i / 2; if (bg >= 0) break; } } if (bg >= 0) { for (int i = bg; i < ed; ++i) { System.out.print(i + " "); } System.out.println(ed); } else { System.out.println("No"); } } }
import java.util.Scanner; public class Main3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int L = sc.nextInt(); int x = 0; double i = L; int j = 0; for (; i <= 100; i++) { if ((N - i * (i - 1) / 2) % i == 0) { //因为除二所以应该是double x = (int) ((N - i * (i - 1) / 2) / i); if (x >= 0) break; } } if (i >= 101) System.out.println("No"); else { for (; j < i - 1; j++) System.out.print(x + j + " "); System.out.print( x+(int)i-1); } } }
//想知道我的代码没有考虑哪种情况 通过率90%
public class Main {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int L = sc.nextInt(); int x = 0; int i = L; int j = 0; for(; i <= 100; i++){ if((N - i*(i-1)/2) % i == 0) x = (N - i*(i-1)/2) / i; if(x != 0) break; } if(i > 100) System.out.println("No"); else{ for(; j < i - 1; j++) System.out.print(x+j + " "); System.out.print(敏感词re>}