一个长度为100的无序随机整型数组,且数值范围为[1-100],写一个算法,判断数组中是否有存在重复值。要求单循环实现.

解决方案 »

  1.   

    public string JudgeRepeat(int[] arrayTmp)
    {
    for(int i=0;i<arrayTmp.Length;++i)
    {
    if(arrayTmp[i] != arrayTmp[arrayTmp[i]-1])
    {
    int tmp = arrayTmp[i];
    arrayTmp[arrayTmp[i]-1] = arrayTmp[i];
    arrayTmp[i] = arrayTmp[arrayTmp[i]-1];
    }
    else
    {
    return "有重复";
    }
    }
    return "没有重复";
    }
      

  2.   

    public string JudgeRepeat(int[] arrayTmp)
    {
    arrayList dd = new ArrayList;
    for(int i=0;i<arrayTmp.Length;++i)
    {
        if (dd.contains(arrayTmp[i]) == false )
                                   {
                                       dd.add(arrayTmp[i]) ;
                                   }
    }
                               if(dd.count == arrayTmp.Length) 
                               {
                                  return "没有重复";
                               }
                               else 
                               {
                                  return "重复";
                               }
    }
      

  3.   

    就是这意思 你try上也成,有重复的会报错
      

  4.   

    foreach(int i in arrayTmp)
    {
       try
       {
    hs.Add(i,"");
       }
    catch
       {
    "有重复"
      }
    }//大概这样
      

  5.   

    Hashtable hs=new Hashtable();……
      

  6.   

    你的单向循环是不是说给定的数组不能在重复循环。
    我的方法是在建一个数组。
    例如,你要判断的数组是A,新建数组是B
    循环A数组,在B中查找是否有和A[i]相同的值,如果没有就往B中插入A[i]的值,i++后在到B中查找是否有和A[i++]相同的值。呵呵,其实还是用到了多次循环。
      

  7.   

    仅判断有否存在重复? 也不限其他条件?
    bool[] mybl = new bool[100];
    int [] myint;
    //赋值
    for(int i=0,int n=myint.Length;i<n;i++)
    {
        if(mybl[myint[i]])
          {
              MessageBox.show( "重复:"+myint[i]]);
              break;
          }
        else
        {
           mybl[myint[i]]=true;
         }
    }
      

  8.   

    private bool CheckV(int[] tmp)
    {
    StringBuilder builder =new StringBuilder(";");
    for(int i=0;i<=tmp.Length;i++)
    {
    if(builder.ToString().IndexOf(";"+tmp[i].ToString()+";")>0)
    {
    return true;
    }
    else
    {
    builder.Append(tmp[i].ToString()).Append(";");
    }
    }
    return false;
    }
      

  9.   

    用一个一百的数组,然后从那一百个数中一个个的读取数将数组的索引和数组的值设为相同,当出现想同时,数组的值便不会为0了judgewhetherrepeat(int x[100])
    {
         int j=0;
         int y[100]={0};
         for(int i=0; i<100; i++)
         {
             j = x[i];
             if(y[j] == 0)
             {
                y[j] = x[i];
             }
             else
                Response.Write("有重复的数");
         }
    }
      

  10.   

    terry_12(大撒发射点)的算法可行哦
      

  11.   

    晕,很简单的问题嘛,一次循环OK啦....................Private Function check_repeat(ByRef arr() As Integer) As Boolean
            Dim i As Integer
            Dim repeat As Boolean = False
            Dim temp As String = ","
            For i = 0 To UBound(arr)
                temp = temp & arr(i) & ","
                If InStr(temp, "," & arr(i) & ",") > 0 Then
                    repeat = True
                    Exit For
                End If
            Next
            Return repeat
    End Function
      

  12.   

    修正一下:Private Function check_repeat(ByRef arr() As Integer) As Boolean
            Dim i As Integer
            Dim repeat As Boolean = False
            Dim temp As String = ","
            For i = 0 To UBound(arr)
                If InStr(temp, "," & arr(i) & ",") > 0 Then
                    repeat = True
                    Exit For
                End If
                temp = temp & arr(i) & ","
            Next
            Return repeat
    End Function
      

  13.   

    跟soldierluo的差不多:)
    如下:
    void JudgeRepeat(int A[])//A的长度100......

      int B[100]={0};
      int i=0;
      for( i=0; i<100; i++)
      {
        B[A[i]]++;
        if ( B[A[i]]>1 )
        {
          printf("有重复");  
          return;
       }
     }
      printf("没有重复");
      return;
    }
      

  14.   

    刚才发的下标有点错:)
    更正如下:
    void JudgeRepeat(int A[])//A的长度100......

      int B[100]={0};
      int i=0;
      for( i=0; i<100; i++)
      {
        B[A[i]-1]++;
        if ( B[A[i]-1]>1 )
        {
          printf("有重复");  
          return;
       }
     }
      printf("没有重复");
      return;
    }
    //下标0.......99
      

  15.   

    需要那么复杂吗?主要方法和上面 Aladins() 说的一样,不过不需要用int[100],用bool[100]就够了,因为你仅仅需要判断是否重复,不需要判断哪个数值重复以及重复多少次。这样的问题最好发到算法板,通常很快就有个好答案。
      

  16.   

    这个很简单,先排个序,再判断相邻又没有重复。
    或者直接用hashtable
      

  17.   

    cancerser(都是混饭吃,记得要结帖) 
    全加起来 不得 1-100的和 就是有重复的
    ---------------------------呵呵,我觉得如果是仅仅判断是否有重复的话用,这位兄弟的是最简单的了,
    因为不重复的和只有一种情况,等于1-100的和.
      

  18.   

    运用正则表达式匹配
    Regex reg = new Regex(str);
    Match m = reg.Match(Array);
    if(m.Success)
    {
    return repeat;
    }
    else
    return true;
      

  19.   

    public string JudgeRepeat(int[] arrayTmp)
    {
    int i=0;
    int getValue=1;
    while(getValue == arrayTmp[i])
    {
    ++getValue;
    ++i;
    }
    getValue = arrayTmp[i];
    while(arrayTmp[getValue-1] != getValue && i<100)
    {
    int tmp = arrayTmp[getValue-1];
    arrayTmp[getValue-1] = getValue;
    getValue = tmp;
    ++i;
    }
    if(i < 100)
    {
    return "有重复";
    }
    else
    {
    return "没有重复";
    }
    }
      

  20.   

    98star(编程我没干过,不专业啊。) 做法简单明了,可是不知道怎么加分呀