• Home

My Codegate

  • Home

백준 30018 타슈 자바 문제풀이

2024/01/17 Posted by Codegate Java No Comments

문제 링크

문제 내용은 입력을 받아 2번째로 입력받은 줄을 3번째로 만드려면 자전거를 몇 번 옮겨야 하는지를 출력으로 반환하면 되는 문제인데

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int count = Integer.parseInt(br.readLine());

    int[] current = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    int[] target = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    int ans = 0;
    for (int i = 0; i < count; i++) {
      int calc = target[i] - current[i];
      if (calc > 0) {
        ans += Math.abs(calc);
      }
    }
    System.out.println(ans);
  }
}

다행히 움직일 경우 거리 제한이나, 제약조건이 없고 몇 번만 옮기면 되는지를 알면 되기 때문에 For 문을 돌려주면서 현재 배열과 목표 배열을 비교해서 목표 배열이 더 크면 차이나는 값을 ans에 합쳐가며 저장해준 후

배열을 모두 돌리고 나면 딱히 이동하는데 제약은 없기 때문에 이전에 합친 값을 출력해주면 끝이다

No Comments
0

Leave a Reply Cancel Reply

Introduction

My Codegate

Latest Posts

  • Google Search Console API 연동방법
  • 인텔리제이 Gradle Dependency 최신 버전 보는 방법
  • Wallet-Tracker 개발일지
  • Moralis API 자바로 호출방법
  • IntelliJ Commit 후 Push 따로 하는 방법

Categories

  • My Project (4)
  • Java (42)
  • Algorithm (161)
    • Java (152)
    • Algorithm Knowledge (3)
    • Algorithm site usage (6)
  • Vue.js (1)
  • Spring (4)
  • Docker (2)
  • IntelliJ (20)
  • Uncategorized (7)

Recent Comments

  • Codegate on Hello world!
  • A WordPress Commenter on Hello world!

© 2025 — mycodegate.com

Prev Next