2015年3月24日 星期二

[2015][Homework]Team06 - Hw02

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("若 a, b 為整數, 求ax+by=c的整數解。");
            Console.WriteLine("請依序輸入a, b, c");
            int a = int.Parse(Console.ReadLine());
            int b = int.Parse(Console.ReadLine());
            int c = int.Parse(Console.ReadLine());
            int temp = 0;
            int GCDa = a;                            //複製a.b以輾轉相除法計算最大公因數
            int GCDb = b;

            while (GCDa != 0)
            {
                if (GCDb > GCDa)
                {
                    temp = GCDa;
                    GCDa = GCDb;
                    GCDb = temp;
                }
                GCDa = GCDa % GCDb;
            }

            Double GCD = GCDb;

            if ((c % GCD) != 0)
            {
                Console.WriteLine("\n方程式 {0}x+{1}y={2} 無解!", a, b, c);
            }
            else
            {
                Double AnsX = 0;
                Double AnsY = 0;
                Console.WriteLine("\n方程式 {0}x+{1}y={2}", a, b, c);
                Console.WriteLine("在0≦y≦a 範圍內的解有:\n");
                for (AnsY = 0; AnsY <= a; AnsY++)
                {
                    AnsX = ((c / GCD) - (AnsY * b / GCD)) / (a / GCD);
                    if ((AnsX % 1) == 0)
                    {
                        Console.WriteLine("x={0}  y={1}", AnsX, AnsY);
                    }
                }
            }

            Console.WriteLine("\n分水遊戲");
            Console.WriteLine("量筒A容量為5公升 量筒B容量為3公升");

            int currentAVolume = 0;                                         //設定A.B兩量筒初始水量為0
            int currentBVolume = 0;

            Console.WriteLine("\n量筒A:{0}公升 量筒B:{1}公升", currentAVolume, currentBVolume);
            Console.WriteLine("\n將量筒A裝滿");
            currentAVolume += 5;

            Console.WriteLine("\n量筒A:{0}公升 量筒B:{1}公升", currentAVolume, currentBVolume);
            Console.WriteLine("\n以量筒A將量筒B裝滿");
            currentBVolume += 3;
            currentAVolume -= currentBVolume;

            Console.WriteLine("\n量筒A:{0}公升 量筒B:{1}公升", currentAVolume, currentBVolume);
            Console.WriteLine("\n將量筒B倒掉 再把量筒A倒入量筒B");
            currentBVolume = 0;
            currentBVolume += currentAVolume;
            currentAVolume -= currentBVolume;

            Console.WriteLine("\n量筒A:{0}公升 量筒B:{1}公升", currentAVolume, currentBVolume);
            Console.WriteLine("\n將量筒A裝滿 再以量筒A將量筒B裝滿");
            currentAVolume = 5;
            currentBVolume += 1;
            currentAVolume -= 1;

            Console.WriteLine("\n量筒A:{0}公升 量筒B:{1}公升", currentAVolume, currentBVolume);
            Console.WriteLine("\n量筒A為4公升 得到所需之水量");
            Console.ReadKey();
        }
    }
}

3 則留言:

  1. input = Console.ReadLine();
    b = int.Parse(input);
    改為
    b= int.Parse(Console.ReadLine());

    回覆刪除
  2. GCDa ==> gcdA
    AnsX ==> ansA
    what is GCA, GCB?

    回覆刪除
  3. 已做完修正!

    通用版分水遊戲請看作業04

    回覆刪除