求 一个C# 面试 题目的解。

0

1.随机生成20组数（无重复的组，{1,2,3,4,5} 和 {5,4,3,2,1}视为重复的组）

2.将这20组数 按照 能组成最大值顺序组成5位数排序(增序)取前5组.

qwert321 | 初学一级 | 园豆：152

0

public List<int[]> getTheFinalArrayList()
{
List<int[]> list=new ArrayList<int[]>();
System.out.println(list);
int count=0;
while(true){
int[] myArray=getFiveRandomInt();
if(list.size()==0){
count++;
System.out.println(count);
}else{
for(int i=0;i<list.size();i++){
if(isEqual(list.get(i),myArray)){
continue;
}else{
count++;
System.out.println(count);
if(count==20){
return list;
}
}
}
}
}
}

int[] getFiveRandomInt()
{
Random random=new Random();
int one=random.nextInt(9);
int two=random.nextInt(9);
int three=random.nextInt(9);
int four=random.nextInt(9);
int five=random.nextInt(9);
int[] mArray=new int[5];
mArray[0]=one;
mArray[1]=two;
mArray[2]=three;
mArray[3]=four;
mArray[4]=five;
return mArray;
}

boolean isEqual(int[] a,int[] b){
if(a.length!=b.length)return false;
Arrays.sort(a);
Arrays.sort(b);
for(int i=0;i<a.length;i++){
if(a[i]!=b[i])return false;
}
return true;
}

ruoyu4game | 园豆：342 (菜鸟二级) | 2013-07-18 01:07
0
```using System;
using System.Collections.Generic;
using System.Linq;

namespace MyConsoleApp
{
public class Program
{
public static void Main(string[] args)
{
List<Group> gs = Create20Groups();
foreach (var item in TopGroups(gs, 5))
{
Console.WriteLine(item.GetRankedString());
}
}

public static List<Group> Create20Groups()
{
List<Group> groups = new List<Group>();
Group g;
while (groups.Count < 20)
{
g = new Group();
if (!groups.Contains(g))
}
return groups;
}

public static List<Group> TopGroups(List<Group> list,int top)
{
return list.OrderByDescending(g => g).Take(top).ToList();
}
}

public class Group : IComparable<Group>,IEquatable<Group>
{
private List<int> group;
public Group()
{
group = new List<int>();
SetRandom();
}

private void SetRandom()
{
Random r = new Random();
for (int i = 0; i < 5; i++)
{
}
}

public override string ToString()
{
string val = "";
for (int i = 0; i < group.Count; i++)
{
val += "," + group[i];
}
return String.Format("Group-->"+val.Substring(1));
}

public string GetRankedString()
{
List<int> list = group.OrderByDescending(i => i).ToList();
string val = "";
for (int i = 0; i < list.Count; i++)
{
val += "," + list[i];
}
return String.Format("Group DESC-->" + val.Substring(1));
}

public int CompareTo(Group other)
{
List<int> l1 = group.OrderByDescending(i => i).ToList();
List<int> l2 = other.group.OrderByDescending(i => i).ToList();
int len = l1.Count;
int result = 0;
for (int i = 0; i < len; i++)
{
if (l1[i] != l2[i])
return l1[i].CompareTo(l2[i]);
}
return result;
}

public bool Equals(Group other)
{
return this.CompareTo(other)==0;
}
}
}```
TiestoRay | 园豆：687 (小虾三级) | 2013-07-18 11:12
0
```  1 class Class1
2     {
3         static void Main(string[] args)
4         {
5             //先定义一个list准备保存所有的数组
6             List<int[]> listarr = new List<int[]>();
7             //循环把一个个数组添加到list中
8             for (int i = 0; i < 20; i++)
9             {
10                 //调用RandomArray方法随机产生一个数组
11                 int[] a = RandomArray();
12                 bool b = false;
13                 //用先排序，再把数组里边的每一项拼接起来的方法判断是否已存在重复的组
14                 Array.Sort(a);
15                 for (int j = 0; j < listarr.Count; j++)
16                 {
17                     StringBuilder sb = new StringBuilder();
18                     StringBuilder sb2 = new StringBuilder();
19                     for (int k = 0; k < 5; k++)
20                     {
21                         sb.Append(listarr[j][k].ToString());
22                         sb2.Append(a[k].ToString());
23                     }
24                     //比较拼接后的a和listarr中的第j项的拼接是否相同，相同返回true
25                     b = sb.Equals(sb2);
26
27                 }
28                 //如果不相等，就把数组a加入到listarr中
29                 if (!b)
30                 {
32                 }
33                 //如果相等为了还保证循环20次，那么i要减1
34                 else
35                 {
36                     i--;
37                 }
38             }
39             //两个foreach循环输出到console
40             foreach (int[] item in listarr)
41             {
42                 foreach (int item1 in item)
43                 {
44                     Console.Write(item1);
45                 }
46                 Console.WriteLine();
47             }
49             Console.WriteLine("第二问");
50             //第二问
51             //定义一个保存新的数字化的各个数组
52             List<int> li = new List<int>();
53             foreach (int[] item in listarr)
54             {
55                 StringBuilder sb = new StringBuilder();
56                 foreach (int item1 in item)
57                 {
58                     sb.Append(item1);
59                 }
60                 //把每一个数组都组装成一个一个数字了
61                 int ii = Convert.ToInt32(sb.ToString());
62                 //把数字加到li中
64             }
65             //将li排序
66             li.Sort();
67             //反转
68             li.Reverse();
69             //新的list保存最后结果——5个数组
70             List<int[]> larr1 = new List<int[]>();
71             //因为要五个循环5次
72             for (int i = 0; i < 5; i++)
73             {
74                 //开始把数字拆成数组
75                 int[] ar = new int[5];
76                 ar[0] = li[i] / 10000 % 10;
77                 ar[1] = li[i] / 1000 % 10;
78                 ar[2] = li[i] / 100 % 10;
79                 ar[3] = li[i] / 10 % 10;
80                 ar[4] = li[i]  % 10;
81                 //把数组加到最终的list中
83             }
84             //两个foreach循环输出到console
85             foreach (int[] item in larr1)
86             {
87                 foreach (int item1 in item)
88                 {
89                     Console.Write(item1);
90                 }
91                 Console.WriteLine();
92             }
94
95         }
96         /// <summary>
97         /// 产生随机数组的方法
98         /// </summary>
99         /// <returns>一个包含5个0-9随机值得数组</returns>
100         static int[] RandomArray()
101         {
102             Random rd = new Random();
103             int[] arr = new int[5];
104             for (int i = 0; i < 5; i++)
105             {
106                 arr[i] = rd.Next(0, 9);
107             }
108             return arr;
109         }
```

Song Yuan | 园豆：219 (菜鸟二级) | 2013-07-18 12:00
0

Qlin | 园豆：2403 (老鸟四级) | 2013-07-18 18:05

您需要登录以后才能回答，未注册用户请先注册