您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页C#查找程序

C#查找程序

来源:爱go旅游网
1.顺序查找

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication6 {

class Program {

{

201, 198,

178, 222,87 };

{

};

{

L[0] = key; {

i = i - 1; }

{

} {

}

q = }

static void Main(string[] args) int[] L = new int[31] {0,10, 6, 98, 78, 45, 69, 79, 52, 132, 245, 255, 115, 145,39, 28, 17, 87, 43, 53, 156, 176, 186, 196, 211, 111, int m;

Console.WriteLine(\"数组L为:\"); for (m = 1; m < 31; m++) Console.Write(\"{0}\", L[m]); Console.Write(\" \"); Console.WriteLine(\"请输入要查找的数\"); int q; do int key = Convert.ToInt32(Console.ReadLine()); int i = 31; do while (L[i] != L[0]); if (i == 0) Console.WriteLine(\"查找的数不存在.\"); else Console.WriteLine(\"要查找的数在第{0}个数\", i); Console.WriteLine(\"继续查找请输入1,否则请输入2\"); Convert.ToInt32(Console.ReadLine()); while (q == 1);

} } }

2.二分法查找

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms;

namespace WindowsFormsApplication3 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

private void button1_Click(object sender, EventArgs e);

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

private void label1_Click(object sender, EventArgs e) {

}

int[] a = new int[15] { 9, 15, 18, 21, 23, 29, 32, 36, 39, 43, 46, 50, , 56, 69 };

private void button1_Click(object sender, EventArgs e) {

int low = 0, high = a.Length, mid = 0; int key = Convert.ToInt16(textBox1.Text); while(low<=high) {

mid = (low + high) / 2; if (key == a[mid]) {

listBox1.Items.Add(\"找的是\" + mid + \"号元素\"); break; }

else if (key < a[mid]) high = mid - 1; else

low = mid + 1; }

if (low > high)

listBox1.Items.Add(\"找不到\" + key); } } }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务