【回到豆瓣】http://site.douban.com/196781/widget/notes/12161495/note/258843932/
【题目】2.给定一个整数X和整数序列A1,A2,......,An,后者已经预先排序,求X是否在序列中,并返回下标,若不在序列中返回-1.
【思路】这道题除了2分还真不知道有啥更好的,关于多分,比如3分4分,一般不常用,我搜到这样一篇文章 仅供参考,http://blog.csdn.net/aaajj/article/details/5601687
【代码】包含2分的递归解和非递归解
1 #include <iostream> 2 #include <vector> 3 #include <cstdlib> 4 #include <ctime> 5 #include <algorithm> 6 using namespace std; 7 8 #define N 100 9 10 //递归解 11 template <typename Comparable> 12 int binarySearch2(const vector<Comparable> & arr,Comparable k, int low,int high){ 13 int mid = (low + high)/2; 14 if(low > high){ 15 return -1; 16 } 17 if(k > arr[mid]){ 18 return binarySearch2(arr,k,mid+1,high); 19 }else if(k < arr[mid]){ 20 return binarySearch2(arr,k,low,mid-1); 21 }else{ 22 return mid; 23 } 24 return -1; 25 } 26 27 //非递归解 28 template <typename Comparable> 29 int binarySearch1(const vector<Comparable> & arr,Comparable k){ 30 int low = 0,high = arr.size()-1; 31 while(low <= high){ 32 int mid = (low + high)/2; 33 if(k > arr[mid]){ 34 low = mid + 1; 35 }else if(k < arr[mid]){ 36 high = mid - 1; 37 }else{ 38 return mid; 39 } 40 } 41 return -1; 42 } 43 44 int main() 45 { 46 srand(time(0)); 47 vector<int> arr; 48 for(int i = 0;i < N;++i){ 49 arr.push_back(rand()); 50 } 51 sort(arr.begin(),arr.end()); 52 for(int i = 0;i < N;++i){ 53 cout<<i<<":"<<arr[i]<<endl; 54 } 55 int k = 0; 56 cin>>k; 57 cout<<binarySearch1(arr,k)<<endl; 58 // cout<<binarySearch2(arr,k,0,arr.size()-1)<<endl; 59 system("pause"); 60 return 0; 61 }
【题目】3.给定正整数A和B,求二者的最大公约数。
【思路】最常见的算法就是欧几里德算法了,其利用了这样的事实,gcd(a,b) = gcd(b,a mod b)(a >= b) (a mod b是指a取余b),之前我写了一种证明,详细讨论请见本帖http://site.douban.com/196781/widget/forum/12161503/discussion/51264526/
【代码】
1 #include <iostream> 2 #include <vector> 3 #include <cstdlib> 4 #include <algorithm> 5 using namespace std; 6 7 //递归解 8 int gcd1(int a,int b){ 9 if(b > a){ 10 int t = a; 11 a = b; 12 b = t; 13 } 14 if(b == 0){ 15 return a; 16 }else{ 17 return gcd1(b,a%b); 18 } 19 } 20 //非递归解 21 int gcd2(int a,int b){ 22 if(b > a){ 23 int t = a; 24 a = b; 25 b = t; 26 } 27 int temp = 0; 28 while(b){ 29 temp = a; 30 a = b; 31 b = temp % b; 32 } 33 return temp; 34 } 35 36 int main3() 37 { 38 int a = 0; 39 int b = 0; 40 cin>>a>>b; 41 cout<<gcd1(a,b)<<endl; 42 system("pause"); 43 return 0; 44 }
【题目】4.给定正整数N,求解其是否为素数。
【思路1】从2开始逐个数除,能整除的话说明不是素数,如果除到sqrt(N),还没有整除的情况,就说明该数是素数,这里番茄酱同学给出了两个改进点:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(1).过分追求时间,可以将一定范围内的所有素数放入Static const的数组中,做二分法查找。
(2).标准做法,for循环,n=3开始n+=2.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这里为何可以n+=2呢?理解这句话很关键,如果一个数不能被2整除,那么它就一定不能被2以上的其他偶数整除。所以大于2的偶数可以被忽略了。
【思路2】筛法,筛法是求一定范围内所有素数的算法。
具体做法:先把N个自然数按次序排列起来。1不是质数,也不是合数,要划去。第二个数2是质数留下来,而把2后面所有能被2整除的数都划去。2后面第一个没划去的数是3,把3留下,再把3后面所有能被3整除的数都划去。3后面第一个没划去的数是5,把5留下,再把5后面所有能被5整除的数都划去。这样一直做下去,就会把不超过N的全部合数都筛掉,留下的就是不超过N的全部质数。
【思路3】关于判断素数还有一些高效的不确定性算法,具体请移步至http://wenku.baidu.com/view/ab5139697e21af45b307a812.html
【代码】
1 #include <iostream> 2 #include <string> 3 #include <cmath> 4 5 using namespace std; 6 7 bool isPrime(int x) { 8 if (x < 3) 9 return x == 2 ? true : false; 10 11 if (x % 2 == 0) 12 return false; 13 14 int sqrt_root = (int)sqrt(x); 15 for (int i=3; i<=sqrt_root; i += 2) 16 if (x % i == 0) 17 return false; 18 19 return true; 20 } 21 22 int main() { 23 int x; 24 while (cin >> x) { 25 string str = isPrime(x) ? "Yes" : "No"; 26 cout << "Is " << x << " a prime? " << str << endl; 27 } 28 29 return 0; 30 }