平方和与立方和

http://acm.hdu.edu.cn/showproblem.php?pid=2007

   1 /*write by ASUN*/
   2 #include"stdio.h"
   3 void main()
   4 {
   5     long int n,m; 
   6     double sumj,sumy;   
   7     while(scanf("%ld%ld",&m,&n)!=EOF)
   8     {
   9         sumj=sumy=0;
  10         if(m>n) //有可能n小于m,这点题目没有将清楚 
  11         {
  12             m=m+n;
  13             n=m-n;
  14             m=m-n;
  15       }
  16         for(;m<=n;m++)
  17         {
  18             if(m%2==1)
  19                 sumj+=m*m*m*1.0;
  20             else 
  21                 sumy+=m*m*1.0;
  22         }
  23         printf("%.0lf %.0lf\n",sumy,sumj);
  24     }
  25 }

   1 /*written by czk*/
   2 #include <stdio.h>
   3 
   4 int square_sum(int n) {
   5     return n*(n+1)*(2*n+1)/6;
   6 }
   7 
   8 int cube_sum(int n) {
   9     return n*(n+1)/2*n*(n+1)/2;
  10 }
  11 
  12 int sum(int n) {
  13     return n*(n+1)/2;
  14 }
  15 
  16 /* cube sum of 1, 3, 5,...,(2*n-1) */
  17 int odd_cube_sum(int n) {
  18     return 8*cube_sum(n) - 12*square_sum(n) + 6*sum(n) - n;
  19 }
  20 
  21 
  22 int main() {
  23     int m, n;
  24     while(scanf("%d%d", &m, &n)!=EOF) {
  25         if(n < m) {
  26             int temp = m;
  27             m = n;
  28             n = temp;
  29         }
  30         printf("%d ", 4*(square_sum(n/2)-square_sum((m-1)/2)));
  31         printf("%d\n", odd_cube_sum((n+n%2)/2)- odd_cube_sum( (m-m%2)/2));
  32     }
  33 }

hdu2007 (last edited 2008-06-07 15:08:29 by czk)

ch3n2k.com | Copyright (c) 2004-2020 czk.