Algorithm(23.1.19)

2023. 1. 19. 22:07·algorithm

2439번 

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int i=0;i<T;i++) {
            for(int j=0;j<T-1-i;j++) {
                System.out.print(" ");
            }
            for(int k=0;k<=i;k++) {
                System.out.print("*");
            }
            System.out.println("");
        }
    }
}

구현을 할때, 반복문의 구조를 짜는것에 큰 어려움은 없었지만, 사소한 범위를 정하는 부분에 대해서 어려움을 겪었다.

여러 반복문을 사용하는 코드에 대해서 조금 더 이해가 필요할 듯 하다.


10951번

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a + b);
        }
    }
}

처음엔 while(True)를 사용해서 무한루프를 만들어 반복문을 돌리려 했으나, 입력값이 모호한 관계로 에러가 나왔다.

따라서 .hasNext()라는 Scanner메소드를 이용하였다.

 

 


10807번

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int a = 0;
        int arr[] = new int[N];
        for(int i=0;i<N;i++){
            arr[i]=sc.nextInt();
        }

        int V = sc.nextInt();
        for(int j=0;j<arr.length;j++){
            if(arr[j]==V){
                a++;
            }
        }
        System.out.println(a);
    }
}

배열을 초기화 해야 한다는 방법이 중요했다.

int 배열[] = new int[배열크기];

 

EOF를 활용하는 2439번의 명확한 공부와 배열,함수 부분은 다음 시간에 포스팅 해보도록 하겠다.!

저작자표시 (새창열림)

'algorithm' 카테고리의 다른 글

Algorithm(23.2.2)  (0) 2023.02.02
Algorithm(23.1.29)  (0) 2023.01.31
Algorithm(23.1.23)  (0) 2023.01.23
Algorithm (23.1.5)  (1) 2023.01.07
'algorithm' 카테고리의 다른 글
  • Algorithm(23.2.2)
  • Algorithm(23.1.29)
  • Algorithm(23.1.23)
  • Algorithm (23.1.5)
noeyh
noeyh
기록하고 성장하는 개발자, 최유현 입니다. github : https://github.com/Choiyuhyeon
    티스토리 홈 로그아웃
  • noeyh
    CreateU
    noeyh
  • 전체
    오늘
    어제
  • 글쓰기 관리
    GitHub
    Notion
    • 분류 전체보기 (31)
      • web (22)
        • html (5)
        • css (4)
        • js (5)
        • react (6)
        • next (1)
      • server (0)
      • linux (0)
      • figma (0)
      • ml (0)
      • algorithm (5)
      • git (1)
      • 미니프로젝트 (0)
      • 프로젝트 (0)
      • 정보 (0)
      • 일상 (0)
      • memo (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • hELLO· Designed By정상우.v4.10.6
noeyh
Algorithm(23.1.19)
상단으로

티스토리툴바