We all know how to find one missing interger in array, my question is how can we find the two missing interger in interger array:
public void Find2MissingNum()
{
int[] iTol = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] iMis = { 1, 2, 4, 6, 7, 8, 9, 10 };
int x, y;
// try to find 3, 5
//sum iMis
int sMis =0 , sTol=0;
foreach (int i in iMis)
{
sMis += i;
}
// sum iTol
sTol = iTol.Length * (iTol.Length + 1) / 2;
// time total
int tMis = 1, tTol = 1;
foreach (int i in iMis)
{
tMis *= i;
}
foreach (int i in iTol)
{
tTol *= i;
}
int timeSmisStol = tTol / tMis;
int difTM = sTol - sMis;
for (int i = 0; i < difTM; i++)
{
if (i * (difTM - i) == timeSmisStol)
{
x = i;
y = difTM - i;
}
}
}
Monday, December 29, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment