java华为od机试 已知总和为sum,连续序列个数为n,求该序列

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main8 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
        String first;
        while (null!=(first=buff.readLine())){
    
    
            String[] s = first.split(" ");
            int sum = Integer.parseInt(s[0]);
            int n = Integer.parseInt(s[1]);

            int x=0,sum2=0;
            boolean flag=true;
            while (sum2<=sum){
    
    

                sum2=a(x,n);
                if (sum2==sum){
    
    
                    flag=false;
                    for (int i=0;i<n;i++){
    
    
                        System.out.print(x+" ");
                        x++;
                    }
                }
                x++;
            }
            if (flag){
    
    
                System.out.println(-1);
            }
        }
    }
    public static int a(int x, int n){
    
    
        return  (x+x+n-1)*n  /2;
    }
}

猜你喜欢

转载自blog.csdn.net/JavaBigData/article/details/117392023
今日推荐