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:我有个思路,就是用一个循环,变量就是索引值,一个一个去验证是否相同。但我总感觉这个方法有点别扭。
希望高手指点。
感激不尽!!!!!
          

解决方案 »

  1.   


    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());
    }
      

  2.   

    折半查找法,提高效力//******************************************************************************************************************************
    //
    //             折半查找法的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