The Unsolvable Problem

The Unsolvable Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem Description
There are many unsolvable problem in the world.It could be about one or about zero.But this time it is about bigger number.
Given an integer n(2 <= n <= 109).We should find a pair of positive integer a, b so that a + b = n and [a, b] is as large as possible. [a, b] denote the least common multiplier of a, b.
 
Input
The first line contains integer T(1<= T<= 10000),denote the number of the test cases.
For each test cases,the first line contains an integer n.
 
Output
For each test cases,print the maximum [a,b] in a line.
 
Sample Input
3
2
3
4
 
Sample Output
1
2
3
 
 
题意:求满足a+b=n,a与b没有公约数(互质),a*b尽可能地大的值
 
思路:
分情况讨论:
如果n是奇数
那么你通过n/=2求得它的中间数,那么a和b就应该为n与n+1
如果n是偶数
那么先求中间数n/=2,再判断此时的这个中间数是奇数还是偶数,如果是奇数,那与它相邻的两个数肯定不互质,可以排除,所以a,b
应该为n-2和n+2;如果是偶数,那么就选n-1和n+1即可
 
代码:
 
#include"iostream"
#include"cmath"
using namespace std;

void Work()
{
long long n;
cin>>n;
if(n==2)   {cout<<1<<endl;return;}
if(n%2!=0) {n=(n+1)/2;cout<<(n-1)*n<<endl;}
else
{
    n/=2;
    if(n%2==0) {cout<<(n-1)*(n+1)<<endl;}
    else       {cout<<(n-2)*(n+2)<<endl;}
}
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
     Work();
    }
    return 0;
}
 
原文地址:https://www.cnblogs.com/zsyacm666666/p/4654678.html