求1!-2!+3!-4!++……+49!+-50!的和。
时间: 2023-11-23 15:07:37 浏览: 84
以下是C语言的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int i = 0;
int n = 0;
long long sum = 0; // 定义一个long long类型的sum保存每次累加的值,避免溢出
for (i = 1; i <= 50; i++) {
n = i % 2 == 0 ? -1 : 1; // 根据i的奇偶性确定符号
long long factorial = 1;
for (int j = 1; j <= i; j++) {
factorial *= j; // 计算i的阶乘
}
sum += n * factorial; // 累加
}
printf("%lld\n", sum); // 输出结果
system("pause");
return 0;
}
```
相关问题
求1!-2! 3!-4! …… 49!-50!的和(c++)
以下是求1!-2! 3!-4! …… 49!-50!的和的C++代码示例:
```cpp
#include <iostream>
using namespace std;
int main() {
int n = 50;
int a = 1;
float Sn = 0;
for (int i = 1; i <= n; i++) {
a *= i;
if (i % 2 == 0)
Sn -= a;
else
Sn += a;
}
cout << "Sum of 1!-2! 3!-4! …… 49!-50! is: " << Sn << endl;
return 0;
}
```
c++实现1-2+3-4……+99-100
好的,这是一个编程问题,可以用循环来实现。以下是实现代码:
for(int i=1; i<=100; i+=2){
cout << i << "-" << i+1 << " ";
}
输出结果为:
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 53-54 55-56 57-58 59-60 61-62 63-64 65-66 67-68 69-70 71-72 73-74 75-76 77-78 79-80 81-82 83-84 85-86 87-88 89-90 91-92 93-94 95-96 97-98 99-100