• 有道难题 双倍超立方数 的解答


    昨天进行的有道难题资格赛Group17中的“难题”:

    Problem Statement
       
    双倍超立方数是指一个正整数可以正好被拆分为两种不同的a^3+b^3的方式,其中a,b均为整数且0<a<=b。对于任何一个指定的 int n, 返回所有的小于等于n的双倍超立方数的个数。
    Definition
       
    Class:  TwiceSuperCubic
    Method:  count
    Parameters:  int
    Returns:  int
    Method signature:  int count(int n)
    (be sure your method is public)
       
    Constraints
    -
    n取值范围为1到1,000,000,000(含)
    Examples
    0)
    1
    Returns: 0

    1)
    1729
    Returns: 1
    1729=1^3+12^3 1729=9^3+10^3


    2)
    475574
    Returns: 27

    人太笨,我的程序输入太大的数时执行时间总是2.016s,多出16毫秒,今天在有道快帖里有人发了c++的代码,用了map<int,int>,我改成了c#,用Dictionary<intint>保存数据,测试了下,速度不是一般的快

    using System;
    using System.Collections;

    class TwiceSuperCubic
    {
        
    public int count(int n)
        {
            
    if(n<1||n>1000000000return 0;
            
    int count = 0;
            Dictionary
    <intint> d = new Dictionary<intint>();
            
    for (int a = 1; a * a * a <= n; a++)
            {
                
    for (int b = a; b * b * b <= n; b++)
                {
                    
    int t = a * a * a + b * b * b;
                    
    if (d.ContainsKey(t)) d[t]++else d.Add(t, 1);
                }
            }
            
    foreach (KeyValuePair<intint> k in d)
            {
                
    if (k.Key <= n && k.Value == 2) count++;
            }
            
    return count;
        }
    }


    然后发下我的代码,在自己机子上测试是正确的,但执行超过2s。

    using System;
    using System.Collections;

    class TwiceSuperCubic
    {
        
    public int count(int n)
        {
            
    if(n<1||n>1000000000return 0;
            
    for (int i = 1; i <= n; i++)
            {
                
    int i_D = 0;
                
    int k = Convert.ToInt32(Math.Floor(Math.Pow(i, 1.0 / 3.0)));
                
    for (int a = 1; a <= k; a++)
                {
                    
    for (int b = a; b <= k; b++)
                    {
                        
    if ((a * a * a + b * b * b) == i)
                        {
                            i_D
    ++;
                            
    break;
                        }
                    }
                    
    if (i_D > 2)
                    {
                        
    break;
                    }
                }
                
    if (i_D == 2)
                {
                    count
    ++;
                }
            }
            
    return count;
        }
    }

    我厚着脸皮发自己不好的代码是很丢人的,大家可要留点面子给我,呵呵。我的TopCoder注册后一直登录不上,昨天我都准备卸载它了,心想再登录一次,不行就卸掉,没想进去了,里面环境很不熟,不过摸摸也就会了,我只做了那个500分的题,因为它那个下拉框显示Select One,哈哈。最后没注意时间还超时了,没有提交,扛了个鸭蛋回来。不过这次接触了TopCoder,这个平台做的真好,很有意思,还“看到”了很多牛人,我看了下他们的Info,Coder Type都是Student,还有显示Middle School的,我很少崇拜谁,但对他们可是很佩服。很支持这样的活动,提高大家的编程兴趣,让牛人有个展现的舞台,振兴中国的软件。

    不对请指正。

  • 相关阅读:
    C# 和 java 基本数据类型
    ESB
    sql server和mysql到底有什么区别
    MySQL服务器线程池插件
    Index Merge 算法
    任职22年,PowerShell之父将从微软离职:曾因开发PowerShell被微软降级过
    Umi 4 发布啦 🎈
    VB/VBA,可能超乎你的想象
    消息推送的场景
    DPI
  • 原文地址:https://www.cnblogs.com/pains/p/1494011.html
Copyright © 2020-2023  润新知