鱼C论坛

 找回密码
 立即注册
查看: 2905|回复: 1

救助

[复制链接]
发表于 2021-10-6 20:00:24 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 sara_est_sarah 于 2021-10-6 20:10 编辑

系統说答案是正確的,但presentation error ...
为什么。。。谢谢。

#include <iostream>

using namespace std;
class node
{
    public:
    int data;
    struct node*next;
};

int main()
{
    int n,m,i;
    char a;
    int answer[100];
    int count=0;
    struct node *head,*tail,*p,*q;
    head=(struct node*)malloc(sizeof(struct node));
    head->data=-1;
    head->next=NULL;
   
   
   
    cin>>n>>m;
        
        
            tail=head;
            for(i=0;i<n;i++)
            {
                p=(struct node*)malloc(sizeof(struct node));
                p->data=i+1;
                tail->next=p;
                p->next=head->next;
                tail=p;
               
            }
            p=head->next;
            q=tail;
            i=1;
            while(p!=q)
            {
                if(i==m){
                    q->next=q->next->next;
                    int j=p->data;
                    cout<<j<<" ";
                    free(p);
                    p=q->next;
                    i=1;
                }
                else
                {
                    q=p;
                    p=p->next;
                    i++;
                }
            }
            
            answer[count]=p->data;
            count++;
            free(p);
            head->next=NULL;
        
        
   
    for (i=0;i<count;i++){
        cout<<answer;
    }
    free(head);

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-6 20:10:01 From FishC Mobile | 显示全部楼层
Question: (Josephus Problem)
n individuals labeled from 1 to n form a circle, which means the next person of the n-th person
is the first person. Counting begins at the first person, and the m-th person counted will go out.
Then the counting restart at the next person of the one who went out, and still the m-th person
counted will go out. Repeat the counting until all of the people have gone out.

In this problem, given n and m, please show the order they went out.

Input
A single line containing two integers n and m separated by a space.

Output
Print n integers in a single line denoting the labels of these n persons and indicating the order
they went out. Please separate each two of these integers by a single space.

Constraint:
1≤n,n≤10^4
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-4 01:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表