while (upperBound - 1 > 0) { for (int j = 1; j < upperBound; ++j) { if (items[0] + items[j] == 10) { Console.WriteLine("{0} + {1} ", items[0], items[j]); swap(ref items, j, upperBound); upperBound--; break; } } swap(ref items, 0, upperBound); upperBound--; } } void swap(ref int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }
int[] items = new int[] { 1, 1, 2,2, 3, 4, 5, 6, 7, 8 }; var Query = from A in items from B in items where A + B == 10 && A+A<11 select new { A = A, B = B }; foreach (var v in Query) { Console.WriteLine("{0},{1}",v.A,v.B); } /* 2,8 2,8 3,7 4,6 5,5 */ 你上个帖子里面我有些啊,你看看
这个效率应该会很高 只需要遍历一次 且只考虑小于5的数 private void button1_Click(object sender, EventArgs e) { List<int> lstContent = new List<int>(); int[] items = new int[] { 1, 1, 2,2,3, 4, 5, 6, 7, 8 }; foreach (int i in items) { if (i >= 5) break; if (lstContent.Contains(i)) continue; lstContent.Add(i); if (items.Contains(10 - i)) { lstContent.Add(10-i); listView1.Items.Add(i.ToString()+","+(10 - i).ToString()); } } }
using System; using System.Collections; using System.Collections.Generic;public class hello { static void Main(String[]args){ int[] items = new int[] {1,1,2,2,3, 4, 5, 6, 7, 8}; Hashtable ht=new Hashtable(); int len=items.Length; for(int i=0;i<len;i++){ if(ht.Contains(items[i])) continue; ht.Add(items[i],"1"); for(int j=i+1;j<len;j++){ if(ht.Contains(items[j])) continue; if(items[i]+items[j]==10){ Console.WriteLine("{0},{1}",items[i],items[j]); break; }
} }
} }
using System; using System.Collections; using System.Collections.Generic;public class hello { static void Main(String[]args){ int[] items = new int[] {1,1,2,2,3, 4, 5, 6, 7, 8}; Hashtable ht=new Hashtable(); int len=items.Length; for(int i=0;i<len;i++){ if(ht.Contains(items[i])) continue; ht.Add(items[i],"1"); for(int j=i+1;j<len;j++){ if(ht.Contains(items[j])) continue; if(items[i]+items[j]==10){ Console.WriteLine("{0},{1}",items[i],items[j]); break; }
{
int[] items = new int[] { 1, 1, 2, 2, 3, 4, 5, 6, 7, 8 }; int upperBound = items.Length - 1;
while (upperBound - 1 > 0)
{
for (int j = 1; j < upperBound; ++j)
{
if (items[0] + items[j] == 10)
{
Console.WriteLine("{0} + {1} ", items[0], items[j]);
swap(ref items, j, upperBound);
upperBound--;
break;
}
} swap(ref items, 0, upperBound);
upperBound--;
}
}
void swap(ref int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
var Query = from A in items
from B in items
where A + B == 10 && A+A<11
select new { A = A, B = B };
foreach (var v in Query)
{
Console.WriteLine("{0},{1}",v.A,v.B);
}
/*
2,8
2,8
3,7
4,6
5,5 */
你上个帖子里面我有些啊,你看看
private void button1_Click(object sender, EventArgs e)
{
List<int> lstContent = new List<int>();
int[] items = new int[] { 1, 1, 2,2,3, 4, 5, 6, 7, 8 }; foreach (int i in items)
{
if (i >= 5)
break; if (lstContent.Contains(i))
continue; lstContent.Add(i); if (items.Contains(10 - i))
{
lstContent.Add(10-i); listView1.Items.Add(i.ToString()+","+(10 - i).ToString());
}
} }
using System.Collections;
using System.Collections.Generic;public class hello
{
static void Main(String[]args){
int[] items = new int[] {1,1,2,2,3, 4, 5, 6, 7, 8};
Hashtable ht=new Hashtable();
int len=items.Length;
for(int i=0;i<len;i++){
if(ht.Contains(items[i])) continue;
ht.Add(items[i],"1");
for(int j=i+1;j<len;j++){
if(ht.Contains(items[j])) continue;
if(items[i]+items[j]==10){
Console.WriteLine("{0},{1}",items[i],items[j]);
break;
}
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;public class hello
{
static void Main(String[]args){
int[] items = new int[] {1,1,2,2,3, 4, 5, 6, 7, 8};
Hashtable ht=new Hashtable();
int len=items.Length;
for(int i=0;i<len;i++){
if(ht.Contains(items[i])) continue;
ht.Add(items[i],"1");
for(int j=i+1;j<len;j++){
if(ht.Contains(items[j])) continue;
if(items[i]+items[j]==10){
Console.WriteLine("{0},{1}",items[i],items[j]);
break;
}
}
}
}
}
如果int[] items中包含了两个2和两个8,例如{ 1, 1, 2,2,3, 4, 5, 6, 7, 8,8 };
是不是就只能找出一对{2,8}?
而且你的程序出现两次遍历 这个根本不需要遍历两次一次足够 要是想实现 {2,8},{2,8},{3,7},{4,6}这个结果 只需要删除这行代码
if (lstContent.Contains(i))
continue;
{2,8},{3,7},{4,6}
要是期望结果为
{2,8},{2,8},{3,7},{4,6}
只需要去掉if (lstContent.Contains(i))
continue;甚至去掉lstContent的定义 可以少5行代码左右
void print_special_pairs(int int_array[], size_t array_length, int special_value)
{
int i = 0,j = array_length - 1,monitor = -1, sum = -1; while (i < j)
{
sum = int_array[i] + int_array[j]; if (sum == special_value)
{
int temp = i++; if (monitor == int_array[temp] || int_array[temp] == int_array[j]) // duplicate! omit this pair.
continue; monitor = int_array[temp];
printf("\n int_array[i] = %d, int_array[j] = %d\n", int_array[temp], int_array[j]);
}
else
{
sum < special_value? (i++):(j--);
continue;
}
}
}int main(){
int iArray[] = {1, 2, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9};
int iSpecial = 10;
print_special_pairs(iArray, sizeof(iArray)/sizeof(int), 10);
return 0;
}result: int_array[i] = 1, int_array[j] = 9 int_array[i] = 2, int_array[j] = 8 int_array[i] = 3, int_array[j] = 7 int_array[i] = 4, int_array[j] = 6