본문 바로가기

programming/algorithm 공부

[Algorithm] 사과나무와 오렌지나무



> Quest.

Sam's house has an apple tree and an orange tree that yield an abundance of fruit. In the diagram below, the red region denotes his house, where  is the start point and  is the end point. The apple tree is to the left of his house, and the orange tree is to its right. You can assume the trees are located on a single point, where the apple tree is at point  and the orange tree is at point .


When a fruit falls from its tree, it lands  units of distance from its tree of origin along the -axis. A negative value of  means the fruit fell  units to the tree's left, and a positive value of  means it falls  units to the tree's right.

Given the value of  for  apples and  oranges, can you determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range )? Print the number of apples that fall on Sam's house as your first line of output, then print the number of oranges that fall on Sam's house as your second line of output.



> Input.

Input Format

The first line contains two space-separated integers denoting the respective values of  and . 

The second line contains two space-separated integers denoting the respective values of  and . 

The third line contains two space-separated integers denoting the respective values of  and . 

The fourth line contains  space-separated integers denoting the respective distances that each apple falls from point . 

The fifth line contains  space-separated integers denoting the respective distances that each orange falls from point .



> Output.

Output Format

Print two lines of output:

On the first line, print the number of apples that fall on Sam's house.

On the second line, print the number of oranges that fall on Sam's house.



> solve

Summary

그러니까 요약하면 각각 사과나무는 a 에 오렌지나무는 b에 있고 s 에서 t 까지의 범위 안에 떨어지면 카운트를 합니다. 


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

int main(){
    int s; 
    int t; 
    scanf("%d %d",&s,&t);
    int a; 
    int b; 
    scanf("%d %d",&a,&b);
    int m; 
    int n; 
    scanf("%d %d",&m,&n);
    int *apple = malloc(sizeof(int) * m);
    
    int m_sum=0, n_sum=0;
    
    for(int apple_i = 0; apple_i < m; apple_i++){
       scanf("%d",&apple[apple_i]);
        if(a+apple[apple_i] >= s && a+apple[apple_i] <= t){
           m_sum++; 
        }
    }
    int *orange = malloc(sizeof(int) * n);
    for(int orange_i = 0; orange_i < n; orange_i++){
       scanf("%d",&orange[orange_i]);
        if(b+orange[orange_i] >= s && b+orange[orange_i] <= t){
           n_sum++; 
        }
    }
             
    printf("%d\n%d", m_sum, n_sum);
    return 0;
}




반응형

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

[Algorithm] Connecting Towns  (0) 2017.10.09
[Algorithm] Handshake / 악수  (0) 2017.10.09
[Algorithm] Divisible Sum Pairs  (0) 2017.10.09
[Algorithm] 캥거루 두 마리  (0) 2017.10.09
[Algorithm] Angry Professor  (0) 2017.10.09