HDU 2007 (水)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2007

题目大意:给你段连续数字,让你求 all sum of (偶数2 )and all sum of (奇数3

解题思路:

暴力遍历这段数字,判断奇偶数,搞一下就行,但对于弱菜的我来说还是学到挺多的。

先上代码吧

代码:

 1 #include<iostream>
 2 #include<cmath>
 3 #include<cstdio>
 4 #include<iomanip>
 5 #include<algorithm>
 6 using namespace std;
 7 int main()
 8 {
 9     int m, n;
10     while(cin >> m >> n)
11     {
12         double x = 0;
13         double y = 0;
14         if(m > n)
15             swap(m, n);
16         for(int i = m; i <= n; i ++)
17         {
18             if(i % 2 == 0)
19                 x += pow(i, 2);
20             else
21                 y += pow(i, 3);
22         }
23         cout << fixed << setprecision(0) << x << ' ' << y << endl;
24     }
25 }

1.这里 如果用 pow(5, 2) 函数, 那 x 必须是 double 类型的,因为 pow(5, 2) 返回的是 24.99997 类似的数,int 会丢失精度,变成 24.

否则int的话就得 i * i * i i这样。

2.cout << fixed << set......是因为输出要有普通输出,不加的话会是科学记数法输出

猜你喜欢

转载自www.cnblogs.com/mimangdewo-a1/p/9367788.html