fujx23 2022-01-03 09:45 采纳率: 91.7%
浏览 21
已结题

无法获得预期的输入结果,预期输入8时获得7,但结果仍然为8,如何解决?

问题:题目:有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的人退出圈子,问最后最后留下的是原来第几号的那位.

问题相关代码,请勿粘贴截图
#include <stdio.h>
int main()
{    int i,j,k=0,l=0,m=0,A[50];
    printf("How many ?\n");
    scanf("%d",&i); 
    for(j=0;j<i;j++) 
    *(A+j)=j+1;
    while(m<i-1)
    {if(A[l]!=0) k++;
     if(k%3==0)
        {A[l]=0;
         k=0;
         m++;}
        l++;
     if(l==i) l=0;    
    }
    while(A[l]==0) l++;
    printf("%d",A[l]);
    return 0;
}

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

2条回答 默认 最新

  • fuill 2022-01-03 10:21
    关注

    试试这个

    #include<stdio.h>
    #include<malloc.h>
    struct node
    
    {
        int num;
    
        struct node *next;
    
    };
    
    struct node *head;
    
    struct node *last;
    
    void cre_list()
    
    {
        head = (struct node *)malloc(sizeof(struct node));
    
        last = head;
    
        head->next = head;
    
    }
    
    void display_node()
    
    {
        struct node *ptr = head->next;
    
        while(ptr != head)
    
        {
            // printf("%d\t",ptr->num);
    
            ptr = ptr->next;
    
        }
    
        //printf("\n");
    
    }
    
    void add_node(int num)
    
    {
        struct node *ptr = (struct node *)malloc(sizeof(struct node));
    
        ptr->num = num;
    
        ptr->next = head;
    
        last->next = ptr;
    
        last = ptr;
    
    }
    
    void rev_node()
    
    {
        struct node *ptr = head;
    
        last->next = head->next;
    
        head = head->next;
    
        free(ptr);
    
    }
    
    void tiren_node()
    
    {
        struct node *ptr = head;
    
        struct node *str = ptr->next;
    
        struct node *qtr = str->next;
    
        while(ptr->next != ptr)
    
        {
            str = ptr->next;
    
            qtr = str->next;
    
            str->next = qtr->next;
    
            ptr = str->next;
    
        }
    
        printf("%d",ptr->num);
    
    }
    
    int main()
    
    {
        int i = 0;
    
        cre_list();
    
        int n;
    
        //printf("please input n:\n");
        printf("How many ?\n");
    
        scanf("%d",&n);
    
        // printf("%d\n",n);
    
        for(i = 1; i <= n; i++)
    
        {
            add_node(i);
    
        }
    
        display_node();
    
        rev_node();
    
        tiren_node();
    
        return 0;
    
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月14日
  • 已采纳回答 3月6日
  • 创建了问题 1月3日

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行