| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 640 人关注过本帖
标题:[求助]找错:用二分法在数组中找80的下标
只看楼主 加入收藏
曲昌文
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-5-22
收藏
 问题点数:0 回复次数:4 
[求助]找错:用二分法在数组中找80的下标

//用二分法在数组中找80的下标
using System;

class class1
{
static void Main(string[] args)
{
int[] a = new int[25];
int x = a.Length;
int i;
int top = x;
int down = 0;

for(i = 0; i < 25; i++)
{
a[i] = int.Parse(Console.ReadLine());
}

i = x / 2;

while(a[i] != 80)
{
if(a[i] < 80)
{
i = top / 2;
top = i;
}
else if(a[i] > 80)
{
down = x - i;
i += (down / 2);
}
}
Console.WriteLine(i);
}
}

搜索更多相关主题的帖子: 二分法 int void using 
2007-08-01 22:15
cheumen
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-8-1
收藏
得分:0 

初步测试没有问题:
如果有什么问题,请提出来,大家讨论下;
namespace ns
{

class class1
{
static void Main(string[] args)
{
int middle = 0;
int head = 0;
int rear = 0;
int length = 0;

Console.WriteLine("Please input the length of your Array");
length = Convert.ToInt32(Console.ReadLine());
rear = length - 1;
int [] myArray=new int [length];

Console.WriteLine();
Console.WriteLine("Please input {0} elements to your Array", length);

for (int i = 0; i < length; i++)
{
Console.Write("[{0}]:",i+1);
myArray[i] = Convert.ToInt32(Console.ReadLine());
}

Console.WriteLine();
Console.WriteLine("Please input the element you want to find");
int num=Convert.ToInt32(Console.ReadLine());

middle = (head + rear) / 2;

while ((myArray[middle] != num) && (head<=rear))
{
if (myArray[middle] < num)
{
head = middle + 1;
}
if(myArray[middle]>num)
{
rear = middle - 1;
}
middle = (head + rear) / 2;
}

Console.WriteLine();
Console.WriteLine("Result is:");
Console.WriteLine((head>rear)?"Can't find":Convert.ToString(middle+1));

}
}

}

[此贴子已经被作者于2007-8-2 8:01:46编辑过]

2007-08-02 00:56
曲昌文
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-5-22
收藏
得分:0 
不懂

http://blog..cn/bfgl我的博客
2007-08-06 20:36
曲昌文
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-5-22
收藏
得分:0 
错误

http://blog..cn/bfgl我的博客
2007-08-06 20:41
cheumen
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-8-1
收藏
得分:0 

有什么错误?

2007-08-07 00:22
快速回复:[求助]找错:用二分法在数组中找80的下标
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017968 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved