一个数组其中有若干个5,10,13如下:
5,10,10,13,13,13,5,5,5,5,
我想让它按5,10,13这样排序,其余按顺序排,如下
5,10,13,5,10,13,5,5,5,13,不知用什么方法这样排序?
5,10,10,13,13,13,5,5,5,5,
我想让它按5,10,13这样排序,其余按顺序排,如下
5,10,13,5,10,13,5,5,5,13,不知用什么方法这样排序?
调试欢乐多
int[] num = {5,10,10,13,13,13,5,5,5,5};
int count5 = 0;
int count10 = 0;
int count13 = 0;
for (int i = 0; i < num.Length; i++)
{
if (num[i] == 5) count5++;
else if (num[i] == 10) count10++;
else if (num[i] == 13) count13++;
}
int count = 0;
count = count5 < count10 ? count5 : count10;
count = count < count13 ? count : count13;
for (int i = 0; i < count; i++)
{
Console.Write("5,10,13,");
}
for (int i = count; i < count5; i++)
{
Console.Write("5,");
}
for (int i = count; i < count10; i++)
{
Console.Write("10,");
}
for (int i = count; i < count13; i++)
{
Console.Write("13,");
}
List<int> list = new List<int>();
int index = -1;
while (list.Count < arr.Length)
{
switch (list.Count % 3)
{
case 0:
index = Array.IndexOf(arr, 5);
break;
case 1:
index = Array.IndexOf(arr, 10);
break;
case 2:
index = Array.IndexOf(arr, 13);
break;
}
if (index != -1)
{
list.Add(arr[index]);
arr[index] = -1;
}
else
break;
}
int[] temp = Array.FindAll(arr, i => i != -1);
Array.Sort(temp);
list.AddRange(temp);
arr = list.ToArray();
foreach (int i in arr)
Console.WriteLine(i);
改为 int[] temp = Array.FindAll(arr, delegate(int i) { return i != -1; });
我有个方法供参考:
新建一数组A[3*B排列数组]和3个变量c5=0,c10=1,c13=2:内容为3个元素为1、2、3、1、2、3......;
然后循环B排列数组,如果5则A[c5]=5且c5+=3,如果10则A[c10]=10且c10+=3,
如果13则A[c13]=13且c13+=3,以此类推,循环数组次;
最后把1、2、3元素删掉就OK了或是输出时不显示1、2、3。
其实是一个扑克的小程序,可能大家都玩过五十K这种扑克玩法,两付牌四种
花色,黑桃、方块、梅花、红桃也就是每种花色两张牌,为方便编程用1代表
黑桃,105就代表黑桃5,110代表黑桃10,113代表黑桃K,以此类推
用2代表红桃,205就代表红桃5,210代表红桃10,213代表红桃K
3代表梅花,305,310,313分别代表梅花5,10,K
4代表方块,405,410,413分别代表方块5,10,K,
这样分到手的36张牌中有不同花色的5,10,K,
同色的放在一起,如都是红桃的5,10,K;如果没有同色的,可以组成5,10,K的
也放在一起,如红桃5,方块10,红桃K,剩下的按顺序排列。我的程序是这样的
for (int i = 0; i < 36; i++)
if (PokerA[theArray[i]].number % 100 == 5 || PokerA[theArray[i]].number % 100 == 10 || PokerA[theArray[i]].number % 100 == 13)
{
if (i != n)
{
p = theArray[i];
theArray[i] = theArray[n];
theArray[n] = p;
}
PokerA[theArray[n]].wsk = 1;
n++;
} //PokerA[theArray[i]].number % 100 == 5数组的值除5取余,这段是将36张牌中不同花色的5,10,K,放在0——n位。
for (int i = 0; i < n ; i++)
{
for (m = i, j = i + 1; j < n ; j++)
if (PokerA[theArray[m]].number > PokerA[theArray[j]].number) m = j;
if (m != i)
{
p = theArray[i];
theArray[i] = theArray[m];
theArray[m] = p; }
} //0--n位排序。这样排可以将同色的放在一起,但杂色的就不行了,我是想如果能组成同色的放在一起组成5,10,K,不成杂色的能组成5,10,K也放在一起
剩下的按顺序排。
105,110,210,113,413,313,105,205,205,405
排序后可以是这样
105,110,113,105,210,313,205,205,405,413
也就是一组同色黑桃5,10,K,一组杂色5,10,K,剩下三个5,一个K;