-
Notifications
You must be signed in to change notification settings - Fork 4
/
factorial.cpp
52 lines (43 loc) · 1.2 KB
/
factorial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*====================================
输入n,求y1=1!+3!+...m!(m是小于等于n的最
大奇数) y2=2!+4!+...p!(p是小于等于n的最
大偶数)
====================================*/
#include<iostream>
using namespace std;
long fractorial(int n){
long y = 1;
for(int i = 1; i <= n; i++)
y *= i;
return y;
}
int main(){
int num;
long y1 = 0, y2 = 0;
cin>>num;
for(int i = 1; i <= num; i += 2)
y1 += fractorial(i);
for(int i = 2; i <= num; i += 2)
y2 += fractorial(i);
cout<<y1<<' '<<y2;
return 0;
}
/*补充一个看到的很棒的解法:
链接:https://www.nowcoder.com/questionTerminal/e58c8a55162d49c48115bdfa5da7da56?f=discussion
来源:牛客网
#include <iostream>
using namespace std;
int main()
{
int n ,ans1, ans2, i, j;
while (cin >> n) {
ans1 = ans2 = 0, j = 1;
for (i = 1; i <= n; ++i) {
j *= i; //j用来保存当前数字i的阶乘
if (i % 2) ans1 += j; //i为奇数,ans1加上i!
else ans2 += j; //i为偶数,ans2加上i!
}
cout << ans1 << " " << ans2 << endl;
}
}
*/