【初学者求教】知道数组里某一位的值,求该位索引。 string[] RVR = new string[1024]; RVR[0] = "fff"; RVR[1] = "sdw"; RVR[2] = "ffs"; RVR[3] = "bbs"; . . RVR[19999] ="abc" . .假设我现在只知道abc,我想知道abc对应的数组RVR的索引等于19999。应该用什么方法实现合适?PS:我有个思路,就是用一个循环,变量就是索引值,一个一个去验证是否相同。但我总感觉这个方法有点别扭。希望高手指点。感激不尽!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void button2_Click(object sender, EventArgs e){ string[] RVR = new string[1024]; RVR[0] = "fff"; RVR[1] = "sdw"; RVR[2] = "ffs"; RVR[3] = "bbs"; RVR[199] ="abc"; MessageBox.Show(Array.IndexOf(RVR, "abc").ToString());} 折半查找法,提高效力//******************************************************************************************************************************//// 折半查找法的C代码实现//// 张明烁编写于2008年6月7日////******************************************************************************************************************************#include <stdio.h>#define N 51void main(void){int a[N];int i,n,num;int top,bottom,mid;int flag=1; //如果在表列中找到数字,则值为1,否则为0int loc=-1;//要查找的数在表列中的位置,如果loca=-1表示表列中没有这个数;如果有这个数,则它的值为所在的位置printf("你想在多少个数中进行折半查找,请输入(1--50):");scanf("%d",&n);while(n<1 || n>50){printf("你输入的数不正确,请重新输入。\n");printf("你想在多少个数中进行折半查找,请输入(1--50):");scanf("%d",&n);}printf("请你输入一个整数 a[1](需保证递增有序):");scanf("%d",&a[1]);i=2;while(i<=n) //输入从小到大的表列{printf("请你输入一个整数 a[%d](需保证递增有序):",i);scanf("%d",&a[i]);if(a[i] > a[i-1]) i++;else printf("你输入的数不满足要求,请重新输入。\n");}//输出表列printf("\n输出表列\n");for(i=1; i<=n; i++){printf("%6d",a[i]);}printf("\n");printf("请你输入要查找的数:");scanf("%d",&num);flag=1; //假设输入的数在表列中top=n;bottom=1;mid=(top+bottom)/2;while(flag) { //printf("top=%d, bottom=%d, mid=%d, a[%d]=%d\n",top,bottom,mid,mid,a[mid]);if( (num>a[top]) || (num<a[bottom]) ) //输入的数 num>a[top] 或者 num<a[bottom],肯定num不在这个表列中{ loc=-1; flag=0;}else if(a[mid]==num) //如果num 等于找到的数{ loc=mid; printf("找到数 %6d 的位置%2d\n",num,loc); break;}else if(a[mid]>num) //若 a[mid]>num,则num 一定在 a[bottom]和a[mid-1]范围之内{ top=mid-1; mid=(top+bottom)/2;}else if(a[mid]<num) //若 a[mid]<num,则num 一定在 a[mid+1]和a[top]范围之内{ bottom=mid+1; mid=(top+bottom)/2;}}if(loc==-1){ printf("%d 这个数在表列中没有找到。\n",num);} printf("折半查找结束,按任意键退出:\n"); }C#代码http://m.cnblogs.com/21954/599648.html SQL语句求解,SQL语句求解,SQL语句求解 大虾救我:C#调用matlab中傅里叶变换对图片处理如何操作? 【推荐】C# 实现 100G数据文件查找100个最大值 【2010支付宝西安最新笔试题】 请进来帮我看下 导出为excel格式的方法之一:以xml为中间文件,具体如何操作 一个删除数据的问题 做了个。NET的视频捕捉控件免费给大家使用 关于TrackBar的问题,请高手指教! 求前几天一帖地址:如何用C#实现ping命令 怎样在listbox中并排显示两个字段的内容?? C# 反射 无法给委托绑定方法? c# 三层问题 高手进来看看 为什么 修改失败
private void button2_Click(object sender, EventArgs e)
{
string[] RVR = new string[1024];
RVR[0] = "fff";
RVR[1] = "sdw";
RVR[2] = "ffs";
RVR[3] = "bbs";
RVR[199] ="abc"; MessageBox.Show(Array.IndexOf(RVR, "abc").ToString());
}
//
// 折半查找法的C代码实现
//
// 张明烁编写于2008年6月7日
//
//******************************************************************************************************************************
#include <stdio.h>
#define N 51
void main(void)
{
int a[N];
int i,n,num;
int top,bottom,mid;
int flag=1; //如果在表列中找到数字,则值为1,否则为0
int loc=-1;//要查找的数在表列中的位置,如果loca=-1表示表列中没有这个数;如果有这个数,则它的值为所在的位置printf("你想在多少个数中进行折半查找,请输入(1--50):");
scanf("%d",&n);while(n<1 || n>50)
{
printf("你输入的数不正确,请重新输入。\n");
printf("你想在多少个数中进行折半查找,请输入(1--50):");
scanf("%d",&n);
}printf("请你输入一个整数 a[1](需保证递增有序):");
scanf("%d",&a[1]);i=2;
while(i<=n) //输入从小到大的表列
{
printf("请你输入一个整数 a[%d](需保证递增有序):",i);
scanf("%d",&a[i]);
if(a[i] > a[i-1])
i++;
else
printf("你输入的数不满足要求,请重新输入。\n");
}//输出表列
printf("\n输出表列\n");
for(i=1; i<=n; i++)
{
printf("%6d",a[i]);
}
printf("\n");printf("请你输入要查找的数:");
scanf("%d",&num);flag=1; //假设输入的数在表列中top=n;
bottom=1;
mid=(top+bottom)/2;while(flag)
{
//printf("top=%d, bottom=%d, mid=%d, a[%d]=%d\n",top,bottom,mid,mid,a[mid]);if( (num>a[top]) || (num<a[bottom]) ) //输入的数 num>a[top] 或者 num<a[bottom],肯定num不在这个表列中
{
loc=-1;
flag=0;
}
else if(a[mid]==num) //如果num 等于找到的数
{
loc=mid;
printf("找到数 %6d 的位置%2d\n",num,loc);
break;
}
else if(a[mid]>num) //若 a[mid]>num,则num 一定在 a[bottom]和a[mid-1]范围之内
{
top=mid-1;
mid=(top+bottom)/2;
}
else if(a[mid]<num) //若 a[mid]<num,则num 一定在 a[mid+1]和a[top]范围之内
{
bottom=mid+1;
mid=(top+bottom)/2;
}
}if(loc==-1)
{
printf("%d 这个数在表列中没有找到。\n",num);
}
printf("折半查找结束,按任意键退出:\n");
}
C#代码http://m.cnblogs.com/21954/599648.html