有道难题第二题算法

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1 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 

这一题大概是这么算的吧。。有更好的算法吗?还望指教

转载于:https://www.cnblogs.com/pandora/archive/2009/06/02/1494905.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值