2007年3月8日 星期四

CMClass: 微軟徵才試題 (續)

昨天的文章算錯答案了... :P
所以... 重寫一個程式... ^o^
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "set.h"

#define LOWER 2
#define UPPER 49

int main(int argc, char *argv[])
{
int i,j;

int m[UPPER * UPPER] = {0};
int a[UPPER + UPPER] = {0};

Set *setA = newSet();
Set *setB = newSet();
Set *setC = newSet();

Node *index = NULL;

for (i = LOWER; i <= UPPER - 1; i++)
for (j = i + 1; j <= UPPER; j++) {
m[i * j]++;
a[i + j]++;
}

for (i = LOWER; i <= UPPER - 1; i++)
for (j = i + 1; j <= UPPER; j++)
if (m[i * j] >= 2)
appendSet(setA, newPair(i, j));

memset(m, 0, sizeof(int) * UPPER * UPPER);
memset(a, 0, sizeof(int) * UPPER * 2);

for (i = LOWER; i <= UPPER - 1; i++)
for (j = i + 1; j <= UPPER; j++)
if (findElement(setA, newPair(i, j))) {
m[i * j]++;
a[i + j]++;
}

for (i = LOWER; i <= UPPER - 1; i++)
for (j = i + 1; j <= UPPER; j++)
if (a[i + j] >= 2 && findElement(setA, newPair(i, j)))
appendSet(setB, newPair(i, j));

memset(m, 0, sizeof(int) * UPPER * UPPER);
memset(a, 0, sizeof(int) * UPPER * 2);

for (i = LOWER; i <= UPPER - 1; i++)
for (j = i + 1; j <= UPPER; j++)
if (findElement(setB, newPair(i, j))) {
m[i * j]++;
a[i + j]++;
}

for (i = LOWER; i <= UPPER - 1; i++)
for (j = i + 1; j <= UPPER; j++)
if (m[i * j] == 1 && findElement(setB, newPair(i, j)))
appendSet(setC, newPair(i, j));

for (index = setC->head; index != NULL; index = index->next)
printf("%d %d\n", index->curr->x, index->curr->y);

destorySet(setA);
destorySet(setB);
destorySet(setC);

return 0;
}
完整的 Source Code 在 http://fd.idv.tw/tux/CMClass.tar.bz2
有興趣可以幫忙 debug ... :P

沒有留言: