/// <re> /// arrA and arrB should be already sorted /// </re> static void PrintSameElements(int[] arrA, int[] arrB) { int floor = 0; foreach (int b in arrB) { for (int i = floor; i < arrA.Length; i++) { if (b == arrA[i]) { Console.WriteLine(b); break; } else if (b < arrA[i]) { floor = i; break; } } } }
int EquallCount=0; int i,j=0;do { if(a[i]>b[j]) { j++; } else if(a[i]<b[j]) { i++; } else { count++ } }while(i!=a.count||j!=b.count)return count;//楼主是这个意思吗?
This one is better :-)
更改了一些错误(原代码见10楼): static int f(int[] a, int[] b) { int count = 0; int i = 0, j = 0; while (i < a.Length && j < b.Length) { if (a[i] > b[j]) { j++; } else if (a[i] < b[j]) { i++; } else { i++; j++; count++; } } return count; }
一个数组中元素可以重复吗?这样的话算不算重复count增加吗/int[] a = new int[] { 1, 3, 4, 5, 6, 6, 7 }; int[] b = new int[] { 3, 3, 4, 6, 7, 9 };
static int compareTowArray(int[] nAAry, int[] nBAry) { int nCount = 0; int i = 0, j = 0; Boolean bASolved = false, bBSolved = false; while (!bASolved || !bBSolved) { if ((i < nAAry.Length) && (j < nBAry.Length)) { if (nAAry[i] <= nBAry[j]) { if (nAAry[i] == nBAry[j]) { nCount++; } i++; } else { j++; } } else { if (i == nAAry.Length) { i--; j++; bASolved = true; continue; } if (j == nBAry.Length) { j--; i++; bBSolved = true; continue; } } } return nCount; }
并不简单啊,修改版 static int compareTowArray(int[] nAAry, int[] nBAry) { int nCount = 0; int i = 0, j = 0; Boolean bASolved = false, bBSolved = false; int nRecordi = -2, nRecordj = -2; while (!bASolved || !bBSolved) { if ((i < nAAry.Length) && (j < nBAry.Length)) { if (nAAry[i] <= nBAry[j]) { if (nAAry[i] == nBAry[j]) { if (!(nRecordi + 1 == i && nRecordj == j) && !(nRecordj + 1 == j && nRecordi == i)) { nCount++; nRecordi = i; nRecordj = j; } } i++; } else { j++; } } else { if (i == nAAry.Length) { i--; j++; bASolved = true; continue; } if (j == nBAry.Length) { j--; i++; bBSolved = true; continue; } } } return nCount; }
{
if(strA[cnt] != strB[cnt])
MessageBox.Show("元素不一样!");
}是这个意思么?比较元素?
A B简称爬杆法
把B当成杆,A的元素当成爬杆者
从A中拿最小的元素和B中从小到大元素比较
如果查找到B中有比它大且不相当的元素,
则从A中换一个元素接着爬
for(int cnt;cnt < strA.Length && cnt < strB.Length;cnt++)
{
if(strA[cnt] != strB[cnt])
MessageBox.Show("元素不一样!");
else
i++;
}
MessageBox.Show("strA于strB中相同元素的个数是:" + i.ToString()); z这样差不多了吧。
string[] strA = new string[10];
string[] strB = new string[10];
for (int j = 0; j < strA.Length; j++)
{
for (int i = 0; i < strB.Length; i++)
{
if (strA[j] == strB[i])
cnt++;
}
}
MessageBox.Show("相同元素个数:" + cnt.ToString());
这次对了,绕
for和if是运行最快的了,多套几个不会影响速度的,而且简单,何必自找烦恼呢?
/// <re>
/// arrA and arrB should be already sorted
/// </re>
static void PrintSameElements(int[] arrA, int[] arrB)
{
int floor = 0;
foreach (int b in arrB)
{
for (int i = floor; i < arrA.Length; i++)
{
if (b == arrA[i]) { Console.WriteLine(b); break; }
else if (b < arrA[i]) { floor = i; break; }
}
}
}
int i,j=0;do
{
if(a[i]>b[j])
{
j++;
}
else if(a[i]<b[j])
{
i++;
}
else
{
count++
}
}while(i!=a.count||j!=b.count)return count;//楼主是这个意思吗?
This one is better :-)
{
int count = 0;
int i = 0, j = 0; while (i < a.Length && j < b.Length)
{
if (a[i] > b[j])
{
j++;
}
else if (a[i] < b[j])
{
i++;
}
else
{
i++;
j++;
count++;
}
} return count;
}
int[] b = new int[] { 3, 3, 4, 6, 7, 9 };
{
int nCount = 0;
int i = 0, j = 0;
Boolean bASolved = false, bBSolved = false;
while (!bASolved || !bBSolved)
{
if ((i < nAAry.Length) && (j < nBAry.Length))
{
if (nAAry[i] <= nBAry[j])
{
if (nAAry[i] == nBAry[j])
{
nCount++;
}
i++;
}
else
{
j++;
}
}
else
{
if (i == nAAry.Length)
{
i--;
j++;
bASolved = true;
continue;
} if (j == nBAry.Length)
{
j--;
i++;
bBSolved = true;
continue;
}
}
} return nCount;
}
{
int nCount = 0;
int i = 0, j = 0;
Boolean bASolved = false, bBSolved = false;
int nRecordi = -2, nRecordj = -2;
while (!bASolved || !bBSolved)
{
if ((i < nAAry.Length) && (j < nBAry.Length))
{
if (nAAry[i] <= nBAry[j])
{
if (nAAry[i] == nBAry[j])
{
if (!(nRecordi + 1 == i && nRecordj == j) && !(nRecordj + 1 == j && nRecordi == i))
{
nCount++;
nRecordi = i;
nRecordj = j;
}
}
i++;
}
else
{
j++;
}
}
else
{
if (i == nAAry.Length)
{
i--;
j++;
bASolved = true;
continue;
} if (j == nBAry.Length)
{
j--;
i++;
bBSolved = true;
continue;
}
}
} return nCount;
}