본문 바로가기

C#/수업 과제

알고리즘 연습문제

1.

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("1. 공격 2. 스킬 3. 도망");
            int act = Convert.ToInt32(Console.ReadLine());
            
            if ((1 <= act) && (act <= 3))
            {
                if (act == 1)
                {
                    Console.WriteLine("고블린의 남은 hp는 281입니다.");
                    Console.WriteLine("앞집꼬마님의 남은 hp는 663, mp는 159입니다.");
                }
                if (act == 2)
                {
                    Console.WriteLine("스킬을 사용했습니다.");
                }
                if (act == 3)
                {
                    Console.WriteLine("당신은 도망쳤습니다.");
                }
            }
            else
            {
                Console.WriteLine("잘못 입력하셨습니다.");
            }
        }
    }
}

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

2.

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("갈수 있는 곳은 안내실, 승강기, 동, 공원 입니다.");
            Console.Write("가려는 곳을 입력하세요.");
            string input = Console.ReadLine();
            bool office = input == "안내실";
            bool lift = input == "승강기";
            bool dong = input == "동";
            bool park = input == "공원";

            if (office)
            {
                Console.WriteLine("안내실로 이동합니다.");
            }
            else if (lift)
            {
                Console.WriteLine("승강기로 이동합니다.");
            }
            else if (dong)
            {
                Console.WriteLine("동으로 이동합니다.");
            }
            else if (park)
            {
                Console.WriteLine("공원으로 이동합니다.");
            }
            else
            {
                Console.WriteLine("{0}으로는 갈수 없습니다.", input);
            }
        }
    }
}

 

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

3.

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("아이템 목록: 도끼, 활, 창");
            Console.Write("착용하시려는 장비를 입력하세요: ");
            string input = Console.ReadLine();
            bool isTrue = (input == "도끼" || input == "활" || input == "창");

            if (isTrue)
            {
                Console.WriteLine("{0}를 착용했습니다.", input);
            }
            else
            {
                Console.WriteLine("{0}은 없습니다.", input);
            }
        }
    }
}

코드를 최대한 간결하게 치려는 방향으로 접근했다.

==을 입력해야할곳에 자주 =만 입력해서 오류가 많이났다.

&&와 ||의 사용도 혼동하지 않게 주의해야겠다.

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

4.

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("m : 마린");
            Console.WriteLine("b : 파벳");
            Console.WriteLine("g : 고스트");
            Console.Write("생산하시려는 유닛의 단축키를 입력하세요: ");
            string input = Console.ReadLine();
            bool isTrue = (input == "m" || input == "b" || input == "g");

            if (isTrue)
            {
                Console.WriteLine("...");
                Console.WriteLine("...");
                Console.WriteLine("...");
                if (input == "m")
                {
                    input = "마린";
                }
                else if (input == "b")
                {
                    input = "파벳";
                }
                else if (input == "g")
                {
                    input = "고스트";
                }
                Console.WriteLine("{0}이 생산되었습니다.", input);
            }
            else
            {
                Console.WriteLine("...");
                Console.WriteLine("...");
                Console.WriteLine("...");
                Console.WriteLine("{0}는 없는 단축키입니다.", input);
            }
        }
    }
}

m, b, g 를 입력했을때 그걸 다시 마린, 파벳, 고스트로 출력하기 위해서

변수의 값을 새로 할당할수 있다는점을 떠올렸다.

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

5.

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("주문하려는 개수 : 10");
            Console.WriteLine("- 누르면 개수 1개 감소");
            Console.WriteLine("+ 누르면 개수 1개 증가");
            Console.Write("입력하세요: ");
            string input = Console.ReadLine();
            bool isTrue = (input == "-" || input == "+");
            int count = 10;

            if (isTrue)
            {
                if (input == "-")
                {
                    Console.WriteLine("{0}개를 주문합니다.", --count);
                }
                else if (input == "+")
                {
                    Console.WriteLine("{0}개를 주문합니다.", ++count);
                }            
            }
            else
            {
                Console.WriteLine("잘못입력하셨습니다.");
            }
        }
    }
}

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

6.

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("게임을 종료 하시겠습니까? (1:종료, 2:취소)");
            int input = Convert.ToInt32(Console.ReadLine());
            
            if (input == 1)
            {
                Console.WriteLine("게임을 종료합니다.");            
            }
            else if (input == 2)
            {
                Console.WriteLine("취소합니다.");
            }
            else
            {
                Console.WriteLine("잘못된 명령어입니다.");
            }
        }
    }
}

 

 

'C# > 수업 과제' 카테고리의 다른 글

for문 연습문제1  (0) 2021.12.24
if문 문제만들어서 풀기  (0) 2021.12.24
if문  (0) 2021.12.24
스레드 SCV Work 공유자원 동기화  (0) 2021.12.24
OX퀴즈  (0) 2021.12.24