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

昨天进行的有道难题资格赛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 > 1000000000 return   0 ;
        
int  count  =   0 ;
        Dictionary
< int int >  d  =   new  Dictionary < int int > ();
        
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 < int int >  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 > 1000000000 return   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的,我很少崇拜谁,但对他们可是很佩服。很支持这样的活动,提高大家的编程兴趣,让牛人有个展现的舞台,振兴中国的软件。

不对请指正。

转载于:https://www.cnblogs.com/pains/archive/2009/06/01/1494011.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值