首页 > ACM题库 > HDU-杭电 > hdu 2132 An easy problem-动态规划-[解题报告]C++
2013
12-29

hdu 2132 An easy problem-动态规划-[解题报告]C++

An easy problem

问题描述 :

We once did a lot of recursional problem . I think some of them is easy for you and some if hard for you.
Now there is a very easy problem . I think you can AC it.
  We can define sum(n) as follow:
  if i can be divided exactly by 3 sum(i) = sum(i-1) + i*i*i;else sum(i) = sum(i-1) + i;
  Is it very easy ? Please begin to program to AC it..-_-

输入:

  The input file contains multilple cases.
  Every cases contain only ont line, every line contains a integer n (n<=100000).
  when n is a negative indicate the end of file.

输出:

  The input file contains multilple cases.
  Every cases contain only ont line, every line contains a integer n (n<=100000).
  when n is a negative indicate the end of file.

样例输入:

1
2
3
-1

样例输出:

1
3
30

2011-12-16 12:11:14

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2132

题意:求sum[i],定义为:当i是3的倍数时,sum[i] = sum[i-1]+i*i*i,否则sum[i] = sum[i-1] + i。

mark:直接打表。TLE了2次,就是不打表直接算的后果。

代码:

# include <stdio.h>


long long dp[100010] ;


int main ()
{
    long long n, i ;
    for (i = 1 ; i<= 100000 ; i++)
    {
        if (i%3==0) dp[i] = dp[i-1] + i*i*i ;
        else dp[i] = dp[i-1]+i ;
    }
    while (~scanf ("%I64d", &n) && n>=0)
        printf ("%I64d\n", dp[n]) ;
    return 0 ;
}

解题转自:http://www.cnblogs.com/lzsz1212/archive/2012/01/06/2315003.html


  1. #include <stdio.h>
    int main(void)
    {
    int arr[] = {10,20,30,40,50,60};
    int *p=arr;
    printf("%d,%d,",*p++,*++p);
    printf("%d,%d,%d",*p,*p++,*++p);
    return 0;
    }

    为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下?

  2. 这道题目的核心一句话是:取还是不取。
    如果当前取,则index+1作为参数。如果当前不取,则任用index作为参数。

  3. 有两个重复的话结果是正确的,但解法不够严谨,后面重复的覆盖掉前面的,由于题目数据限制也比较严,所以能提交通过。已更新算法

  4. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。