# 演算法作業三_活動選擇

user_3763047219
c_cpp
2 years ago
1.1 kB
3
Indexable
Never
```#define _CRT_SCURE_NO_WARNINGS
#include <iostream>

int main() {
int n = 0;
int s[1000] = { 0 }, f[1000] = { 0 }, num_of_act[1000] = { 0 };
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &s[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &f[i]);
}
for (int i = 1; i <= n; i++) {
num_of_act[i] = i;
}
for (int i = 1; i <= n; i++) {
for (int j = n; j >= 1; j--) {
if (s[j] < s[j - 1]) {
int temp = 0;
temp = s[j];
s[j] = s[j - 1];
s[j - 1] = temp;

int temp2 = 0;
temp2 = f[j];
f[j] =f[j - 1];
f[j - 1] = temp2;

int temp3 = 0;
temp3 = num_of_act[j];
num_of_act[j] = num_of_act[j - 1];
num_of_act[j - 1] = temp3;
}
}
}
int ans[1000];
ans[1] = num_of_act[n];
int j = n;
int act_index = 1;
for (int i = n-1; i >= 1; i--) {
if (f[i] <= s[j]) {
act_index = act_index + 1;
ans[act_index] = num_of_act[i];
j = i;
}
}

printf("%d\n", act_index);
printf("(%d",ans[act_index]);
for (int i = act_index-1; i >=1 ; i--) {
printf(",%d", ans[i]);
}
printf(")");
}```