import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner reader = new Scanner(System.in); int n = reader.nextInt(); if(n%2==0){ double resulteven = (1.0+n-1)*n/4; double resultodd = (2.0+n)*n/4; System.out.println(resulteven-resultodd); } else{ double resulteven = (1.0+n)*n/4; double resultodd = (1.0+n-1)*(n/2-1)/2; System.out.println(resulteven-resultodd); } } }
#include <iostream> using namespace std; int main() { int n,Sum=0; cin>>n; for (int i=1;i<n+1;i++) { Sum+=pow(-1,i+1)*i; } return 0; }
import java.util.Scanner; public class Main22{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int res=fun(n); System.out.println("结果是:"+res); } public static int fun(int n){ if(n==0) return 0; int res=0; for(int i=1;i<=n;i++){ if(i%2==1){ res=res+i; } else{ res=res-i; } } return res; } }
public class Solution{ public static void main(String[] args){ int n; int sum = 0; for(int i = 1 ; i<=n ; i++){ if(i%2 == 0){ sum -= i; }else{ sum += i; } } } }