昨天看到莫同志的两个算法题.自己写了俩程序跟帖回过去.没有人理睬我.于是今天就斗胆发首页,等拍砖.
第一个找重复数的题目,看题意我就想到了递归.于是下面的代码...

1 static void Main(string[] args)
static void Main(string[] args)
2 {
{
3 //定义数组
//定义数组
4 int[] list = new int[1001];
int[] list = new int[1001];
5 Random random = new Random();
Random random = new Random();
6 for (int i = 1; i < 1001; i++)
for (int i = 1; i < 1001; i++)
7 {
{
8 list[i - 1] = i;
list[i - 1] = i;
9 }
}
10 list[1000] = random.Next(1, 1000);
list[1000] = random.Next(1, 1000);
11
12 Console.WriteLine(add(1, list) - (1 + 1000) * 500);
Console.WriteLine(add(1, list) - (1 + 1000) * 500);
13 Console.Read();
Console.Read();
14
15 }
}
16 //递归算法
//递归算法
17 public static int add(int i,int[] list)
public static int add(int i,int[] list)
18 {
{
19 if (i < list.Length)
if (i < list.Length)
20 {
{
21 return list[i - 1] + add(++i, list);
return list[i - 1] + add(++i, list);
22 }
}
23 else
else
24 return list[i - 1];
return list[i - 1];
25 }应该符合题意吧.
}应该符合题意吧.
第二个问题.
很多人已经分析过了,就直接上代码吧

1 class Program
    class Program
2 {
    {
3 static void Main(string[] args)
        static void Main(string[] args)
4 {
        {
5 List<Woman> w = new List<Woman>();
            List<Woman> w = new List<Woman>();
6 w.Add(new Woman { Min = 1, Where = 0, WID = 1 });
            w.Add(new Woman { Min = 1, Where = 0, WID = 1 });
7 w.Add(new Woman { Min = 2, Where = 0, WID = 2 });
            w.Add(new Woman { Min = 2, Where = 0, WID = 2 });
8 w.Add(new Woman { Min = 5, Where = 0, WID = 3 });
            w.Add(new Woman { Min = 5, Where = 0, WID = 3 });
9 w.Add(new Woman { Min = 10, Where = 0, WID = 4 });
            w.Add(new Woman { Min = 10, Where = 0, WID = 4 });
10 w.Add(new Woman { Min = 15, Where = 0, WID = 5 });
            w.Add(new Woman { Min = 15, Where = 0, WID = 5 });
11 w.Add(new Woman { Min = 5, Where = 0, WID = 6 });
            w.Add(new Woman { Min = 5, Where = 0, WID = 6 });
12 WomenGapBridge womenGapBridge =
            WomenGapBridge womenGapBridge =
13 new WomenGapBridge(w);
                new WomenGapBridge(w);
14 womenGapBridge.GapBridge();
            womenGapBridge.GapBridge();
15 Console.ReadLine();
            Console.ReadLine();
16 }
        }
17 }
    }
18
19 class Woman {
    class Woman {
20 public int WID { set; get; }
        public int WID { set; get; }
21 public int Min { set; get; }
        public int Min { set; get; }
22 public byte Where { set; get; }
        public byte Where { set; get; }
23 }
    }
24
25 class WomenGapBridge {
    class WomenGapBridge {
26 private int num = 0;
        private int num = 0;
27 private int gonum = 1;
        private int gonum = 1;
28 private int UseTime = 0;
        private int UseTime = 0;
29 List<Woman> Women;
        List<Woman> Women;
30
31 public WomenGapBridge(List<Woman> w)
        public WomenGapBridge(List<Woman> w)
32 {
        {
33 this.Women = w;
            this.Women = w;
34 num = w.Count;
            num = w.Count;
35 foreach (var n in w)
            foreach (var n in w)
36 {
            {
37 Console.WriteLine(string.Format("No.{0},过桥需要{1}分钟", n.WID, n.Min));
                Console.WriteLine(string.Format("No.{0},过桥需要{1}分钟", n.WID, n.Min));
38 }
            }
39 }
        }
40
41 public void GapBridge()
        public void GapBridge()
42 {
        { 
43 while(Women.Exists(s=>s.Where==0))
            while(Women.Exists(s=>s.Where==0))
44 {
            {
45 Go();
                Go();
46 }
            }
47 Console.WriteLine(string.Format("总用时{0}分", UseTime));
            Console.WriteLine(string.Format("总用时{0}分", UseTime));
48 }
        }
49
50 public void Go()
        public void Go()
51 {
        {
52 IEnumerable<Woman> n1;
            IEnumerable<Woman> n1;
53
54
55 if (gonum % 2 == 0)
            if (gonum % 2 == 0)
56 {
            {
57 n1 = (from s in Women
                n1 = (from s in Women
58 where s.Where == 0
                          where s.Where == 0
59 orderby s.Min descending
                          orderby s.Min descending
60 select s).Take(2);
                          select s).Take(2);
61 
                
62 }
            }
63 else
            else
64 {
            {
65 n1 = (from s in Women
                n1 = (from s in Women
66 where s.Where == 0
                          where s.Where == 0
67 orderby s.Min ascending
                          orderby s.Min ascending
68 select s).Take(2);
                          select s).Take(2);
69 }
            }
70 int maxTime = 0;
            int maxTime = 0;
71
72 foreach (var n in n1)
            foreach (var n in n1)
73 {
            {
74 if (n.Min > maxTime)
                if (n.Min > maxTime) 
75 maxTime = n.Min;
                    maxTime = n.Min;
76 Console.WriteLine(string.Format("No.{0} 过河", n.WID));
                Console.WriteLine(string.Format("No.{0} 过河", n.WID));
77 n.Where = 1;
                n.Where = 1;
78 }
            }
79 Console.WriteLine(string.Format("用时:{0}分",maxTime));
            Console.WriteLine(string.Format("用时:{0}分",maxTime));
80 UseTime += maxTime;
            UseTime += maxTime;
81 gonum++;
            gonum++;
82 if (gonum < (num))
            if (gonum < (num))
83 {
            { 
84 Back();
                Back();
85 }
            }
86 }
        }
87
88 public void Back()
        public void Back()
89 {
        {
90 var back = (from s in Women
            var back = (from s in Women
91 where s.Where == 1
                        where s.Where == 1
92 orderby s.Min ascending
                        orderby s.Min ascending
93 select s).Take(1).First<Woman>();
                        select s).Take(1).First<Woman>();
94 back.Where = 0;
            back.Where = 0;
95 UseTime += back.Min;
            UseTime += back.Min;
96 Console.WriteLine(string.Format("No.{0} 回去", back.WID));
            Console.WriteLine(string.Format("No.{0} 回去", back.WID));
97 Console.WriteLine(string.Format("用时:{0}分", back.Min));
            Console.WriteLine(string.Format("用时:{0}分", back.Min));
98
99 }
        }
100 }
    }
			
			
			
			        第一个找重复数的题目,看题意我就想到了递归.于是下面的代码...

1
 static void Main(string[] args)
static void Main(string[] args)2
 {
{3
 //定义数组
//定义数组4
 int[] list = new int[1001];
int[] list = new int[1001];5
 Random random = new Random();
Random random = new Random();6
 for (int i = 1; i < 1001; i++)
for (int i = 1; i < 1001; i++)7
 {
{8
 list[i - 1] = i;
list[i - 1] = i;9
 }
}10
 list[1000] = random.Next(1, 1000);
list[1000] = random.Next(1, 1000);11

12
 Console.WriteLine(add(1, list) - (1 + 1000) * 500);
Console.WriteLine(add(1, list) - (1 + 1000) * 500);13
 Console.Read();
Console.Read();14

15
 }
}16
 //递归算法
//递归算法17
 public static int add(int i,int[] list)
public static int add(int i,int[] list)18
 {
{19
 if (i < list.Length)
if (i < list.Length)20
 {
{21
 return list[i - 1] + add(++i, list);
return list[i - 1] + add(++i, list);22
 }
}23
 else
else24
 return list[i - 1];
return list[i - 1];25
 }
}第二个问题.
很多人已经分析过了,就直接上代码吧

1
 class Program
    class Program2
 {
    {3
 static void Main(string[] args)
        static void Main(string[] args)4
 {
        {5
 List<Woman> w = new List<Woman>();
            List<Woman> w = new List<Woman>();6
 w.Add(new Woman { Min = 1, Where = 0, WID = 1 });
            w.Add(new Woman { Min = 1, Where = 0, WID = 1 });7
 w.Add(new Woman { Min = 2, Where = 0, WID = 2 });
            w.Add(new Woman { Min = 2, Where = 0, WID = 2 });8
 w.Add(new Woman { Min = 5, Where = 0, WID = 3 });
            w.Add(new Woman { Min = 5, Where = 0, WID = 3 });9
 w.Add(new Woman { Min = 10, Where = 0, WID = 4 });
            w.Add(new Woman { Min = 10, Where = 0, WID = 4 });10
 w.Add(new Woman { Min = 15, Where = 0, WID = 5 });
            w.Add(new Woman { Min = 15, Where = 0, WID = 5 });11
 w.Add(new Woman { Min = 5, Where = 0, WID = 6 });
            w.Add(new Woman { Min = 5, Where = 0, WID = 6 });12
 WomenGapBridge womenGapBridge =
            WomenGapBridge womenGapBridge =13
 new WomenGapBridge(w);
                new WomenGapBridge(w);14
 womenGapBridge.GapBridge();
            womenGapBridge.GapBridge();15
 Console.ReadLine();
            Console.ReadLine();16
 }
        }17
 }
    }18

19
 class Woman {
    class Woman {20
 public int WID { set; get; }
        public int WID { set; get; }21
 public int Min { set; get; }
        public int Min { set; get; }22
 public byte Where { set; get; }
        public byte Where { set; get; }23
 }
    }24

25
 class WomenGapBridge {
    class WomenGapBridge {26
 private int num = 0;
        private int num = 0;27
 private int gonum = 1;
        private int gonum = 1;28
 private int UseTime = 0;
        private int UseTime = 0;29
 List<Woman> Women;
        List<Woman> Women;30

31
 public WomenGapBridge(List<Woman> w)
        public WomenGapBridge(List<Woman> w)32
 {
        {33
 this.Women = w;
            this.Women = w;34
 num = w.Count;
            num = w.Count;35
 foreach (var n in w)
            foreach (var n in w)36
 {
            {37
 Console.WriteLine(string.Format("No.{0},过桥需要{1}分钟", n.WID, n.Min));
                Console.WriteLine(string.Format("No.{0},过桥需要{1}分钟", n.WID, n.Min));38
 }
            }39
 }
        }40

41
 public void GapBridge()
        public void GapBridge()42
 {
        { 43
 while(Women.Exists(s=>s.Where==0))
            while(Women.Exists(s=>s.Where==0))44
 {
            {45
 Go();
                Go();46
 }
            }47
 Console.WriteLine(string.Format("总用时{0}分", UseTime));
            Console.WriteLine(string.Format("总用时{0}分", UseTime));48
 }
        }49

50
 public void Go()
        public void Go()51
 {
        {52
 IEnumerable<Woman> n1;
            IEnumerable<Woman> n1;53

54

55
 if (gonum % 2 == 0)
            if (gonum % 2 == 0)56
 {
            {57
 n1 = (from s in Women
                n1 = (from s in Women58
 where s.Where == 0
                          where s.Where == 059
 orderby s.Min descending
                          orderby s.Min descending60
 select s).Take(2);
                          select s).Take(2);61
 
                62
 }
            }63
 else
            else64
 {
            {65
 n1 = (from s in Women
                n1 = (from s in Women66
 where s.Where == 0
                          where s.Where == 067
 orderby s.Min ascending
                          orderby s.Min ascending68
 select s).Take(2);
                          select s).Take(2);69
 }
            }70
 int maxTime = 0;
            int maxTime = 0;71

72
 foreach (var n in n1)
            foreach (var n in n1)73
 {
            {74
 if (n.Min > maxTime)
                if (n.Min > maxTime) 75
 maxTime = n.Min;
                    maxTime = n.Min;76
 Console.WriteLine(string.Format("No.{0} 过河", n.WID));
                Console.WriteLine(string.Format("No.{0} 过河", n.WID));77
 n.Where = 1;
                n.Where = 1;78
 }
            }79
 Console.WriteLine(string.Format("用时:{0}分",maxTime));
            Console.WriteLine(string.Format("用时:{0}分",maxTime));80
 UseTime += maxTime;
            UseTime += maxTime;81
 gonum++;
            gonum++;82
 if (gonum < (num))
            if (gonum < (num))83
 {
            { 84
 Back();
                Back();85
 }
            }86
 }
        }87

88
 public void Back()
        public void Back()89
 {
        {90
 var back = (from s in Women
            var back = (from s in Women91
 where s.Where == 1
                        where s.Where == 192
 orderby s.Min ascending
                        orderby s.Min ascending93
 select s).Take(1).First<Woman>();
                        select s).Take(1).First<Woman>();94
 back.Where = 0;
            back.Where = 0;95
 UseTime += back.Min;
            UseTime += back.Min;96
 Console.WriteLine(string.Format("No.{0} 回去", back.WID));
            Console.WriteLine(string.Format("No.{0} 回去", back.WID));97
 Console.WriteLine(string.Format("用时:{0}分", back.Min));
            Console.WriteLine(string.Format("用时:{0}分", back.Min));98

99
 }
        }100
 }
    }