---
title: "1+2-3+4-5...n 一行代码搞定"
author: "kaobian"
date: "2011-03-20T19:21:26Z"
tags: []
categories: ["编程语言"]
slug: "1-2-3-4-5-n"
draft: false
lastmod: "2011-07-14T10:13:23Z"
id: 968658
url: "https://kaobian.iteye.com/blog/968658"
---

这个题目有很多算法，我感觉我的算法执行效率还是 比其他的算法高一些：

就这么一个方法就可以搞定

	public static int sum(int n){
		return ((n-1)%2)==1?(1+(((n-1)/2)*-1)+n):(1+(n-1)/2*(-1)) ;
	}