ask (-1)^i*i的前n项和
Input
In the only line given n (1≤n≤105105)
Output
Print a single line containing answer.
Sample 1
| Inputcopy | Outputcopy |
|---|---|
1 |
-1 |
Sample 2
| Inputcopy | Outputcopy |
|---|---|
2 |
1 |
Sample 3
| Inputcopy | Outputcopy |
|---|---|
3 |
-2 |
#include <iostream>
#include <cmath>
const int N=100007;
int arr[100007]={0};
using namespace std;
int main()
{
int j,ans=0;
cin >>j;
for (int i=1;i<=j;i++)
{
arr[i]=(pow(-1,i))*i;
ans+=arr[i];
}
cout <<ans<<endl;
}
文章讲解如何用C++实现计算序列ask((-1)^i*i)的前n项和,涉及循环和算术运算。

被折叠的 条评论
为什么被折叠?



