แสดงค่าที่หายไปจากชุดข้อมูลที่ผู้ใช้ป้อน
Given: array = {88, 105, 3, 2, 200, 0, 10}
Output: 1 4-9 11-87 89-99
อัลกอริทึม
START STEP 1 -> Take an array with elements, bool flag[MAX] to False, int i, j, n to size of array STEP 2 -> Loop For from i to 0 and i= 0 Set flag[array[i]] = true End IF Step 3 -> End For Loop Step 4 -> Loop For from i to 0 and i End For Loop STOP
ตัวอย่างโค้ด (C)
#include <stdio.h>
#define MAX 100
int main(int argc, char const *argv[]) {
int array[] = {88, 105, 3, 2, 200, 0, 10};
bool flag[MAX] = {false};
int i, j, n;
n = sizeof(array)/sizeof(array[0]);
for (i = 0; i < n; i++) {
if (array[i] < 100 && array[i] >= 0) {
flag[array[i]] = true;
}
}
for (i = 0; i < MAX; ++i) {
if (flag[i] == false) {
j = i + 1;
while (j < MAX && flag[j] == false)
j++;
if (j == i + 1)
printf("%d\n", i);
else
printf("%d-%d\n", i, j-1);
i = j;
}
}
return 0;
}
ผลลัพธ์
1 4-9 11-87 89-99