BaekJoon[C#]

반복문 1110

wny0320 2022. 9. 20. 15:41

백준 온라인 코딩 문제풀이

https://www.acmicpc.net/

 

Baekjoon Online Judge

Baekjoon Online Judge 프로그래밍 문제를 풀고 온라인으로 채점받을 수 있는 곳입니다.

www.acmicpc.net

 

코드 참고

https://replit.com/@wny0320

 

wny0320 (박 상운)

Run code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.

replit.com

 

10871, 10952, 10951, 1110 문제중 일부에 대해서 다룸


10871

using System;
using System.Collections.Generic;

class Program
{
    public static void Main(string[] args)
    {
        string[] Input = Console.ReadLine().Split();
        string[] num_arr = Console.ReadLine().Split();
        List<int> num_list = new List<int>();
        for (int i = 0; i < Int32.Parse(Input[0]); i++)
        {
            if (Int32.Parse(num_arr[i]) < Int32.Parse(Input[1]))
            {
                num_list.Add(Int32.Parse(num_arr[i]));
            }
        }
        for (int i = 0; i < num_list.Count; i++)
        {
            Console.Write(num_list[i] + " ");
        }
    }
}

10871은 매개변수 i가 주어진 배열의 크기만큼 반복문을 돌리게 함

 

만약 주어진 배열에 해당 index의 value가 주어진 수보다 작으면 list에 추가

 

list 인덱스들을 반복문을 통하여 출력

 


10951

using System;
using System.Collections.Generic;
class Program
{
    public static void Main(string[] args)
    {
        List<int> result = new List<int>();
        while(true)
        {
            string Input = Console.ReadLine();
            if(Input == null)
            {
                break;
            }
            else
            {
                string[] Input_split = Input.Split();
                result.Add(Int32.Parse(Input_split[0])+ Int32.Parse(Input_split[1]));
            }
        }
        for(int i = 0; i < result.Count; i++)
        {
            Console.WriteLine(result[i]);
        }
    }
}

10951은 다른 것보다 null 값 입력에서 애를 먹었음

 

null 입력은 CMD 창에서 Ctrl + Z를 누르고 나온 명령을 입력하면 중지가 돼서 null이 입력이 됨

 

나머지는 10871과 동일

 


1110

using System;
class Program
{
    public static void Main(string[] args)
    {
        int input = Int32.Parse(Console.ReadLine());
        int new_num = input;
        int cnt = 0;
        do
        {
            if (new_num < 10)
            {
                new_num = new_num * 10 + new_num;
                cnt++;
            }
            else
            {
                new_num = (new_num % 10) * 10 + ((new_num / 10 + new_num % 10) % 10);
                cnt++;
            }
        } while (input != new_num);
        Console.WriteLine(cnt);
    }
}

1110은 입력 값 input 과 계산하여 나오는 새로운 숫자 new_num을 변수로 두고 do - while 문을 이용함

(∵new_num = input으로 두었기 때문에 while문을 사용하여 조건이 맞아 바로 반복문이 실행되지 않는 경우)

 

new_num의 10의 자리 수는 new_num / 10 으로 1의 자리 수는 new_num % 10 으로 알 수 있음

 

그리고 새 숫자를 만들 때 new_num의 각 자리수의 합이 10이 넘을 수 있으므로 1의 자리만 빼기 위해 %10을 한번 더 함

 

이후 cnt 값을 출력해줌

'BaekJoon[C#]' 카테고리의 다른 글

1차원 배열 4344  (0) 2022.09.24
1차원 배열 2562  (0) 2022.09.23
반복문 2439  (0) 2022.09.19
반복문 15552  (0) 2022.09.18
반복문 10950  (0) 2022.09.17