HDU 4627

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

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 <= 10 9).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
 
此题的话,因为一个数n由两个正整数a+b得来,所以可以先确定a和b的范围,是从1到n/2 
 因为从n/2+1到n,是和前半部分重复,不用计算 
 然后,就用辗转相除法,a,b互质,输出最大的 LCM(a, b),即a,b的最小公倍数最大。
 注意:这种算法易懂,但是很容易超时,自己做的时候就是

#include <stdio.h>
int main()
{
int a,b,c;
int max,min,t1,t2;
int i,n,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
max=;
for(i=; i<=n/; i++)
{
a=i;
b=n-i;
a>b?t1=a:t1=b;
t2=n-t1;
while(t2)
{
c=t1%t2;
t1=t2;
t2=c;
}
min=a*b/t1;
if(min>max)
max=min;
}
printf("%d\n",max);
}
return ;
}

另外还有一种,这是一种奇偶求法,很简单巧妙,经某ACM大神指点得知

#include <stdio.h>

int main()
{
int n,T;
long long max;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
max=;
if (n==) printf("1\n");
else
{
if (n%==)
{
max=n/;
if (max%==) max=(max+)*(max-);
else max=(max+)*(max-);
}
else
{
max=n/;
max=max*(max+);
}
printf("%I64d\n",max);
}
}
return ;
}

最新文章

  1. html之file标签 --- 图片上传前预览 -- FileReader
  2. jquery之css操作
  3. Android项目开发实战-2048游戏
  4. hibernate学习(3)——api详解对象(2)
  5. chrome浏览器不允许记忆登录账户的方法
  6. 0429 Scrum团队成立与第6-7章读后感
  7. informix 查看 当前锁表
  8. How to change statusbar text color to dark on android 4.4
  9. Visual Studio 中 Tab 转换为空格的设置
  10. tomcat学习笔记1
  11. Microsoft Azure自动化测试
  12. uCGUI窗口的创建过程分析
  13. django新手第一课
  14. ubunut在系统恢复模式下无法修改root密码的分析和解决
  15. 跳板机 jumpserver
  16. Vue 限制input输入 限数字 或 小数点后两位number
  17. 解决TypeError: __init__() missing 1 required positional argument: &#39;on_delete&#39;
  18. Linux里的2&gt;&amp;1究竟是什么
  19. bootstrap modal 弹出其他页面
  20. Java排序算法——堆排序

热门文章

  1. redis学习心得之三-【java操作redis】
  2. hive 模块
  3. bzoj1036 zjoi2008 树的统计 count
  4. SCOI2014 方伯伯的OJ onlinejudge
  5. Visual studio 2008 的语法高亮插件 NShader
  6. Eclipse Python配置
  7. 在Ubuntu上下载、编译和安装Android最新源码
  8. 细说Lucene源码(一):索引文件锁机制
  9. Android三种实现自定义ProgressBar的方式介绍
  10. Android内存等信息