hdu 1399 Starship Hakodate-maru (暴力搜索)
2015-10-12 14:19
459 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1399
题目大意:找到满足i*i*i+j*(j+1)*(j+2)/6形式且小于等于n的最大值。
还有第二种认为不错的代码。
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
View Code
题目大意:找到满足i*i*i+j*(j+1)*(j+2)/6形式且小于等于n的最大值。
1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 while(scanf("%d",&n),n) 10 { 11 int j,k,max=0; 12 for(j=0; j*(j+1)*(j+1)/6<=n; j++) 13 { 14 for(k=0; k*k*k<=n; k++) 15 { 16 if(k*k*k+j*(j+1)*(j+2)/6>n) 17 break; 18 } 19 if(max<(k-1)*(k-1)*(k-1)+j*(j+1)*(j+2)/6&&(k-1)*(k-1)*(k-1)+j*(j+1)*(j+2)/6<=n) 20 max=(k-1)*(k-1)*(k-1)+j*(j+1)*(j+2)/6; 21 } 22 printf("%d\n",max); 23 } 24 }
还有第二种认为不错的代码。
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 5 using namespace std; 6 7 int main() 8 { 9 int n,t,max,res; 10 while(scanf("%d",&n)!=EOF) 11 { 12 if(n==0) break; 13 max=0; 14 for(int i=0; i*(i+1)*(i+2)/6<=n; i++) 15 { 16 res=0; 17 t=n-i*(i+1)*(i+2)/6; 18 for(int j=(int)pow(n,1.0/3); j>=0; j--) 19 { 20 if(j*j*j<=t) 21 { 22 res=j*j*j; 23 break; 24 } 25 } 26 if(res+i*(i+1)*(i+2)/6>max) 27 max=res+i*(i+1)*(i+2)/6; 28 } 29 printf("%d\n",max); 30 } 31 return 0; 32 }
View Code
相关文章推荐
- hdu 1711 Number Sequence
- hdu 1686 Oulipo (kmp)
- 冒泡排序
- hdu 2199 Can you solve this equation?
- hdu 3746 Cyclic Nacklace
- hdu 1358 Period
- hdu 2817 A sequence of numbers(快速幂取余)
- hdu 2899 Strange fuction (二分)
- hdu 2141 Can you find it?(二分查找)
- hdu 1061 Rightmost Digit
- poj 1363 Rails
- hdu 1237 简单计算器
- 17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication
- RPC与RMI区别
- js字符串函数(split、join、indexOf、substring)
- Linux-Vim编辑器使用
- 最受欢迎的5个Android ORM框架
- lvds(800*600)
- hdu 1195 Open the Lock
- hdu 1518 Square(深搜+剪枝)