while的实现
#include<iostream> using namespace std; void whilee1(int *a,int shang,int xia,int num)// { cout << "shang\tzhong\txia\t " << endl; while (shang >=xia)//如果把序号大的看作上面用shang > xia,序号小的看作上面用xia<shang { int zhong = (shang + xia) / 2; cout << shang << "\t" << zhong << "\t" << xia << endl; if (num == a[zhong]) { cout << "whilee1 find" << endl; break; } else if (a[zhong]<num) { xia = zhong + 1; } else //(num<zhong) { shang = zhong - 1; } } if (xia >shang) { cout << "while1 not find " << endl; } } void whilee2(int *a, int xia, int shang, int num) { cout << "shang\tzhong\txia\t " << endl; while (shang<=xia)//如果把序号大的看作上面用shang >= xia,序号小的看作上面用xia<shang { int zhong = (shang + xia) / 2; cout << shang << "\t" << zhong << "\t" << xia << endl; if (num == a[zhong]) { cout << "whilee2 find" << endl; break; } else if (a[zhong]<num) { shang = zhong + 1; } else //(num<zhong) { xia = zhong - 1; } } if (shang>xia) { cout << "while2 not find " << endl; } } dowhile的实现#include<iostream>
using namespace std; void dowhile1(int *a,int shang,int xia,int num) { int zhong; cout << "shang\tzhong\txia\t " << endl; do { zhong = (shang + xia) / 2; cout << shang<<"\t"<<zhong<<"\t"<<xia<<"\t " << endl; if (num == a[zhong]) { cout << "dowhile1 find" << endl; break; } else if (num > a[zhong]) { xia = zhong + 1; } else { shang = zhong - 1; } } while (shang >= xia); if (xia >shang) { cout << "dowhile1 not fid" << endl; } } void dowhile2(int *a, int xia, int shang, int num) { int zhong; cout << "shang\tzhong\txia\t " << endl; do { zhong = (shang + xia) / 2; cout << shang << "\t" << zhong << "\t" << xia << "\t " << endl; if (num == a[zhong]) { cout << "dowhile2 find" << endl; break; } else if (num > a[zhong]) { shang = zhong + 1; } else { xia = zhong - 1; } } while (xia>=shang); if (shang>xia) { cout << "dowhile2 not fid" << endl; } }
for的实现
#include<iostream> using namespace std; void forr(int *a, int shang, int xia, int num) { int zhong; cout << "shang\tzhong\txia\t" << endl; for (zhong = (shang + xia) / 2; shang >= xia;zhong=(shang+xia)/2) { cout << shang<<"\t"<<zhong<<"\t"<<xia<<"\t" << endl; if (num==a[zhong]) { cout << "for find" << endl; break; } else if (num > a[zhong]) { xia = zhong + 1; } else { shang = zhong - 1; } } if (xia > shang) { cout << "for not find" << endl; } }
goto的实现
#include<iostream> using namespace std; void gotoo(int *a, int shang, int xia, int num) { if (shang >= xia) { cout << "shang\tzhong\txia\t" << endl; A: int zhong = (shang + xia) / 2; cout << shang << "\t" << zhong << "\t" << xia << "\t" << endl; if (num == a[zhong]) { cout << "goto find" << endl; } else if (num > a[zhong]) { if (shang > xia) { xia = zhong + 1; goto A; } goto B; } else //(num < a[zhong]) { if (shang > xia) { shang = zhong - 1; goto A; } } } if(shang<xia) { B:cout << "goto not find" << endl; } }
递归的实现
#include<iostream> using namespace std; void recurrence(int *a, int shang, int xia, int num) { if (shang >=xia) { int zhong = (shang + xia) / 2; cout << shang << "\t" << zhong << "\t" << xia << endl; if (num == a[zhong]) { cout << "recurrence find" << endl; } else if (num > a[zhong]) { recurrence(a, shang, zhong + 1, num); } else { recurrence(a, zhong - 1, xia, num); } } else { cout << "recurrence not find" << endl; } }
时间: 2024-10-11 05:00:26