游戏描述:
30 个人在一条船上,超载,需要 15 人下船。
于是人们排成一队,排队的位置即为他们的编号。
报数,从 1 开始,数到 9 的人下船。
如此循环,直到船上仅剩 15 人为止,问都有哪些编号的人下船了呢?
#include<stdio.h> #include<stdlib.h> struct shipper { int num; struct shipper*
p_num; }; typedef struct shipper Shipper; int main() { int i; int count;
count=0; Shipper* ship_temp; Shipper* ship_head; Shipper* ship_ele; ship_temp =
(Shipper *)malloc(sizeof(Shipper)); ship_head=ship_temp; for(i=1;i<=30;i++) {
ship_ele = (Shipper *)malloc(sizeof(Shipper)); //malloc ship_ele->num=i;
ship_temp->p_num=ship_ele; //temp先作为头节点 ,然后temp指向下一节点 ship_temp=ship_ele; }
/***********链表首尾衔接**********/ ship_temp->p_num=ship_head->p_num;
/***********验证循环链表**********/ while(0) { printf("%d\n",ship_head->p_num->num) ;
ship_head->p_num=ship_head->p_num->p_num; } ship_temp=ship_head->p_num;
while(count<=14) { count=count+1; for(i=1;i<=7;i++) {
ship_temp=ship_temp->p_num; if(i==7) {
printf("下船人员编号为:%d\n",ship_temp->p_num->num);
ship_temp->p_num=ship_temp->p_num->p_num; } } ship_temp=ship_temp->p_num; } }
今日推荐