欢乐暑假线上编程比赛第四题:分配糖果

在csdn上看到这么个题目,与友友们一起分享下,如果有别的做法,也希望能拿出来交流交流。

题目详情

有n个小朋友站成一排(编号从0到n-1),每个小朋友有一个rating值,存放在ratings数组中。老师需要给他们分

配糖果,每个小朋友至少需要一颗糖果,对于任意相邻的两个小朋友i和i+1,rating值大的必须比rating值小的分

配的糖果多(rating相同的没必要分配一样多的糖果)。

请计算最少需要多少颗糖果,才能完成上述分配。

输入格式:

多组数据,每组数据第一行是一个正整数n。

接下来n行,每行有1个正整数,表示每个小朋友的rating值。所有整数都不超过100000。

输出格式:

每组数据一行,包括一个正整数,表示做少需要的糖果数。

 

在下的解决方案:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 分配糖果
{
    class Program
    {
        static void Main(string[] args)
        {
            Run();
        }

        static int ConvertToInt(string str)
        {
            try
            {
                int number=Convert.ToInt32(str);
                if(number>100000)
                {
                    Console.WriteLine("输入的数字超出要求!");
                    Message();
                }
                return number;
            }
            catch
            {
                Console.WriteLine("输入字符格式错误!");
                Message();
            }
            return -1;
        }

        static void Run()
        {
            Console.Clear();
            Console.Write("请输入小朋友个数:");
            int n = ConvertToInt(Console.ReadLine());
            int[] ratings = new int[n];
            for (int i = 0; i < n; i++)
            {
                Console.Write(string.Format("请输入第{0}个小朋友的rating:", i + 1));
                ratings[i] = ConvertToInt(Console.ReadLine());
            }
            if (n < 0)
            {
                Console.WriteLine("您输入的数不符合实际情况!");
            }
            else if (n == 0)
            {
                Console.WriteLine(string.Format("需要{0}个糖果1", 0));
            }
            else if (n == 1)
            {
                Console.WriteLine(string.Format("需要{0}个糖果2", 1));
            }
            else
            {
                int[] candys = new int[n];
                for (int i = 0; i < n; i++)
                {
                    Dictionary<int, int> dic = new Dictionary<int, int>();
                    for (int j = 0; j < n; j++)
                    {
                        if (candys[j] == 0)
                        {
                            if (dic.Count() <= 0)
                            {
                                dic.Add(j, ratings[j]);
                            }
                            else if (ratings[j] == dic.Values.First())
                            {
                                dic.Add(j, ratings[j]);
                            }
                            else if (ratings[j] < dic.Values.First())
                            {
                                dic.Clear();
                                dic.Add(j, ratings[j]);
                            }
                        }
                    }
                    if (dic.Count() > 0)
                    {
                        foreach (var d in dic)
                        {
                            if (d.Key == 0)
                            {
                                candys[d.Key] = candys[d.Key + 1] + 1;
                            }
                            else if (d.Key == n - 1)
                            {
                                if (d.Value == ratings[d.Key-1])
                                {
                                    candys[d.Key] = 1;
                                }
                                else
                                {
                                    candys[d.Key] = candys[d.Key - 1] + 1;
                                }
                            }
                            else
                            {
                                if (candys[d.Key - 1] > candys[d.Key + 1])
                                {
                                    if (d.Value == ratings[d.Key-1])
                                    {
                                        candys[d.Key] = candys[d.Key + 1] + 1;
                                    }
                                    else
                                    {
                                        candys[d.Key] = candys[d.Key - 1] + 1;
                                    }
                                }
                                else
                                {
                                    if (d.Value == ratings[d.Key + 1])
                                    {
                                        candys[d.Key] = candys[d.Key - 1] + 1;
                                    }
                                    else
                                    {
                                        candys[d.Key] = candys[d.Key + 1] + 1;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                Console.WriteLine();
                Console.WriteLine(string.Format("需要{0}个糖果3", candys.Sum()));
            }
            Message();
        }

        static void Message()
        {
            Console.Write("输入1重新游戏,输入其他任意键退出:");
            string key = Console.ReadLine();
            if (key == "1")
            {
                Run();
            }
            else
            {
                Environment.Exit(0);
            }
        }
    }
}

 

posted on 2014-08-24 18:18  苦B程序员  阅读(339)  评论(0编辑  收藏  举报