翻牌问题

~K 2019-04-19 11:17:10
手上有一叠牌, 把第一张牌(即位于顶面的牌)开始从上往下依次编号为1~n. 重复以下操作: 把第一张牌扔掉, 然后把新的第一张牌放到整叠牌的最后. 问最后扔掉的是哪一张?
...全文
209 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
636f6c696e 2019-04-19
  • 打赏
  • 举报
回复
仅供参考,输入输出需要按照题设要求改改:
#include <stdio.h>
#include <stdlib.h>

typedef struct tag_list_node
{
    void *next;
    int num;
}list_node_s;

void display_list(list_node_s* head)
{     
    if (NULL == head)
    {
        return;
    }
    
    list_node_s *cur = head;
    printf("list member:");
    while(cur != NULL)
    {
        printf(" %d", cur->num);
        cur = cur->next;
    }
    printf("\n");
}

list_node_s* init_list(int num)
{
    list_node_s *head;
    list_node_s *cur;
    list_node_s *next;
    int i;
    
    head = (list_node_s *)malloc(sizeof(list_node_s));
    head->num = 1;
    head->next = NULL;
    
    cur = head;
    for (i = 2; i <= num; i++)
    {
        next = (list_node_s *)malloc(sizeof(list_node_s));
        next->num = i;
        cur->next = next;
        cur = next;
    }
    
    cur->next = NULL; 
    display_list(head);

    return head;    
}


list_node_s* remove_node_from_list_head(list_node_s* head)
{
    if (NULL == head || NULL == head->next)
    {
        return NULL;
    }
    
    list_node_s *new_head = head->next;
    free(head);
    return new_head;
}

void insert_node_from_list_tail(list_node_s* head, int num)
{
    list_node_s *cur = head;
    list_node_s *node;
    
    while (cur->next != NULL)
    {
        cur = cur->next;
    }
    
    node = (list_node_s *)malloc(sizeof(list_node_s));
    node->num = num;
    node->next = NULL;
    cur->next = node;
}

int list_proc(list_node_s* head)
{
    list_node_s *cur_head = head;
    
    while (cur_head->next != NULL)
    {
        cur_head = remove_node_from_list_head(cur_head);
        insert_node_from_list_tail(cur_head, cur_head->num);
        cur_head = remove_node_from_list_head(cur_head);
        display_list(cur_head);
    }
    
    return cur_head->num;
}

int main()
{
    list_node_s *head;
    int num;
    int last_num;
    
    scanf("%d", &num);
    head = init_list(num);
    last_num = list_proc(head);
    printf("%d\n", last_num);
    
    return 0;
}
636f6c696e 2019-04-19
  • 打赏
  • 举报
回复
不难吧,用个链表数据结构就能做啊
  • 打赏
  • 举报
回复

#include <stdio.h>

int main()
{
int T, n, x, y;

scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
if (y = 0, (x = n) == 1) puts("1");
else
{
while (x > (1 << y)) x -= (1 << y++);
printf("%d\n", x != 1 ? (x - 1) * 2 : n);
}
}

return 0;
}

  • 打赏
  • 举报
回复
这题可以直接计算:

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧