2015年3月22日 星期日

[2015][Homework]Team04 - Hw02

HW02_1



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

namespace _2015OOP_HW2_1
{
    class HW2_1
    {
        static void Main(string[] args)
        {
            int a;
            int a1;
            int b;
            int b1;
            int c;
            int c1;
            int x0;
            int y0;
            int gcd;
            int count=0;
            Console.WriteLine("a、b為整數,求ax+by=c的整數解。");
            Console.WriteLine("please enter a:");
            a = int.Parse(Console.ReadLine());
            Console.WriteLine("please enter b:");
            b = int.Parse(Console.ReadLine());
            Console.WriteLine("please enter c:");
            c = int.Parse(Console.ReadLine());
            Console.WriteLine("======================");
            gcd=HW2_1.gcd(a, b);

            if (c % gcd == 0)
            {
                //a'用a1代替(b.c亦同)。
                a1 = a / gcd;
                b1 = b / gcd;
                c1 = c / gcd;
                Console.WriteLine("{0}x+{1}y={2}", a, b, c);
                Console.WriteLine("在0<=y<={0}的範圍內, 尋找 x 的整數解", a);
              
                for (int i = 0; i <= a; i++)
                {
                    if (count < 1) //通解只需要一個所以用個count,如果超過一個就跳出。                
                    {
                        if ((c1 - (b1 * i)) % a1 == 0)
                        {
                            x0 = (c1 - (b1 * i)) / a1; //算出X0的公式 
                            y0 = (c1 - a1 * x0) / b1;  //算出Y0的公式 
                            Console.WriteLine("通解:");
                            Console.WriteLine("x={0}+{1}t", x0, b1);
                            Console.WriteLine("y={0}-{1}t", y0, a1);
                            count++;
                        }
                    }
                    else
                        break;                 
                }               
            }
            else 
            {
                Console.WriteLine("無解");
            }           
            Console.ReadKey();
        }

        static int gcd(int m, int n)
        {
            int tempt =0 ;
            while (n != 0)
            {
                tempt = m % n;
                m = n;
                n = tempt;
            }
            return m;
        }

    }
}


HW02-2



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

namespace _2015OOP_HW02_2
{
    class Program
    {
        static void Main(string[] args)
        {
            int input;
            int Cylinder3L;
            int Cylinder5L;
            int WaterIn3L=0;
            int WaterIn5L=0;
            Console.WriteLine("分水遊戲!");
            Console.WriteLine("請輸入<-2,-1,0,1,2>其中一個數");
            input=int.Parse(Console.ReadLine());
            
            //通解x=-2+5t,y=2-3t;
            Cylinder3L = -2 + 5 * input;//cylinder3l=X
            Cylinder5L = 2 - 3 * input;//cylinder5L=Y
            Console.WriteLine("===========");
            while (Cylinder3L != 0 || Cylinder5L != 0)
            {
                if (Cylinder3L > Cylinder5L)// input>1的算法
                {                 
                    Console.WriteLine("量筒A裝滿水");
                    WaterIn3L = 3;
                    Cylinder3L--;
                    Console.WriteLine("A:({0}) B:({1})", WaterIn3L, WaterIn5L);
                    //將A燒杯的水倒到B燒杯
                    if (5 - WaterIn5L > 3)
                    {
                        WaterIn5L = WaterIn5L + WaterIn3L;
                        WaterIn3L = 0;
                        Console.WriteLine("量筒A==>量筒B");
                        Console.WriteLine("A:({0}) B:({1})", WaterIn3L, WaterIn5L);
                    }
                    else
                    {                       
                        WaterIn3L = WaterIn3L - (5 - WaterIn5L);
                        WaterIn5L = 5;
                        Console.WriteLine("量筒A==>量筒B");
                        Console.WriteLine("A:({0}) B:({1})", WaterIn3L, WaterIn5L);
                    }
                    //B燒杯滿了,把水倒掉
                    if (WaterIn5L == 5)
                    {
                        Console.WriteLine("量筒B把水倒掉");
                        WaterIn5L = 0;
                        Console.WriteLine("A:({0}) B:({1})", WaterIn3L, WaterIn5L);
                        Cylinder5L++;
                        WaterIn5L = WaterIn5L + WaterIn3L;
                        WaterIn3L = 0;
                        Console.WriteLine("量筒A==>量筒B");
                        Console.WriteLine("A:({0}) B:({1})", WaterIn3L, WaterIn5L);
                    }                                                  
                }
                if (Cylinder3L < Cylinder5L)
                {                   
                    if (WaterIn5L == 0)//把B燒杯裝水
                    {
                        Console.WriteLine("量筒B裝滿水");
                        WaterIn5L = 5;
                        Cylinder5L--;
                        Console.WriteLine("A:({0}) B:({1})", WaterIn3L, WaterIn5L);
                    }
                    else if (WaterIn3L == 3)//A燒杯水滿了要倒掉
                    {
                        Console.WriteLine("量筒A把水倒掉");
                        WaterIn3L = 0;
                        Cylinder3L++;
                        Console.WriteLine("A:({0}) B:({1})", WaterIn3L, WaterIn5L);
                    }
                    else if (WaterIn5L > 3 - WaterIn3L)//將B燒杯倒到A,如果B燒杯的水會滿出A燒杯
                    {
                        WaterIn5L = WaterIn5L - (3 - WaterIn3L);
                        WaterIn3L = 3;
                        Console.WriteLine("量筒B==>量筒A");
                        Console.WriteLine("A:({0}) B:({1})", WaterIn3L, WaterIn5L);
                    }
                    else if (WaterIn5L <= 3 - WaterIn3L)//將B燒杯倒到A,B燒杯的水不會讓A燒杯溢出
                    {
                        WaterIn3L = WaterIn5L;
                        WaterIn5L = 0;
                        Console.WriteLine("量筒B==>量筒A");
                        Console.WriteLine("A:({0}) B:({1})", WaterIn3L, WaterIn5L);
                    }                       
                }
            }
            Console.ReadKey();
        }
    }
}


沒有留言:

張貼留言