Chinaunix首页 | 论坛 | 博客
  • 博客访问: 473673
  • 博文数量: 59
  • 博客积分: 345
  • 博客等级: 二等列兵
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-18 22:44
个人简介

to be myself

文章分类

全部博文(59)

文章存档

2017年(5)

2013年(47)

2012年(3)

2011年(4)

分类: C/C++

2013-03-02 15:55:11


  1. 没有加注释,简单的链表。
  2. n=1的时候较特殊,一张牌只能作为Remaining card,也就是Discarded cards后面无需输出。
  3. 但题目的意思说"The following operation is performed as long as there are at least two cards in the deck.",提交的时候却要考虑等于1的情况.否则就WA.
  4. #include <stdio.h>
  5. #include <stdlib.h>

  6. typedef struct
  7. {
  8.     int num;
  9.     struct Node *next;
  10. }Node;

  11. int main()
  12. {
  13.     int n, m, num;
  14.     Node *p, *head, *q, *tail;
  15.     while (scanf("%d", &n), n!=0)
  16.     {
  17.         num = 0;
  18.         m = n;
  19.         p = (Node *)malloc(sizeof(Node));
  20.         p->next = NULL;
  21.         n--;
  22.         head = p;
  23.         p->num = ++num;
  24.         while (n--)
  25.         {
  26.             q = (Node *)malloc(sizeof(Node));
  27.             q->next = NULL;
  28.             q->num = ++num;
  29.             p->next = q;
  30.             p = q;
  31.         }
  32.         tail = p;
  33.         p = head;
  34.         printf("Discarded cards:");
  35.         while (m-- > 1)
  36.         {
  37.             printf(" %d", p->num);
  38.             if (m > 1)
  39.             {
  40.                 printf(",");
  41.             }
  42.             if (p->next != NULL)
  43.             {
  44.                 tail->next = p->next;
  45.                 tail = p->next;
  46.                  p = tail->next;
  47.             }
  48.         }
  49.         printf("nRemaining card: %dn", p->num);
  50.     }
  51.     return 0;
  52. }
  53. //程序完
2010-08-17 21:06 发表于百度空间,今搬至CU。
阅读(1251) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~