[ACM]Q10465: Homer Simpson

[ACM]Q10465: Homer Simpson


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

namespace Q10465
{
    class Program
    {
        static private int[] mnt; static private char[] par = { ' ' }; static private int temp, tmp;
        static void Main(string[] args)
        {
            for (; ; )
            {
            label: Console.Write("M N T>>>"); temp = 0;
                try
                {
                    mnt = Array.ConvertAll<string, int>(Console.ReadLine().Split(par, StringSplitOptions.RemoveEmptyEntries), int.Parse);
                    if (mnt.Length != 3) throw new Exception();
                }
                catch { goto label; }
                tmp = mnt[2];
                for (int i = 0; i <= mnt[2] / mnt[0]; i++)
                    for (int j = 0; j <= mnt[2] / mnt[1]; j++)
                    {
                        if (mnt[2] == i * mnt[0] + j * mnt[1])
                        {
                            if (i + j > temp) temp = i + j;
                            break;
                        }
                    }
                if (temp == 0)
                {
                    for (int i = 0; i <= mnt[2] / mnt[0]; i++)
                        for (int j = 0; j <= mnt[2] / mnt[1]; j++)
                        {
                            if (mnt[2] - i * mnt[0] - j * mnt[1] > 0 && mnt[2] - i * mnt[0] - j * mnt[1] < tmp)
                            {
                                if (i + j > temp) temp = i + j;
                            }
                            else if (mnt[2] - i * mnt[0] - j * mnt[1] < 0) break;
                        }
                }
                Console.WriteLine(temp);
            }
        }
    }
}