標題:
[隨堂練習] TQC+510 二分搜尋法
[打印本頁]
作者:
周政輝
時間:
2017-5-13 12:31
標題:
[隨堂練習] TQC+510 二分搜尋法
import java.util.Scanner;
public class JPD05 {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] argv) {
search();
search();
}
public static void search() {
int[] data = {5, 9, 13, 15, 17, 19, 25, 30, 45}; // 已排序資料
System.out.print("請輸入要找尋的資料:");
int target = keyboard.nextInt();
int high = data.length-1;
int low = 0;
int mid = 0;
int n = 0;
while(low <=high)
{
System.out.println("尋找區間:"+low+"("+data[low]+").."+high+"("+data[high]+")中間:"+mid+"("+data[mid]+")");
n++;
mid = (high+low)/2;
if(target>data[mid])
{
low = mid+1;
}
else if (target < data[mid])
{
high = mid-1;
}
else{
break;
}
}
System.out.println("經過"+n+"次尋找");
if(target == data[mid])
{
System.out.println("您要找的資料在陣列中的第"+mid+"的位置");
}
else{
System.out.println(target+"不在陣列當中");
}
}
}
複製代碼
作者:
蔡季樺
時間:
2017-5-13 12:37
本帖最後由 蔡季樺 於 2017-8-20 15:14 編輯
import java.util.Scanner;
public class JPA05 {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] argv) {
search();
search();
}
public static void search() {
int[] data = {5, 9, 13, 15, 17, 19, 25, 30, 45};
System.out.print("請輸入要找尋的資料:");
int target = keyboard.nextInt();
int high = data.length-1;
int low = 0;
int mid = 0;
int n = 0;
while(low <=high)
{
mid = (high+low)/2;
n++;
System.out.println("尋找區間:"+low+"("+data[low]+").."+high+"("+data[high]+")中間:"+mid+"("+data[mid]+")");
if(target>data[mid])
{
low = mid+1;
}
else if (target < data[mid])
{
high = mid-1;
}
else{
break;
}
}
System.out.println("經過"+n+"次尋找");
if(target == data[mid])
{
System.out.println("您要找的資料在陣列中的第"+mid+"個位置");
}
else{
System.out.println(target+"不在陣列當中");
}
}
}
複製代碼
作者:
蔡庭豪
時間:
2017-5-13 12:41
import java.util.Scanner;
public class JPA05 {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] argv) {
search();
search();
}
public static void search() {
int[] data = {5, 9, 13, 15, 17, 19, 25, 30, 45}; // 已排序資料
System.out.print("請輸入要找尋的資料:");
int target = keyboard.nextInt();
int high = data.length-1 ;
int low = 0;
int mid = 0;
int n = 0;
while(low<=high)
{
System.out.printf("尋找區間:%d(%d)..%d(%d),中間:%d(%d)",low,data[low],high,data[high],mid,data[mid]);
n++;
mid=(high+low)/2;
if(target>data[mid]){
low = mid+1;
}
else if(target<data[mid]){
high = mid-1;
}
else{
break;
}
}
System.out.println("經過"+n+"次的搜尋");
if(target == data[mid]){
System.out.println("您要找的資料在陣列中的第"+mid+"個位置");
}
else{
System.out.println(target+"不在陣列中");
}
}
}
複製代碼
作者:
王彥甯
時間:
2017-5-13 12:42
import java.util.Scanner;
public class JPA05 {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] argv) {
search();
search();
}
public static void search() {
int[] data = {5, 9, 13, 15, 17, 19, 25, 30, 45}; // 已排序資料
System.out.print("請輸入要找尋的資料:");
int target = keyboard.nextInt();
int high = data.length-1;
int low = 0;
int mid = 0;
int count = 0;
while(low<=high)
{
count++;
mid = (high+low)/2;
System.out.printf("尋找區間:%d(%d)..%d(%d),中間:%d(%d)\n",low,data[low],high,data[high],mid,data[mid]);
if(low<mid)
{
low=mid+1;
}
else if(high>mid)
{
high=mid-1;
}
else
{
break;
}
}
System.out.printf("經過%d次的尋找\n",count);
if(target==data[mid])
{
System.out.printf("您要找的資料在陣列中第%d個位置",mid);
}
else
{
System.out.printf("%d不在陣列中\n",target);
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2