试求和为N,积为最大两个整数分别为多少?

从键盘输入一个整数,它是另外两个整数的加法运算和。这样的两个整数的组合有很多种,请找出积最大的一组数据,请注意输出两个整数以空格分割,按由小到大的顺序输出。

输入格式:从键盘输入一个整数
输出格式:输出两个整数、从小到大,以空格分隔。
输入样例:33
输出样例:16 17
输入样例:-55
输出样例:-26 -25

package jmstest;

import java.util.Scanner;

public class Qiuhe {

    public static void main(String[] args) throws Exception {
        int result1 = 0;
        int result2 = 0;
        int multiply = 0;
        try {
            Scanner xx = new Scanner(System.in);
            int input = xx.nextInt();
            if (input != 0) {
                int factor = input > 0 ? 1 : -1;
                for (;;) {
                    if (factor == input) {
                        break;
                    }
                    if ((input - factor) * factor > multiply) {
                        multiply = (input - factor) * factor;
                        result1 = input - factor;
                        result2 = factor;
                    }
                    if (input > 0) {
                        factor++;
                    } else {
                        factor--;
                    }
                }
            } else {
                result1 = -1;
                result2 = 1;
            }
        } catch (Exception e) {
        }
        System.out.println(Integer.min(result1, result2) + " "
                + Integer.max(result1, result2));
    }

}

猜你喜欢

转载自blog.csdn.net/waei08/article/details/79712746