这个题和约瑟夫环很像 用链表可以轻易解决的问题~
注意:认真读题 不包括m n 容易被坑…
#include <iostream>
using namespace std;
struct ListNode
{
int num;
struct ListNode* next;
ListNode(int num):num(num),next(NULL){}
};
int main()
{
int m,n;
cin>>m>>n;
ListNode* head=new ListNode(1);
ListNode* p=head;
ListNode* xy;int f=0;
for(int i=2;i<=n;i++)
{
if(i%2==1)//奇
{
f++;
ListNode* q=new ListNode(i);
if(f==1) xy=q;//存当前幸运数
p->next=q;
p=p->next;
}
}
int dn=1;
ListNode* h=head;
while(dn)
{
dn=0;
int index=1;
h=head;
while(h->next!=NULL)
{
index++;
if(index%xy->num==0)
{
dn++;
h->next=h->next->next;
}
else h=h->next;
}
xy=xy->next;
}
int ans=0;
h=head;
while(h->next!=NULL)
{
if(h->num>m&&h->num<n)
ans++;
h=h->next;
}
if(h->num<n) ans++;
cout<<ans;
return 0;
}