标签:
//10个小屁孩报数,报到3退出,继续报,选出孩子王 struct tagKing { int index; tagKing * pNext; }; void main() { int n = 10; tagKing * pKings = new tagKing[n]; for (int i=0;i<n;i++) { pKings[i].index = i+1; pKings[i].pNext = &pKings[i+1]; if(i+1>=n) pKings[i].pNext = &pKings[0]; } tagKing * pFirst = &pKings[0]; tagKing * pCur = pFirst; while(pCur->pNext->index != pCur->index) { tagKing * pTmp = pCur->pNext->pNext; //1-2-3-4-5-6-7-8-9-10 pCur = pCur->pNext->pNext->pNext; printf("index=%d,need out\n",pTmp->index); pTmp->index = pTmp->pNext->index; pTmp->pNext = pTmp->pNext->pNext; } printf("the king is %d\n",pCur->pNext->index); }
标签:
原文地址:http://my.oschina.net/cn0512/blog/515469