1) 在一串數字中將重複的刪除
bool bIsDup = false;
int[] iRawData = { 0, 3, 5, 3, 2, 1, 0 };
List<int> lstAns = new List<int>();
for (int i = 0; i < iRawData.Count(); i++)
{
bIsDup = false;
foreach (var a in lstAns)
if (iRawData[i] == a)
{
bIsDup = true;
break;
}
if (!bIsDup)
lstAns.Add(iRawData[i]);
}
iRawData = lstAns.ToArray();
2) 在一個數組裡找出兩個數,使得他們的和為給定的數值 (例如7)
int[] iRawData = { 0, 3, 5, 3, 2, 1, 0, 9, 8, 3, 2, 1, 7, 6, 5};
int iAns = 7;
List<KeyValuePair<int,int>> lstAns = new List<KeyValuePair<int,int>>();
for (int i = 0; i < iRawData.Count(); i++)
{
for (int j = 0; j < iRawData.Count(); j++)
{
if (iRawData[i]+iRawData[j] == iAns)
lstAns.Add(new KeyValuePair<int,int>(iRawData[i],iRawData[j]));
}
}
3)
沒有留言:
張貼留言