Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
有哪位高手帮忙看一下,我这样做怎么是WA的呢? 验证Sample没问题,是不是还有些特殊情况?#include <stdio.h> typedef struct tagNode { int nParent; int nChildNum; }Node; void PrintNode(Node *pIndex, int nNodeNum, int nNode) { int i = 0; if(pIndex[nNode].nParent == 0) printf("(%d", nNode); else printf(" (%d", nNode); for(i = 1; i <= nNodeNum; i ++) if(pIndex[i].nParent == nNode) PrintNode(pIndex, nNodeNum, i); printf(")"); } void PrintTree(Node *pIndex, int nNodeNum) { int i = 0, nRoot = 1; for(i = 1; i <= nNodeNum; i++) { if(pIndex[i].nParent) continue; nRoot = i; break; } PrintNode(pIndex,nNodeNum, nRoot); } int InitData(int *pData, char *strLine) { int res = 0, i = 0,j = 0; char strPart[4]; while(strLine[i] != '\0') { while(strLine[i] == ' ') i ++; j = 0; while(strLine[i] >= '0' && strLine[i] <= '9') strPart[j++] = strLine[i++]; strPart[j] = '\0'; pData[res++] = atoi(strPart); } return res + 1; } int main() { Node pIndex[100]; int pData[100]; int nNodeNum, i, j; int nMinLeaf, nMaxLeaf; char strLine[2048]; while(1) { if(NULL == gets(strLine) || strLine[0] == '\0') break; for(i = 0; i < 100; i++) { pIndex[i].nParent = 0; pIndex[i].nChildNum = 0; } nNodeNum = InitData(pData, strLine); for(i = 0; i < nNodeNum-1; i++) pIndex[pData[i]].nChildNum ++; for(i = 0; i < nNodeNum - 1; i ++) { nMinLeaf = 0x7FFFFFFF; for(j = 1; j <= nNodeNum; j++) { if(pIndex[j].nParent || pIndex[j].nChildNum > 0) continue; if(nMinLeaf > j) nMinLeaf = j; } pIndex[nMinLeaf].nParent = pData[i]; pIndex[pData[i]].nChildNum --; } PrintTree(pIndex, nNodeNum); printf("\n"); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator