From fb62b9578ae645188db52bcb52d72ff356367060 Mon Sep 17 00:00:00 2001 From: Ye Xianjin Date: Wed, 19 Nov 2014 17:25:10 +0800 Subject: [PATCH] the sum of two integer may overflow. see http://googleresearch.blogspot.jp/2006/06/extra-extra-read-all-about-it-nearly.html for details. --- C++/chapLinearList.tex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/C++/chapLinearList.tex b/C++/chapLinearList.tex index d5a020f3..025ff15d 100644 --- a/C++/chapLinearList.tex +++ b/C++/chapLinearList.tex @@ -182,7 +182,7 @@ \subsubsection{代码} int search(int A[], int n, int target) { int first = 0, last = n; while (first != last) { - const int mid = (first + last) / 2; + const int mid = first + (last - first) / 2; if (A[mid] == target) return mid; if (A[first] <= A[mid]) { @@ -240,7 +240,7 @@ \subsubsection{代码} bool search(int A[], int n, int target) { int first = 0, last = n; while (first != last) { - const int mid = (first + last) / 2; + const int mid = first + (last - first) / 2; if (A[mid] == target) return true; if (A[first] < A[mid]) {