9466번: 텀 프로젝트
이번 가을학기에 '문제 해결' 강의를 신청한 학생들은 텀 프로젝트를 수행해야 한다. 프로젝트 팀원 수에는 제한이 없다. 심지어 모든 학생들이 동일한 팀의 팀원인 경우와 같이 한 팀만 있을 수도 있다. 프로젝트 팀을 구성하기 위해, 모든 학생들은 프로젝트를 함께하고 싶은 학생을 선택해야 한다. (단, 단 한 명만 선택할 수 있다.)
https://www.acmicpc.net/problem/9466

Description

  • 싸이클을 이루는 그룹은 제외하고, 이루지 못하는 학생들의 수를 얻어야 함.
  • 시간이 무려 3초지만 O(N)으로 해결해야 함.
  • 따라서 한 번 스캔하면서 싸이클을 이루지 못하는 학생들을 체크하고, 싸이클을 따라가다 해당 학생을 만나면 탐색을 중단해야 함.
  • vector_name.clear()를 이용해 초기화하는 것이 시간소모 적음.

 

#include <iostream>
#include <vector>
using namespace std;

int T, N;
vector<int> students;

void cal() {
	int answer = 0;
	int group_num = 1;
	vector<bool> visited(N, false);
	vector<int> group(N, 0);
	vector<int> visited_node;	// temporal cycling students

	for (int i = 0; i < N; i++) {

		// if the student already has a group num, skip 
		if (group[i] || visited[i] == true) continue;

		visited_node.clear();
		visited[i] = true;
		visited_node.push_back(i);

		int cur_node = students[i];
		// scan only !visited node && had no group(=0) 
		while (!visited[cur_node] && group[cur_node] == 0) {
			visited[cur_node] = true;
			visited_node.push_back(cur_node);
			cur_node = students[cur_node];
		}

		/*	if origin and destin. same after scan cycle, find where does the cycle starts.
			if it's not the node which starts cycle, put -1 to mark it's not groupable */
		int visited_size = visited_node.size();
		for (int j = 0; j < visited_size; j++) {
			if (visited_node[j] != cur_node) {
				group[visited_node[j]] = -1;
			}
			else {
				for (int k = j; k < visited_size; k++) {
					group[visited_node[j]] = group_num;
				}
				group_num++;
				break;
			}
		}
	}

	for (int i = 0; i < N; i++) {
		if (group[i] < 0) answer++;
	}

	cout << answer << "\n";

	return;
}

int main() {
	ios::sync_with_stdio(false);

	cin >> T;
	for (int i = 0; i < T; i++) {
		cin >> N;
		students = vector<int>(N, 0);

		for (int j = 0; j < N; j++) {
			cin >> students[j];
			students[j] -= 1;
		}
		cal();
	}

	return 0;
}

'스터디 > 알고리즘' 카테고리의 다른 글

C++ 백준 4673 - 셀프 넘버  (0) 2019.11.17
C++ 백준 4344 - 평균은 넘겠지  (0) 2019.11.17
C++ 백준 8958 - OX퀴즈  (0) 2019.11.17
C++ 백준 1546 - 평균  (0) 2019.11.17
C++ 백준 2577 - 숫자의 개수  (0) 2019.11.17

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
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <iostream>
#include "string.h"
using namespace std;
 
int Generate(int OG);
 
int main()
{
    bool Array[10001= { 0 };
 
    for (int i = 1; i < 10001; i++
    {
        int temp = Generate(i);
        if (temp < 10001)
        {
            Array[temp] = 1;
        }
    }
 
    for (int i = 1; i < 10001; i++)
    {
        if(Array[i] == 0printf("%d\n", i);
    }
 
    return 0;
}
 
int Generate(int OG) 
{
    int sum = OG;
 
    while (OG != 0)
    {
        sum += (OG%10);
        OG /= 10;
    }
 
    return sum;
}
 

1부터 순차적으로 수열의 다음 숫자를 생성한 뒤 생성된 숫자를 1~10000까지의 배열에서 True값으로 바꿔버린다.
그리고 난 후 False값을 가지는 숫자들 (셀프 넘버)만 출력한다.

 

bool type의 배열 초기화는 False, 0으로! 기억하자

'스터디 > 알고리즘' 카테고리의 다른 글

BOJ 9466_텀 프로젝트  (0) 2021.03.09
C++ 백준 4344 - 평균은 넘겠지  (0) 2019.11.17
C++ 백준 8958 - OX퀴즈  (0) 2019.11.17
C++ 백준 1546 - 평균  (0) 2019.11.17
C++ 백준 2577 - 숫자의 개수  (0) 2019.11.17

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
32
33
34
35
36
37
#include <stdio.h>
#include <iostream>
#include "string.h"
using namespace std;
 
int main()
{
    int C, N = 0;
    int Scores[1000= { 0 };
 
    scanf("%d"&C);
    for (int i = 0; i < C; i++
    {
        scanf("%d"&N);
        double sum = 0;
 
        for (int k = 0; k < N; k++)
        {
            int score = 0;
            scanf("%d"&score);
            Scores[k] = score;
            sum += score;
        }
 
        sum /= N;
        int count = 0;
 
        for (int k = 0; k < N; k++)
        {        
            if (Scores[k] > sum) count++;
        }
 
        printf("%0.3lf%\n", (double)count / N * 100);
    }
 
    return 0;
}
 
 

포인트는 마지막에 퍼센트를 계산할 때 double로 분자를 형변환 시켜주는 것. 

'스터디 > 알고리즘' 카테고리의 다른 글

BOJ 9466_텀 프로젝트  (0) 2021.03.09
C++ 백준 4673 - 셀프 넘버  (0) 2019.11.17
C++ 백준 8958 - OX퀴즈  (0) 2019.11.17
C++ 백준 1546 - 평균  (0) 2019.11.17
C++ 백준 2577 - 숫자의 개수  (0) 2019.11.17

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
32
33
34
35
36
#include <stdio.h>
#include <iostream>
#include "string.h"
using namespace std;
 
int main()
{
    int N = 0;
    char Ans[81= { 0 };
 
    scanf("%d\n"&N);
 
    for(int i = 0 ; i < N; i++) {
        scanf("%s", Ans);
        int count = 0;
        int sum = 0;
 
        for (int j = 0; j < strlen(Ans) + 1; j++) {        // \0 까지 세어줘야 함
            if (Ans[j] == 'O')
            {
                count++;
                sum += count;
            }
            else if (Ans[j] == 'X')
            {
                count = 0;
            }
            else if (Ans[j] == '\0')
            {
                printf("%d\n", sum);
            }
        }
    }
 
    return 0;
}
 


미리 사이즈가 81인 (문자열이므로 \0 감안) Char 배열을 선언해두고 입력받아 그대로 카운트 한 뒤, 출력

'스터디 > 알고리즘' 카테고리의 다른 글

C++ 백준 4673 - 셀프 넘버  (0) 2019.11.17
C++ 백준 4344 - 평균은 넘겠지  (0) 2019.11.17
C++ 백준 1546 - 평균  (0) 2019.11.17
C++ 백준 2577 - 숫자의 개수  (0) 2019.11.17
C++ 백준 10951 - A+B - 4  (0) 2019.11.14
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
#include <stdio.h>
using namespace std;
 
int main()
{
    double Scores[1000= { 0 };
    int N = 0;
    double max = 0;
    double sum = 0;
 
    scanf("%d\n"&N);
 
    for (int i = 0; i < N; i++) {
        int score = 0;
        scanf("%d"&score);
        Scores[i] = score;
    }
 
    for (int i = 0; i < N; i++) {
        if (Scores[i] > max) max = Scores[i];
    }
 
    for (int i = 0; i < N; i++) {
        Scores[i] = Scores[i] / max * 100;
        sum += Scores[i];
    }
 
    printf("%0.2lf\n", sum / N);
 
    return 0;
}
 

다른 건 별 문제가 없었는데, 소수점 아래 2자리까지 반올림해 출력하도록 하는 게 기억이 안나서 찾아봤다.

0.2lf로구나.

'스터디 > 알고리즘' 카테고리의 다른 글

C++ 백준 4344 - 평균은 넘겠지  (0) 2019.11.17
C++ 백준 8958 - OX퀴즈  (0) 2019.11.17
C++ 백준 2577 - 숫자의 개수  (0) 2019.11.17
C++ 백준 10951 - A+B - 4  (0) 2019.11.14
C++ 백준 2884 - 알람시계  (0) 2019.09.09

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
 
int main()
{
    int A, B, C, Result =0;
    int count[10= {0};
    
    scanf("%d\n%d\n%d\n"&A, &B, &C);
    Result = A*B*C;
    
    while(Result > 0){
        int num = Result % 10;
        Result /= 10;
        count[num]++;
    }
    
    for(int i = 0; i < 10; i++){
        printf("%d\n", count[i]);
    }
    
    return 0;
}
 

 

 

포인트는 각 숫자들을 곱한 값이 몇 자리 수일지 모르기에

 

몇 자리이던 상관없이 계산 가능하도록 %를 이용해 각 자리를 구하며 즉시 배열에 값으로 때려넣는 것,,!

'스터디 > 알고리즘' 카테고리의 다른 글

C++ 백준 8958 - OX퀴즈  (0) 2019.11.17
C++ 백준 1546 - 평균  (0) 2019.11.17
C++ 백준 10951 - A+B - 4  (0) 2019.11.14
C++ 백준 2884 - 알람시계  (0) 2019.09.09
C++ 백준 2753 - 윤년  (0) 2019.09.09

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
using namespace std;
int main() {
    int A, B = 0;
    
    while (scanf("%d %d"&A, &B) != EOF) {
        printf("%d\n", A + B);
    }
    return 0;
}
 


입력받는 A, B 쌍의 개수가 정해지지 않은 케이스. 종료 조건도 나와 있지 않다.

for(;;)을 사용하면 출력 초과로 틀리게 된다.

이런 경우엔 End of File (EOF) 인지 체크하도록 해야한다.

더이상 읽을 데이터가 없는지를 체크하는 것.

'스터디 > 알고리즘' 카테고리의 다른 글

C++ 백준 8958 - OX퀴즈  (0) 2019.11.17
C++ 백준 1546 - 평균  (0) 2019.11.17
C++ 백준 2577 - 숫자의 개수  (0) 2019.11.17
C++ 백준 2884 - 알람시계  (0) 2019.09.09
C++ 백준 2753 - 윤년  (0) 2019.09.09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<stdio.h>
using namespace std;
 
int main() {
    int hour, minute;
 
    cin >> hour >> minute;
 
    if (minute >= 45) minute -= 45;
    else if (hour == 0) {
        hour = 23;
        minute = 15 + minute; // 60 + (minute - 45)
    }
    else if (hour != 0) {
        hour--;
        minute = 15 + minute; // 60 + (minute - 45)
    }
 
    cout << hour << " " << minute << endl;
 
    return 0;
}
 
 

 

hour 이랑 minute 사이에 " " 출력 빼먹어서 진짜 한참 쳐다봤따.,.

 

https://tre2man.tistory.com/121

언제나 더 깔끔한 풀이가 있는 법..,,

 

'스터디 > 알고리즘' 카테고리의 다른 글

C++ 백준 8958 - OX퀴즈  (0) 2019.11.17
C++ 백준 1546 - 평균  (0) 2019.11.17
C++ 백준 2577 - 숫자의 개수  (0) 2019.11.17
C++ 백준 10951 - A+B - 4  (0) 2019.11.14
C++ 백준 2753 - 윤년  (0) 2019.09.09

+ Recent posts