蒜头君想把计算机专业相关的课程都写一遍,放到计蒜客上面帮助同学们学习。但是蒜头君意识到要写的课程有很多很多,蒜头君实在忙不过来,于是他想招几位实习生帮助一块写课程。招聘广告一发,吸引了好多大牛前来应聘,于是蒜头君每天都要安排面试。 有一天,结束了一天面试后,boss 跑来问蒜头君:“小蒜,你觉得今天面试的同学里面,谁最优秀呀,谁最适合写课程呀?”蒜头君递给 boss 一沓简历,回答到:“这里有 NN 份简历,boss 你猜猜哪位同学最优秀。” 每份简历都有一个对应的 idid,编号从 11 开始,依次从第一份简历到最后一份简历。boss 会从简历里抽掉 MM 份简历,每次他随机念一个数字 num_inum i ,然后从第一份简历开始数,数到第 num_inum i 份时,就会把对应的简历抽掉,接着念下一个数字。抽掉 MM 份简历后,boss 从剩余的简历中,取出最中间的一份简历,然后点点头念道:“我相信这位同学一定最优秀,哈哈”。 现在蒜头君想知道这份简历的 idid 是多少,聪明的你能帮他算出来吗?
#include<iostream>
using namespace std;
template <class T>
struct chainNode
{
T element;
chainNode<T>* next;
chainNode() {}
chainNode(const T& element)
{
this->element = element;
}
chainNode(const T& element, chainNode<T>* next)
{
this->element = element;
this->next = next;
}
};
template<class T>
class chain
{
public:
chainNode<T>* firstNode;
int listSize;
chain(int initialCapacity = 10)
{
firstNode = NULL;
listSize = 0;
}
~chain()
{
while (firstNode != NULL)
{
chainNode<T>* nextNode = firstNode->next;
delete firstNode;
firstNode = nextNode;
}
}
void insert(int theIndex, const T& theElement)
{
if (theIndex == 0)
{
firstNode = new chainNode<T>(theElement, firstNode);
}
else
{
chainNode<T>* p = firstNode;
for (int i = 0; i != theIndex - 1; ++i)
p = p->next;
p->next = new chainNode<T>(theElement, p->next);
}
++listSize;
}
void get(int index) const
{
chainNode<T>* temp = firstNode;
for (int i = 0; i != index; ++i)
temp = temp->next;
cout << temp->element;
}
void erase(int theIndex)
{
chainNode<T>* deleteNode;
if (theIndex == 0)
{
deleteNode = firstNode;
firstNode = firstNode->next;
}
else
{
chainNode<T>* temp = firstNode;
for (int i = 0; i != theIndex - 1; ++i)
temp = temp->next;
deleteNode = temp->next;
temp->next = temp->next->next;
}
--listSize;
delete deleteNode;
}
void output() const
{
chainNode<T>* currentNode = firstNode;
for (int i = 0; i != listSize; ++i)
{
cout << currentNode->element;
currentNode = currentNode->next;
if (i < listSize - 1)
cout << " ";
}
}
};
int main()
{
int size_list = 0, size_delete = 0;
cin >> size_list >> size_delete;
chain<int> list;
for (int i = 0; i != size_list; ++i)
{
list.insert(i, i + 1);
}
int* arry = new int[size_delete];
for (int i = 0; i != size_delete; ++i)
{
int temp = 0;
cin >> temp;
arry[i] = temp;
}
for (int i = 0; i != size_delete; ++i)
{
list.erase(arry[i] - 1);
}
list.get(list.listSize / 2);
}