hdu 2007 - 平方和与立方和

时间:2021-05-28 22:40:52

题目大意:

给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。

解答:

坑你没商量!要考虑输入数a,b的大小。如果a>b,需要交换a,b的值。

   1:  #include<stdio.h>
   2:  #include<stdlib.h>
   3:  int main(){
   4:      int a,b,i;
   5:      while(scanf("%d %d",&a,&b)!=EOF){
   6:          long sum1=0,sum2=0;
   7:          if(a>b){
   8:              int tmp=a;
   9:              a=b;
  10:              b=tmp;
  11:          }
  12:          for(i=a;i<=b;i++){
  13:              if(i%2==0)
  14:                  sum1+=i*i;
  15:              else
  16:                  sum2+=i*i*i;
  17:          }
  18:          printf("%ld %ld\n",sum1,sum2);
  19:      }
  20:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }