]]>綆楁硶浼樺寲婧愪簬浜虹被鏅烘収http://www.tkk7.com/BlueSUN/archive/2006/10/14/75164.html灞遍灝忓瓙灞遍灝忓瓙Sat, 14 Oct 2006 10:05:00 GMThttp://www.tkk7.com/BlueSUN/archive/2006/10/14/75164.htmlhttp://www.tkk7.com/BlueSUN/comments/75164.htmlhttp://www.tkk7.com/BlueSUN/archive/2006/10/14/75164.html#Feedback10http://www.tkk7.com/BlueSUN/comments/commentRss/75164.htmlhttp://www.tkk7.com/BlueSUN/services/trackbacks/75164.html 姣忎釜闂鏈夊緢澶氱瑙f硶錛屼絾鍏朵腑瀛樺湪涓縐嶆渶浼樼殑綆楁硶錛屾嵁鎴戣瀵熷拰鎬濊冿紝鈥樻噿浜衡欐槸鍐欎笉鍑洪偅縐嶆渶浼樼畻娉曠殑錛屼負浠涔堝憿錛熷洜涓烘渶浼樼畻娉曟湁涓涓緢鏄庢樉鐨勭壒鐐瑰氨鏄畻娉曟湰韜泦緇撲簡浜虹被鐨勮仾鏄庢墠鏅猴紝璁╂垜鏉ョ敤涓涓疄渚嬫潵璇佹槑榪欎釜瑙傜偣錛?/font>
闂錛?br />璇瘋綆楀綋鍙傛暟涓?n(n寰堝ぇ) 鏃? 1-2+3-4+5-6+7+......+n 鐨勫?br /> 鈥樻噿浜衡欒В娉曪細 public class Lazy { 聽 public static void main(String[] args) { 聽聽聽 int n = 10000; 聽聽聽 int result = 0; 聽聽聽 for (int i = 0, flag = 1; i < n; i++) { 聽聽聽聽聽 result += flag * (i + 1); 聽聽聽聽聽 flag =聽-flag; 聽聽聽 } 聽聽聽 System.out.println(result); 聽 } }
鈥樺嫟浜衡欒В娉曪細 public class Diligent { 聽 public static void main(String[] args) { 聽聽聽 int n = 10000; 聽聽聽 int result = 0; 聽聽聽 if (0 == n % 2) { 聽聽聽聽聽 result = -n / 2; 聽聽聽 } else { 聽聽聽聽聽 result = -n / 2 + n;聽 //鐢變簬-n / 2浼氳垗寮冨皬鏁伴儴鍒嗭紝鎵浠ユ棤闇鍐欐垚-(n - 1) / 2 聽聽聽 } 聽聽聽 System.out.println(result); 聽 } }