hdu2132

题目: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..-_-

output the result sum(n).

1 2 3 -1
1 3 30
题解:对于这道题我有两种解法:1、题目已经给出1 2 3的sum[i],对于公式sum(i) = sum(i-1) + i*i*i,只要知道知道前一个数,就可以算出后一个数,就可以用一个for循环,算出后面的数,存成一个数组,在知道i值后,直接在数组里面调出就可以了。不过这个数组要给的很大。2、用函数来做,用递归思想,也涉及到循环,不过不用开数组。
#include<stdio.h>
int main()
{

__int64
a[];
__int64
n,i;
a[]=;
a[]=;
a[]=;
for
(i=;i<=;i++)
{

if
(i%==) a[i]=a[i-]+i*i*i;
else
a[i]=a[i-]+i;
}

while
(scanf("%I64d",&n)!=EOF)
{

if
(n<) break;
printf("%I64d\n",a[n]);
}
return;
}
上一篇:java多线程探究


下一篇:如何在ppt或word中添加高亮代码?