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

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

输入格式:

从键盘中输入一个整数

输出格式:
在一行中输出两个整数,以空格分割,按由小到大的顺序输出。

输入样例:
33
输出样例:
16 17
输入样例:
-51
输出样例:
-26 -25

我的代码

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while(true)
{
int x = in.nextInt();
if (x == 0)
{
System.out.println(-1 + " " + 1);
} else if (x % 2 == 0)
{
System.out.println(x / 2 + " " + (x / 2));
} else
{
if (x > 0)
{
System.out.println(x / 2 + " " + ((x / 2) + 1));
} else
{
System.out.println((x / 2 - 1) + " " + ((x / 2)));
}
}
}
}

一些网上的代码,我做了些改进

public static void main(String[] args)
{

Scanner xx = new Scanner(System.in);
while(true)
{
int result1 = 0;
int result2 = 0;
int multiply = 0;
int input =xx.nextInt();
if(input==0)
{
result1 = -1;
result2 = 1;
}
else if(input==1)
{
result1 = 0;
result2 = 1;
}
else if(input==-1)
{
result1 = -1;
result2 = 0;
}
else
{
int factor = input > 0 ? 1 : -1;
while(true)
{
if (factor == input)
{
break;
}
if ((input - factor) * factor > multiply)
{
multiply = (input - factor) * factor;
result1 = input - factor;
result2 = factor;
}
if (input > 0)
{
factor++;
} else
{
factor--;
}
}
}
/*
* 这里也可以进行修改一下,
* 因为当输入的数是正数时,result1是大的
* 当输入的是负数时,result1是小的数
*/
System.out.println(Integer.min(result1, result2) + " " + Integer.max(result1, result2));
}
}