去华为面试的时候给了我这个题目,当时没有写完整,真是不应该,现在写出来,希望下次不要犯同样的错误。
代码没注释相信都能看懂吧!
#include
<
iostream
>
using namespace std;
struct link
{
int code;
link * next;
};
int main( void )
{
int n;
link * pJose = NULL;
link * pCurrent = NULL;
cin >> n;
pJose = new link[n];
pCurrent = pJose;
for ( int i = 1 ; i <= n; ++ i)
{
pCurrent -> next = pJose + i % n;
pCurrent -> code = i;
pCurrent = pCurrent -> next;
}
link * pTemp;
pCurrent = & pJose[n - 1 ];
while (pCurrent != pCurrent -> next)
{
for ( int i = 0 ; i < 3 ; ++ i)
{
pTemp = pCurrent;
pCurrent = pTemp -> next;
}
cout << pCurrent -> code << endl;
pTemp -> next = pCurrent -> next;
pCurrent = pTemp;
}
delete []pJose;
pJose = NULL;
}
using namespace std;
struct link
{
int code;
link * next;
};
int main( void )
{
int n;
link * pJose = NULL;
link * pCurrent = NULL;
cin >> n;
pJose = new link[n];
pCurrent = pJose;
for ( int i = 1 ; i <= n; ++ i)
{
pCurrent -> next = pJose + i % n;
pCurrent -> code = i;
pCurrent = pCurrent -> next;
}
link * pTemp;
pCurrent = & pJose[n - 1 ];
while (pCurrent != pCurrent -> next)
{
for ( int i = 0 ; i < 3 ; ++ i)
{
pTemp = pCurrent;
pCurrent = pTemp -> next;
}
cout << pCurrent -> code << endl;
pTemp -> next = pCurrent -> next;
pCurrent = pTemp;
}
delete []pJose;
pJose = NULL;
}
代码没注释相信都能看懂吧!