杭电oj 2007-2008-2009

2007 平方和与立方和
在这里插入图片描述

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   int x,y;
   int s1=0;
   int s2=0;
   while(cin >> x >> y)
   {
       if(x>y) swap(x,y);
       for(int i=x;i<=y;i++)
       {
          if(i%2==0)
             s1 += pow(i,2);
          else
             s2 += pow(i,3);
       }
       cout << s1 << " " << s2 << endl;
       s1 = s2 = 0; 
   }
   return 0;
}

注意判断x,y的大小关系;注意s1,s2定义时要=0.

2008 数值统计
在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
    int a,b,c,n;
    double m;//注意类型
    while(cin >> n)
    {
       if(n==0) break;
       a = b = c = 0;//注意位置
       while(n--)
       {
          cin >> m;
          if(m<0) a++;
          else if(m==0) b++;
          else c++;
       }
       cout << a << " " << b << " " << c << endl;
       
    }
    return 0;
}

2009 求数列的和
在这里插入图片描述

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
    int n,m;
    double sum;
    while(cin >> n >> m)
    {
        sum = 0;
        double a=n;
        for(int i=0;i<m;i++)
        {
            sum += a;
            a = sqrt(a);
        }
        cout << fixed << setprecision(2) << sum << endl;
    }
    return 0;
}

注意:整数n开平方根后就有可能成为double类型,所以要额外定义一个double类型的a进行下面的开平方根运算;此外,要注意到m=0的情况。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值