View

https://www.acmicpc.net/problem/2577

 

2577번: 숫자의 개수

첫째 줄에 A, 둘째 줄에 B, 셋째 줄에 C가 주어진다. A, B, C는 모두 100보다 크거나 같고, 1,000보다 작은 자연수이다.

www.acmicpc.net

 

📚 문제

두 자연수 A와 B가 있을 때, A%B는 A를 B로 나눈 나머지 이다. 예를 들어, 7, 14, 27, 38을 3으로 나눈 나머지는 1, 2, 0, 2이다. 수 10개를 입력받은 뒤, 이를 42로 나눈 나머지를 구한다. 그 다음 서로 다른 값이 몇 개 있는지 출력하는 프로그램을 작성하시오.

예제 입력 예제 출력
39
40
41
42
43
44
82
83
84
85
6

 

[힌트] 39, 40, 41, 42, 43, 44, 82, 83, 84, 85를 42로 나눈 나머지는 39, 40, 41, 0, 1, 2, 40, 41, 0, 1이다. 서로 다른 값은 모두 6개가 있다.

 

📝 문제 해결

풀이 1 - boolean형 배열

나머지는 0부터 41까지이므로 boolean형 배열 42개짜리를 만들어준다. 그리고 각각의 입력에서 42를 나눈 나머지 값이 처음 나왔다면 (!arr[num]) arr[num]을 true로 처리해주고 count값을 1 증가시켜주어 카운팅한다. 

import java.util.Scanner;
import java.io.IOException;

public class Main{
    public static void main(String args[]) throws IOException{
        Scanner sc=new Scanner(System.in);
        boolean[] array=new boolean[42];

        int num=0;
        int count=0;

        for(int i=0;i<10;i++){
            num=sc.nextInt()%42;
            if(!array[num]){
                count++;
                array[num]=true;
            }
        }
        System.out.println(count);
    }
}

 

풀이2 - HashSet

import java.util.HashSet;
import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        HashSet<Integer> hs=new HashSet<Integer>();
        int num[]=new int[10];
        
        for(int i=0;i<num.length;i++){
            num[i]=sc.nextInt()%42;
        }
        for(int i=0;i<num.length;i++){
            hs.add(num[i]);
        }
        System.out.println(hs.size());
    }
}
 

*HashSet : Set의 파생클래스로 순서가 없고 중복을 허용하지 않는다.

  • HashSet은 순서를 보장하지 않기 때문에 출력해보면 입력한 순서와 다르게 출력이 된다.
  • HashSet은 null 저장을 허용한다.
  • HashSet<Type>으로 HashSet을 생성할 수 있다.
  •  add() : 인자를 저장하고 객체를 저장할 때 객체가 Set에 저장되어있지 않았다면 True를 리턴합니다. 이미 저장되어있다면 False를 리턴합니다. 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package part1;
 
import java.util.HashSet;
import java.util.Scanner;
 
public class BaekJoon{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        HashSet<String> hs=new HashSet<String>();
        String fruits[]=new String[5];
        
        for (int i=0;i<fruits.length;i++){
            fruits[i]=sc.next();
        }
        for(int i=0;i<fruits.length;i++) {
            hs.add(fruits[i]);
        }
        System.out.print(hs);
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
딸기
바나나
사과
키위
딸기
[사과, 키위, 바나나, 딸기]
  • remove() : 인자로 전달된 객체를 Set에서 삭제한다. Set에 객체가 존재하여 삭제가 되었다면 True를 리턴한다. Set에 파일이 존재하지 않으면 False를 리턴한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package part1;
 
 
public class BaekJoon{
    public static void main(String args[]){
        HashSet<String> fruits=new HashSet<String>();
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("kiwi");
        System.out.println("fruits: "+fruits);
        
        fruits.remove("apple");
        System.out.println("fruits: "+fruits);
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
fruits: [banana, apple, kiwi]
fruits: [banana, kiwi]
  • removeAll() : 인자로 받은 Collection에 저장된 아이템들을 HashSet에서 삭제한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package part1;
 
 
public class BaekJoon{
    public static void main(String args[]){
        HashSet<String> fruits=new HashSet<String>();
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("kiwi");
        System.out.println("fruits: " + fruits);
 
        ArrayList<String> removed = new ArrayList<>();
        removed.add("apple");
        removed.add("kiwi");
 
        fruits.removeAll(removed);
        System.out.println("fruits: " + fruits);
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
fruits: [banana, apple, kiwi]
fruits: [banana]
  • clear() : HashSet의 모든 아이템들을 삭제한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package part1;
 
 
public class BaekJoon{
    public static void main(String args[]){
        HashSet<String> fruits = new HashSet<String>();
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("kiwi");
        System.out.println("fruits: " + fruits);
 
        fruits.clear();
        System.out.println("fruits: " + fruits);
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
fruits: [banana, apple, kiwi]
fruits: []
  • contains() : Set안에 객체가 존재하는지 여부를 리턴해준다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package part1;
 
 
public class BaekJoon{
    public static void main(String args[]){
        HashSet<String> fruits = new HashSet<String>();
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("kiwi");
 
        System.out.println("has apple? " + fruits.contains("apple"));
        System.out.println("has grape? " + fruits.contains("grape"));
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
has apple? true
has grape? false
  • Iterator() : HashSet의 모든 객체를 순회한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package part1;
 
 
public class BaekJoon{
    public static void main(String args[]){
        HashSet<String> fruits = new HashSet<String>();
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("kiwi");
 
        Iterator<String> it = fruits.iterator();
        while (it.hasNext())
            System.out.println("fruits: " + it.next());
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
fruits: banana
fruits: apple
fruits: kiwi

 Iterator() 대신 for문을 이용하여 모든 객체에 접근

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package part1;
 
 
public class BaekJoon{
    public static void main(String args[]){
        HashSet<String> fruits = new HashSet<String>();
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("kiwi");
 
        for (String fruit : fruits) {
            System.out.println("fruits: " + fruit);
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
fruits: banana 
fruits: apple 
fruits: kiwi
  • isEmpty() : HashSet에 저장된 아이템이 없으면 True를 리턴해준다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package part1;
 
 
public class BaekJoon{
    public static void main(String args[]){
        HashSet<String> fruits = new HashSet<String>();
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("kiwi");
        
        System.out.println("is empty? " + fruits.isEmpty());
        fruits.clear();
        System.out.println("is empty? " + fruits.isEmpty());
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
is empty? false
is empty? true
  • size() : HashSet에 저장된 아이템 개수를 리턴한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package part1;
 
 
public class BaekJoon{
    public static void main(String args[]){
        HashSet<String> fruits = new HashSet<String>();
        fruits.add("apple");
        
        System.out.println("size? " + fruits.size());
        
        fruits.add("banana");
        fruits.add("kiwi");
        
        System.out.println("size? " + fruits.size());
        System.out.println("is empty? " + fruits.isEmpty());
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
size? 1
size? 3
is empty? false
728x90
Share Link
reply
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31