您的位置:首页 > 其它

算法-->顺序查找

2017-07-22 22:50 232 查看
package 顺序查找;

import java.util.Scanner;

public class ShunXu {
static final int N = 15;

static int searchFun(int a[], int n, int x) {
int i, f = -1;
for (i = 0; i < n; i++) {
if (x == a[i]) {
f = i;
break;
}
}
return f;
}

public static void main(String[] args) {
int x, n, i;
ShunXu sh = new ShunXu();
int[] shuzu = new int
;
for (i = 0; i < N; i++) {
shuzu[i] = (int) (100 + Math.random() * (100 + 1));
}
System.out.print("顺序查找演示\n");
System.out.print("数据序列\n");
for (i = 0; i < N; i++) {
System.out.print(" " + shuzu[i]);
}
System.out.print("\n\n");
System.out.print("输入要查找的数\n");
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
n = searchFun(shuzu, N, x);
if (n < 0) {
System.out.print("没找到数据" + x);
} else {
System.out.println("数据 " + x + "位于数据的第" + (n + 1) + "个元素处");
}

}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 顺序查找