zoukankan      html  css  js  c++  java
  • hdu 1399 Starship Hakodate-maru (暴力搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1399

    题目大意:找到满足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
    ",max);
    23     }
    24 }

    还有第二种认为不错的代码。

     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
    ",max);
    30     }
    31     return 0;
    32 }
    View Code
  • 相关阅读:
    c#判断输入的是不是数字
    params 关键字载入空值的陷阱
    拆箱陷阱
    接口的概念和声明
    数据库
    ios 纵向的UISlider
    微信小程序学习网站
    Spark RDD(2)
    SparkSql
    廖雪峰老师学习网站
  • 原文地址:https://www.cnblogs.com/qq-star/p/3890920.html
Copyright © 2011-2022 走看看