题解 | #序列和#

序列和

https://www.nowcoder.com/practice/46eb436eb6564a62b9f972160e1699c9

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = 0;
        int L = 0;
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) {
            N = in.nextInt();
            L = in.nextInt();
        }
        func(N,L);
    }
  //还是暴力求解适合我本不聪明的脑瓜
    public static void func(int N,int L){
        for(int i=L ; i<=100 ;i++){
            for(int a1 = 0 ; a1<=N/i ;a1++){
                if(a1*i+((i-1)*i)/2==N){
                    for(int j=0 ;j<i ;j++){
                        System.out.print(a1+j+" ");
                    }
                    return;
                }
            }
        }
        System.out.println("No");
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务