我写的找重复数和过桥问题.
我写的找重复数和过桥问题.欢迎拍砖
昨天看到莫同志的两个算法题.自己写了俩程序跟帖回过去.没有人理睬我.于是今天就斗胆发首页,等拍砖.
第一个找重复数的题目,看题意我就想到了递归.于是下面的代码...
第二个问题.
很多人已经分析过了,就直接上代码吧
几个人过河都可以.
拍砖吧,谢谢.
第一个找重复数的题目,看题意我就想到了递归.于是下面的代码...
Code
1static void Main(string[] args)
2{
3//定义数组
4int[] list = new int[1001];
5Random random = new Random();
6for (int i = 1; i < 1001; i++)
7{
8list[i - 1] = i;
9}
10list[1000] = random.Next(1, 1000);
11
12Console.WriteLine(add(1, list) - (1 + 1000) * 500);
13Console.Read();
14
15}
16//递归算法
17public static int add(int i,int[] list)
18{
19if (i < list.Length)
20{
21return list[i - 1] + add(++i, list);
22}
23else
24return list[i - 1];
25}
应该符合题意吧.1static void Main(string[] args)
2{
3//定义数组
4int[] list = new int[1001];
5Random random = new Random();
6for (int i = 1; i < 1001; i++)
7{
8list[i - 1] = i;
9}
10list[1000] = random.Next(1, 1000);
11
12Console.WriteLine(add(1, list) - (1 + 1000) * 500);
13Console.Read();
14
15}
16//递归算法
17public static int add(int i,int[] list)
18{
19if (i < list.Length)
20{
21return list[i - 1] + add(++i, list);
22}
23else
24return list[i - 1];
25}
第二个问题.
很多人已经分析过了,就直接上代码吧
Code
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 List<Woman> w = new List<Woman>();
6 w.Add(new Woman { Min = 1, Where = 0, WID = 1 });
7 w.Add(new Woman { Min = 2, Where = 0, WID = 2 });
8 w.Add(new Woman { Min = 5, Where = 0, WID = 3 });
9 w.Add(new Woman { Min = 10, Where = 0, WID = 4 });
10 w.Add(new Woman { Min = 15, Where = 0, WID = 5 });
11 w.Add(new Woman { Min = 5, Where = 0, WID = 6 });
12 WomenGapBridge womenGapBridge =
13 new WomenGapBridge(w);
14 womenGapBridge.GapBridge();
15 Console.ReadLine();
16 }
17 }
18
19 class Woman {
20 public int WID { set; get; }
21 public int Min { set; get; }
22 public byte Where { set; get; }
23 }
24
25 class WomenGapBridge {
26 private int num = 0;
27 private int gonum = 1;
28 private int UseTime = 0;
29 List<Woman> Women;
30
31 public WomenGapBridge(List<Woman> w)
32 {
33 this.Women = w;
34 num = w.Count;
35 foreach (var n in w)
36 {
37 Console.WriteLine(string.Format("No.{0},过桥需要{1}分钟", n.WID, n.Min));
38 }
39 }
40
41 public void GapBridge()
42 {
43 while(Women.Exists(s=>s.Where==0))
44 {
45 Go();
46 }
47 Console.WriteLine(string.Format("总用时{0}分", UseTime));
48 }
49
50 public void Go()
51 {
52 IEnumerable<Woman> n1;
53
54
55 if (gonum % 2 == 0)
56 {
57 n1 = (from s in Women
58 where s.Where == 0
59 orderby s.Min descending
60 select s).Take(2);
61
62 }
63 else
64 {
65 n1 = (from s in Women
66 where s.Where == 0
67 orderby s.Min ascending
68 select s).Take(2);
69 }
70 int maxTime = 0;
71
72 foreach (var n in n1)
73 {
74 if (n.Min > maxTime)
75 maxTime = n.Min;
76 Console.WriteLine(string.Format("No.{0} 过河", n.WID));
77 n.Where = 1;
78 }
79 Console.WriteLine(string.Format("用时:{0}分",maxTime));
80 UseTime += maxTime;
81 gonum++;
82 if (gonum < (num))
83 {
84 Back();
85 }
86 }
87
88 public void Back()
89 {
90 var back = (from s in Women
91 where s.Where == 1
92 orderby s.Min ascending
93 select s).Take(1).First<Woman>();
94 back.Where = 0;
95 UseTime += back.Min;
96 Console.WriteLine(string.Format("No.{0} 回去", back.WID));
97 Console.WriteLine(string.Format("用时:{0}分", back.Min));
98
99 }
100 }
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 List<Woman> w = new List<Woman>();
6 w.Add(new Woman { Min = 1, Where = 0, WID = 1 });
7 w.Add(new Woman { Min = 2, Where = 0, WID = 2 });
8 w.Add(new Woman { Min = 5, Where = 0, WID = 3 });
9 w.Add(new Woman { Min = 10, Where = 0, WID = 4 });
10 w.Add(new Woman { Min = 15, Where = 0, WID = 5 });
11 w.Add(new Woman { Min = 5, Where = 0, WID = 6 });
12 WomenGapBridge womenGapBridge =
13 new WomenGapBridge(w);
14 womenGapBridge.GapBridge();
15 Console.ReadLine();
16 }
17 }
18
19 class Woman {
20 public int WID { set; get; }
21 public int Min { set; get; }
22 public byte Where { set; get; }
23 }
24
25 class WomenGapBridge {
26 private int num = 0;
27 private int gonum = 1;
28 private int UseTime = 0;
29 List<Woman> Women;
30
31 public WomenGapBridge(List<Woman> w)
32 {
33 this.Women = w;
34 num = w.Count;
35 foreach (var n in w)
36 {
37 Console.WriteLine(string.Format("No.{0},过桥需要{1}分钟", n.WID, n.Min));
38 }
39 }
40
41 public void GapBridge()
42 {
43 while(Women.Exists(s=>s.Where==0))
44 {
45 Go();
46 }
47 Console.WriteLine(string.Format("总用时{0}分", UseTime));
48 }
49
50 public void Go()
51 {
52 IEnumerable<Woman> n1;
53
54
55 if (gonum % 2 == 0)
56 {
57 n1 = (from s in Women
58 where s.Where == 0
59 orderby s.Min descending
60 select s).Take(2);
61
62 }
63 else
64 {
65 n1 = (from s in Women
66 where s.Where == 0
67 orderby s.Min ascending
68 select s).Take(2);
69 }
70 int maxTime = 0;
71
72 foreach (var n in n1)
73 {
74 if (n.Min > maxTime)
75 maxTime = n.Min;
76 Console.WriteLine(string.Format("No.{0} 过河", n.WID));
77 n.Where = 1;
78 }
79 Console.WriteLine(string.Format("用时:{0}分",maxTime));
80 UseTime += maxTime;
81 gonum++;
82 if (gonum < (num))
83 {
84 Back();
85 }
86 }
87
88 public void Back()
89 {
90 var back = (from s in Women
91 where s.Where == 1
92 orderby s.Min ascending
93 select s).Take(1).First<Woman>();
94 back.Where = 0;
95 UseTime += back.Min;
96 Console.WriteLine(string.Format("No.{0} 回去", back.WID));
97 Console.WriteLine(string.Format("用时:{0}分", back.Min));
98
99 }
100 }
几个人过河都可以.
拍砖吧,谢谢.