多项式"/>
1126: 零起点学算法33——求多项式
Description
形如1-2+3-4...+n,你会编写吗?
Input
输入1个正整数n(多组数据)
n<=1000
Output
输出1-2+3-4...+n的值(每组数据一行)
Sample Input
3
Sample Output
2
Source
零起点学算法
Code
#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{int n;while(~scanf("%d",&n)){int ans=0;for(int i=1;i<=n;i++){if(i%2==0)ans-=i;elseans+=i;}cout<<ans<<endl;}
}
更多推荐
1126: 零起点学算法33——求多项式
发布评论