惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
Last Week in AI
Last Week in AI
罗磊的独立博客
量子位
Jina AI
Jina AI
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
美团技术团队
雷峰网
雷峰网
爱范儿
爱范儿
S
SegmentFault 最新的问题
小众软件
小众软件
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 雨中漫步的太阳

基于跨数据库的事务的一个讨论,希望参考下大家的意见 Wave帮助、技巧、术语集 从IBatis2.X 移植到IBatis3.0 sqlMapConfig and sqlMap XML 配置文件升级说明 - 雨中漫步的太阳 .NET正则基础——.NET正则类及方法应用[转载] 按钮单击ThickBox弹出窗口 推荐一款界面设计工具 Balsamiq Mockups jQuery.extend的用法 转载 qq2009 好像和金山词霸屏幕取词有冲突 错误 1 error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char [5]' to 'LPCWSTR' 使用commons-logging和log4j记录日志[转载] 动手改造Ibatis,使其支持文件系统存储数据列 之 看我如何给ResultMap增加属性 发一个 Window Live Writer 插件 SyntaxHighlight 代码样式 700万数据随机取10条仅用不到10ms? 动手改造Ibatis,使其支持文件系统存储数据列 之 源码下载编译和SqlMapConfig解析 IBAtisHelper 源代码放出,需要的下载吧 动手改造Ibatis,使其支持文件系统存储数据列 预览 大家讨论下,结合文件系统,给数据库瘦身方案的可行性 Ibatis SelectKey iBatis resultMap groupBy属性使用心得[转载]
也发一个有道第二题的算法,练练脑子
雨中漫步的太阳 · 2009-06-03 · via 博客园 - 雨中漫步的太阳

第二道算法题(500分)

题目要求:双倍超立方数是指一个正整数可以正好被拆分为两种不同的a^3+b^3的方式,其中a,b均为整数且0<a<=b。对于任何一个指定的 int n, 返回所有的小于等于n的双倍超立方数的个数。

看到坛子里面有人发的帖子 是关于有道第二题的,饶有兴趣,自己也写了一个算法,效率上面应该比引用文章的效率会高点,具体情况请大家测试吧

引用上述网址的的程序代码 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Collections;
 6 
 7 namespace ConsoleApplication1
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int n = int.Parse(Console.ReadLine());
14 
15             System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
16             watch.Start();
17             List<int> baseNums = new List<int>();
18             Dictionary<intint> Results = new Dictionary<intint>();
19             int ResultCount = 0;
20 
21             for (int a = 1; a * a * a < n; a++)
22             {
23                 baseNums.Add(a * a * a);
24             }
25             for (int a = 0; a < baseNums.Count; a++)
26             {
27                 for (int b = a; b < baseNums.Count; b++)
28                 {
29                     int _temp = baseNums[a] + baseNums[b];
30                     if (_temp < n + 1)
31                     {
32                         if (Results.ContainsKey(_temp))
33                         {
34                             Results[_temp] += 1;
35                         }
36                         else
37                         {
38                             Results.Add(_temp, 1);
39                         }
40                     }
41                 }
42             }
43 
44             foreach (var item in Results)
45             {
46                 if (item.Value == 2)
47                 {
48                     ResultCount += 1;
49                 }
50             }
51 
52             watch.Stop();
53             Console.WriteLine("we found {0} results in {1} milliseconds", ResultCount, watch.ElapsedMilliseconds);
54         }
55     }
56 }
57 
  

  下面是我的算法, 思路其实就是 这里的 ab 两个数一定都不比给出的n的立方根的的整数大

using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
int n = 475574;
            
int ResultCount = 0;
            System.Diagnostics.Stopwatch watch 
= new System.Diagnostics.Stopwatch();
            watch.Start();
            Dictionary
<intint> countMap = new Dictionary<intint>();
            
int i = (int)Math.Pow(n, 1.0 / 3.0);
            
for (; i > 0; i--)
            {
                
int k = i * i * i;
                
int m = n - k;
                
for (int x = 1; x <= i; x++)
                {
                    
if ((x * x * x) <= m)
                    {
                        
int nn = (i * i * i) + (x * x * x);
                        
if (countMap.ContainsKey(nn))
                        {
                            
if (countMap[nn] < 2)
                            {
                                ResultCount
++;
                            }

                        }

else
                        {
                            countMap.Add(nn, 
1);
                        }
                    }
                    
else
                    {
                        
break;
                    }
                }
            }
            watch.Stop();
            Console.WriteLine(
"we found {0} results in {1} milliseconds", ResultCount, watch.ElapsedMilliseconds);

        }
    }
}

测试结果 如下图: