2017年5月20日 星期六

ITSA 52 - [Problem 4] 合併排序 - 參考答案

Difficulty: Eazy
Ref: ITSA 52 - [Problem 4] 合併排序
/*******************************************************/
/* [Problem 4] 合併排序                                 */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/20                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
 char buf[512];
 while (1) {
  bool data[512] = { false };
  int lastout;

  for (int i = 0; i < 2; i++) {
   fgets(buf, 512, stdin);
   char *pch = strtok(buf, " ");
   while (pch != NULL) {
    data[lastout = atoi(pch)] = true;
    pch = strtok(NULL, " ");
   }
   if (!lastout) return 0;
  }

  for (int i = 0; i < 512; i++)
   if (data[i])
    if (lastout) {
     lastout = 0;
     printf("%d", i);
    }
    else
     printf(" %d", i);

  printf("\n");
 }
}
Debug: I/O
使用陣列做標記完成。
不用想太複雜,測資給的也很小。
2 5 6 8 12
1 4 15
1 8 9 11
2 4 5 10 13
0 0 0 0 0
1 2 4 5 6 8 12 15
1 2 4 5 8 9 10 11 13

沒有留言:

張貼留言