【英雄会】三元组

sjlzcj 2014-01-03 09:46:21
没有通过,,提示超过3秒超时失败。。
下面的代码就抛砖引玉了,,希望通过的同学可以赐教一下

public class Ternary {
public static void main(String[] args) {
long start = System.currentTimeMillis();
System.out.println(count(1, 50000));
long end = System.currentTimeMillis();
System.out.println(end - start);
}

private static long count(int a, int b) {
long resultCount = 0;
int tempCount = 0;
for (int n = a; n <= b; n++) {
for (int y = 1; y * y <= n; y++) {
if (n % y == 0) {
int t = n / y;
int k = t + y;
if ((k & 3) == 0) {
if (y > (k >>= 2)) {
tempCount++;
}
if (t != y && t > k) {
tempCount++;
}
}
}
}
resultCount += (tempCount - 1) * tempCount >> 1;
tempCount = 0;
}
return resultCount;
}
}

...全文
127 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolbamboo2008 2014-01-04
  • 打赏
  • 举报
回复
超时失败?你这个程序超时失败是因为for死循环?但看起来不像,我觉得你是在别的地方出的错把
sjlzcj 2014-01-04
  • 打赏
  • 举报
回复
引用 1 楼 coolbamboo2008 的回复:
超时失败?你这个程序超时失败是因为for死循环?但看起来不像,我觉得你是在别的地方出的错把
算法和结果应该没什么问题, 只在运行效率上

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧