## 豆豆与黄金剑, 一道典型DP题的C#解法

### Sample Input

Sample Input #1Sample Input #231 33 22 4510 19 27 36 45 5


### Sample Output

Sample Output #1Sample Output #221

C#代码如下:

1    public class QQ
2    {
3        public int Height getset; }
4        public int ATK getset; }
5    }

6    class Program
7    {
8        static void Main(string[] args)
9        {
10            int n, h, atk;
11            Console.WriteLine("input the population n(1<=n<= 1000):");
12            while ((n = int.Parse(Console.ReadLine())) > 1000 || n < 1) ;
13            List<QQ> population = new List<QQ>(n+1);
14
15            Console.WriteLine("input the Height and ATK properties,split by one space:");
16            for (int i = 0; i < n; i++)
17            {
18                string[] property = Console.ReadLine().Split(new Char[] ' ' });
19                if ((h = int.Parse(property[0])) < 1000000 && h > -1 &&
20                    (atk = int.Parse(property[1])) < 1000000 && atk > -1)
21                    population.Add(new QQ { Height=h,ATK=atk});
22            }

23
24            population.Sort((a, b) =>
25            {
26                if (a.Height != b.Height)
27                    return a.Height - b.Height;
28                else if (a.ATK != b.ATK)
29                    return a.ATK - b.ATK;
30                else
31                    return 0;
32            }
);
33
34            int[] tb = new int[n];
35            for (int i = 0; i < n; i++)
36            {
37                tb[i] = 1;
38            }

39
40            for (int i = 0; i < n; i++)
41            {
42                for (int j = 0; j < i; j++)
43                {
44                    if (population[i].Height > population[j].Height &&
45                        population[i].ATK > population[j].ATK && tb[i] < tb[j] + 1)
46                    {
47                        tb[i] = tb[j] + 1;
48                    }

49                }

50            }

51            Console.WriteLine("the answer is: "+tb.Max().ToString());
52        }

53    }

PS:还是可以更健壮更优化的,欢迎交流 ;-)

posted on 2009-07-25 20:01  J.D Huang  阅读(899)  评论(0编辑  收藏  举报