69,369
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#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;
}