"SWEA 1959 두 개의 숫자열"의 두 판 사이의 차이

9번째 줄: 9번째 줄:
==C++==
==C++==
<source lang='cpp'>
<source lang='cpp'>
#include <iostream>
using namespace std;
int main() {
    int T;
    scanf("%d", &T);
    int N, M;
    int A[20], B[20];
    for(int tc=1; tc<=T; tc++) {
        scanf("%d %d", &N, &M);
        for(int i=0; i<N; i++) scanf("%d", &A[i]);
        for(int i=0; i<M; i++) scanf("%d", &B[i]);
        int max = -99999999;
        if( M > N ) {
            for(int offset=0; offset<M-N+1; offset++) {
                int value = 0;
                for(int i=0; i<N; i++) value += A[i] * B[i+offset];
                if( value > max ) max = value;
            } 
        }
        else {
            for(int offset=0; offset<N-M+1; offset++) {
                int value = 0;
                for(int i=0; i<M; i++) value += A[i+offset] * B[i];
                if( value > max ) max = value;
            }
        }
        printf("#%d %d\n", tc, max);
    }
}
</source>
</source>



2019년 1월 26일 (토) 16:11 판

1 개요

SWEA 1959 두 개의 숫자열
SW Expert 아카데미
# 문제 풀이

틀:SWEA 난이도 2-2

2 C++

#include <iostream>
using namespace std;
int main() {
    int T;
    scanf("%d", &T);
    int N, M;
    int A[20], B[20];
    for(int tc=1; tc<=T; tc++) {
        scanf("%d %d", &N, &M);
        for(int i=0; i<N; i++) scanf("%d", &A[i]);
        for(int i=0; i<M; i++) scanf("%d", &B[i]);
        int max = -99999999;
        if( M > N ) {
            for(int offset=0; offset<M-N+1; offset++) {
                int value = 0;
                for(int i=0; i<N; i++) value += A[i] * B[i+offset];
                if( value > max ) max = value;
            }   
        }
        else {
            for(int offset=0; offset<N-M+1; offset++) {
                int value = 0;
                for(int i=0; i<M; i++) value += A[i+offset] * B[i];
                if( value > max ) max = value;
            }
        }
        printf("#%d %d\n", tc, max);
    }
}

3 Java

import java.util.Scanner;
import java.util.Arrays;
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, m, i, k;
            n = sc.nextInt();
            m = sc.nextInt();
            int a[] = new int[n];
            int b[] = new int[m];
            for( i=0; i<n; i++) a[i] = sc.nextInt();
            for( i=0; i<m; i++) b[i] = sc.nextInt();
            int sum, max = 0;
            for( k=0; k<m-n+1; k++ ) {
                sum = 0;
                for( i=0; i<n; i++) sum += a[i] * b[i+k];
                if( sum > max ) max = sum;
            }
            for( k=0; k<n-m+1; k++ ) {
                sum = 0;
                for( i=0; i<m; i++) sum += a[i+k] * b[i];
                if( sum > max ) max = sum;
            }
            System.out.format("#%d %d\n", t, max );
        }
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}