본문 바로가기

programming/algorithm 공부

[Algorithm] Divisible Sum Pairs


> Quest.

You are given an array of  integers, , and a positive integer, . Find and print the number of  pairs where  and  +  is evenly divisible by .



> Input.

Input Format

The first line contains  space-separated integers,  and , respectively. 

The second line contains  space-separated integers describing the respective values of .



> Output.

Output Format

Print the number of  pairs where  and  +  is evenly divisible by .



> solve

Summary

요약하면, 입력 값 a 배열의 임의의 두 쌍의 합이 k 의 배수인 경우가 몇개인가 하는 문제입니다.


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n; 
    int k; 
    scanf("%d %d",&n,&k);
    int *a = malloc(sizeof(int) * n);
    for(int a_i = 0; a_i < n; a_i++){
       scanf("%d",&a[a_i]);
    }
    
    int cnt = 0;
    for(int i=0; i< n-1; i++){
        for(int j=i+1; j<n; j++){
            if((*(a+i) + *(a+j)) % k == 0){
                cnt++;
            }
        }
    }
    printf("%d", cnt);
    
    return 0;
}



반응형

'programming > algorithm 공부' 카테고리의 다른 글

[Algorithm] Connecting Towns  (0) 2017.10.09
[Algorithm] Handshake / 악수  (0) 2017.10.09
[Algorithm] 캥거루 두 마리  (0) 2017.10.09
[Algorithm] 사과나무와 오렌지나무  (0) 2017.10.09
[Algorithm] Angry Professor  (0) 2017.10.09