SWEA 1940 가랏! RC카!

1 개요[ | ]

SWEA 1940 가랏! RC카!
SW Expert 아카데미
# 문제 풀이

틀:SWEA 난이도 2-3

2 C++[ | ]

#include <iostream>
using namespace std;
int main() {
    int T;
    scanf("%d", &T);
    int N, command, param;
    for(int tc=1; tc<=T; tc++) {
        scanf("%d", &N);
        int speed = 0;
        int distance = 0;
        for(int i=0; i<N; i++) {
            scanf("%d", &command);
            switch(command) {
            case 1:
                scanf("%d", &param);
                speed += param;
                break;
            case 2:
                scanf("%d", &param);
                speed -= param;
                if( speed<0 ) speed = 0;
                break;
            }
            distance += speed;   
        }
        printf("#%d %d\n", tc, distance);
    }
}

3 Java[ | ]

import java.util.Scanner;
public class Solution {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int t=1; t<=T; t++) {
            int N = sc.nextInt();
            int a, v = 0, d = 0;
            for(int n=0; n<N; n++) {
                int command = sc.nextInt();
                if( command != 0 ) {
                    a = sc.nextInt();
                    if( command == 1 ) v += a;
                    else {
                        v -= a;
                        if( v<0 ) v=0;
                    }
                }
                d += v;
            }
            System.out.format( "#%d %d\n", t, d );
        }
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}