zoukankan      html  css  js  c++  java
  • 杭电OJ2007----平方和与立方和(易错题)

    Problem Description

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

    Input

    输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。

    Output

    对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
    你可以认为32位整数足以保存结果。

    Sample Input

    1 3 2 5

    Sample Output

    4 28 20 152

    题解:

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    using namespace std;
    #define PI 3.1415927
    int main()
    {
        int n,m,ans1,ans2,temp;
        while (cin >> n >> m)
        {
            if(m<n)
            {
                temp = n;
                 n= m;
                 m = temp;
            }
            ans1 = 0, ans2 = 0;
            for (int i =n; i <=m; i++)
                if (i % 2 != 0)
                    ans1 += pow(i, 3);
                else
                    ans2 += pow(i, 2);
            cout << ans2<<" "<< ans1<< endl;
        }
        return 0;
    }

    注意:输入的两个数不一定前面的数比后面的数小。

    永远热泪盈眶。
  • 相关阅读:
    FreeCodeCamp:Chunky Monkey
    FreeCodeCamp:Slasher Flick
    FreeCodeCamp:Truncate a string
    FreeCodecamp:Repeat a string repeat a string
    FreeCodeCamp:Confirm the Ending
    FreeCodeCamp:Return Largest Numbers in Arrays
    FreeCodeCamp:Title Case a Sentence
    git和GItHub的区别
    dedecms简介
    html 7大知识点
  • 原文地址:https://www.cnblogs.com/2021WGF/p/14253252.html
Copyright © 2011-2022 走看看