输入6个整数,如果6个数中有且仅有3个不同的返回true,否则返回false 最简单方便的算法 如何写? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private bool ThreeNumbers(int[] array) { ArrayList list=new ArrayList(); foreach(int n in array) { if(!list.Contains(n)) { list.Add(n); if(list.Count>3) return false; } } if(list.Count==3) return true; else return false; } 用一个局部变量来计数,应该会更快。 private bool ThreeNumbers(int[] array) { ArrayList list=new ArrayList(); int count=0; foreach(int n in array) { if(!list.Contains(n)) { list.Add(n); count++; if(count>3) return false; } } if(count==3) return true; else return false; } if(count==3) return true; else return false;为什么不写成return count == 3; 按我理解输入输出是不是这样的?1,1,1,2,2,3 = false1,1,2,2,3,3 = false1,2,3,4,4,4 = true1,2,2,2,3,4 = true private bool ThreeNumbers(int[] array) { ArrayList list=new ArrayList(); foreach(int n in array) { if(!list.Contains(n)) { list.Add(n); if(list.Count>3) return false; } } if(list.Count==3) return true; else return false; } int[] tempArrayInt = new tempArrayInt [6];//存放你的数组List<Int> tempList = new List<Int>();for(int i= 0;i <tempArrayInt .Length;i++ ){ if(tempList.Count > 0){ for(int j=0;j<tempList.Count;j++){if(tempArrayInt [i] == tempList[j]){break;}else{if(j == tempList.Count -1 && tempArrayInt [i] != tempList[j]){tempList.Add(tempArrayInt [i]);}}}}else{ tempList.Add(tempArrayInt [i]);}if(tempList.Count != 3){ return false;}else{return true;}} int []a=new int[6]for(int i=1;i<a.length;i++){if(a[i]!=0]){int s=a[i]/a[i-1]{if(s==1)int aa=s++;if(aa==3)return;}}} Linq最简洁...retutn (array.Distinct().Count() - 1) == 3; private void Form1_Load(object sender, EventArgs e) { //先排序,然后相临的相减. 仅有相临的3个相减结果等于零的时候return true . 其他的false ; int[] a =new int[]{ 1,1,5,1,9,7}; Array.Sort(a); int[] result = new int[5]; result[0] = a[0]-a[1]; result[1] = a[1]-a[2]; result[2] = a[2]-a[3]; result[3] = a[3]-a[4]; result[4] = a[4]-a[5]; int j =0 ; for (int i=0 ; i<5;i++) { if (result[i]==0) j++; } if (j == 2) { MessageBox.Show("true"); } else { MessageBox.Show("false"); } }逻辑上好象简单些.虽涉及数组排序,但时间复杂度上还是我想到最小的,其他的要涉及好多比较过程,其实就是变相的排序.还会比先排序耗时多些! retutn (array.Distinct().Count() - 1) == 3;呵呵,我看到问题就直接想Distinct了,还没回就看见11楼的了 treegridview绑定的问题 多线程问题,在调用一个线程的时候,如何保证界面不死 如何设置string的第i个字符 请问C#中怎样才能安装字体? 如果为一个没有KeyPress事件的控件添加KeyPress事件 想请教一个Excel和Sql的问题 求一算法,高人进 System.UnauthorizedAccessException was unhandled问题. C#窗体文本显示线程问题 C#数据绑定中的多个问题。 如何实现在多选项卡间搜索 怎么捕vs启动和关闭的事件?
{
ArrayList list=new ArrayList();
foreach(int n in array)
{
if(!list.Contains(n))
{
list.Add(n);
if(list.Count>3) return false;
}
}
if(list.Count==3) return true;
else return false;
}
{
ArrayList list=new ArrayList();
int count=0;
foreach(int n in array)
{
if(!list.Contains(n))
{
list.Add(n);
count++;
if(count>3) return false;
}
}
if(count==3) return true;
else return false;
}
else return false;
为什么不写成
return count == 3;
1,1,1,2,2,3 = false
1,1,2,2,3,3 = false
1,2,3,4,4,4 = true
1,2,2,2,3,4 = true
{
ArrayList list=new ArrayList();
foreach(int n in array)
{
if(!list.Contains(n))
{
list.Add(n);
if(list.Count>3) return false;
}
}
if(list.Count==3) return true;
else return false;
}
int[] tempArrayInt = new tempArrayInt [6];//存放你的数组
List<Int> tempList = new List<Int>();
for(int i= 0;i <tempArrayInt .Length;i++ )
{
if(tempList.Count > 0)
{
for(int j=0;j<tempList.Count;j++)
{
if(tempArrayInt [i] == tempList[j])
{
break;
}
else
{
if(j == tempList.Count -1 && tempArrayInt [i] != tempList[j])
{
tempList.Add(tempArrayInt [i]);
}
}
}
}
else
{
tempList.Add(tempArrayInt [i]);
}if(tempList.Count != 3)
{
return false;
}
else
{
return true;
}
}
for(int i=1;i<a.length;i++)
{
if(a[i]!=0])
{
int s=a[i]/a[i-1]{
if(s==1)int aa=s++;
if(aa==3)
return;}
}}
{
//先排序,然后相临的相减. 仅有相临的3个相减结果等于零的时候return true . 其他的false ;
int[] a =new int[]{ 1,1,5,1,9,7};
Array.Sort(a);
int[] result = new int[5];
result[0] = a[0]-a[1];
result[1] = a[1]-a[2];
result[2] = a[2]-a[3];
result[3] = a[3]-a[4];
result[4] = a[4]-a[5];
int j =0 ;
for (int i=0 ; i<5;i++)
{
if (result[i]==0)
j++;
}
if (j == 2)
{
MessageBox.Show("true");
}
else
{
MessageBox.Show("false"); } }
逻辑上好象简单些.虽涉及数组排序,但时间复杂度上还是我想到最小的,其他的要涉及好多比较过程,其实就是变相的排序.还会比先排序耗时多些!