# 2013级测试赛 - F 播放器

654人阅读 评论(0)

Description

Input

PRE 表示按下了“上一首”按钮。

PLAY x 其中x为一个整数( 0 < x <= n )，表示按下了播放列表中的第x首歌。

NEXT 表示按下了“下一首”按钮。

Output

Sample Input

1
5 10
PRE
NEXT
PLAY 5
NEXT
PLAY 5
PLAY 3
NEXT
PRE
PRE
PRE


Sample Output

1
2
5
5
5
3
4
3
5
2


Hint

这个题也算我对链表的第一次应用，首先摆出开始回馈编译错误的代码

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
int play;
struct lis *next;
struct lis *last;
} lis;

int main()
{
int ii = 0,nn = 0;
int n = 0,m = 0;
int len = 0,mp3 = 0,quan = 0;
char c[10],*p;
lis *head = NULL;
lis *ne,*t;  //NEW

freopen ("1.txt","r",stdin);

scanf ("%d",&nn);
for (ii = 0; ii < nn; ii++)
{
head = malloc (sizeof (lis) * 1);
ne->play = 1;
ne->next = NULL;
ne->last = NULL;

scanf ("%d %d",&n,&m);
getchar ();
while(m--)
{
gets (c);
p = c;
len = 0;
while (*p != '\0')
{
len++;
p++;
}

if(len >= 6)
{
p--;
quan = 1;
mp3 = 0;
while (*p != ' ')
{
mp3 += ((int)*p - 48) * quan;
quan *= 10;
p--;
}

if (mp3 != t->play)
{
t = malloc (sizeof (lis) * 1);
ne->next = t;
t->last = ne;
ne = ne->next;
ne->play = mp3;
ne->next = NULL;
}
}
else if (len == 4)
{
if (ne->play < n)
{
t = malloc (sizeof (lis) * 1);
t->play = ne->play + 1;
ne->next = t;
t->last = ne;
ne = ne->next;
ne->next = NULL;
}
}
else if (len == 3)
{
if (ne != head)
{
free (ne);
ne = ne->last;
ne->next = NULL;
}
}
printf ("%d\n",ne->play);
}
}

return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct lis
{
int play;
struct lis *next;
struct lis *last;
};

int main()
{
int ii = 0,nn = 0;
int n = 0,m = 0;
int len = 0,mp3 = 0,quan = 0;
char c[10],*p;
struct lis *head = NULL;
struct lis *ne,*t;  //NEW

//freopen ("1.txt","r",stdin);

scanf ("%d",&nn);
for (ii = 0; ii < nn; ii++)
{
head = (struct lis *)malloc (sizeof (struct lis) * 1);
ne->play = 1;
ne->next = NULL;
ne->last = NULL;

t = NULL;//将t指空

scanf("%d %d%*c",&n,&m);
//getchar ();
while(m--)
{
gets (c);
p = c;
len = 0;
//这里可以用 strlen()
//或者换一个思路 用strcmp()
//在课本上貌似都有 可以去看一下
while (*p != '\0')
{
len++;
p++;
}
//if(strcmp("PLAY",c) == 0)
if(len >= 6)
{
p--;
quan = 1;
mp3 = 0;
while (*p != ' ')
{
mp3 += ((int)*p - 48) * quan;
quan *= 10;
p--;
}
//scanf("%d",&mp3);
if (mp3 > n)
mp3 = n;

//printf("mp3 = %d\n",mp3);

//此处t有可能没有分配内存
//比如第一组输入即为 PLAY 5
//if (mp3 != t->play)
if(mp3 != ne->play)//逻辑或与 的 短路原则
{
t = (struct lis *)malloc (sizeof (struct lis) * 1);
ne->next = t;
t->last = ne;
ne = ne->next;
ne->play = mp3;
ne->next = NULL;
}
}
else if (len == 4 || c[0] == 'N')
//else if(strcmp("NEXT",c) == 0)
{
if (ne->play < n)
{
t = (struct lis *)malloc (sizeof (struct lis) * 1);
t->play = ne->play + 1;
ne->next = t;
t->last = ne;
ne = ne->next;
ne->next = NULL;
}
}
else if (len == 3 || c[0] == 'P')
// else if(strcmp("PRE",c) == 0)
{
if (ne != head)
{
t = ne;
ne = ne->last;
free (t);
ne->next = NULL;
}
}
printf ("%d\n",ne->play);
// printf ("\t%#x\n",&ne);
}
}
return 0;
}


#include <stdio.h>
#include <stdlib.h>

struct lis
{
int play;
struct lis *next;
struct lis *last;
};

int main()
{
int ii = 0,nn = 0;
int n = 0,m = 0;
int len = 0,mp3 = 0,quan = 0;
char c[10],*p;
struct lis *head = NULL;
struct lis *ne,*t;  //NEW

//freopen ("1.txt","r",stdin);

scanf ("%d",&nn);
for (ii = 0; ii < nn; ii++)
{
head = (struct lis *)malloc (sizeof (struct lis) * 1);
ne->play = 1;
ne->next = NULL;
ne->last = NULL;
//第一个错误没有把t初始化

scanf ("%d %d",&n,&m);
getchar ();
while(m--)
{
gets (c);
p = c;
len = 0;
while (*p != '\0')
{
len++;
p++;
}

if(len >= 6)
{
p--;
quan = 1;
mp3 = 0;
while (*p != ' ')
{
mp3 += ((int)*p - 48) * quan;
quan *= 10;
p--;
}
if (mp3 > n)
mp3 = n;

if (mp3 != ne->play)
{
t = (struct lis *)malloc (sizeof (struct lis) * 1);
ne->next = t;
t->last = ne;
ne = ne->next;
ne->play = mp3;
ne->next = NULL;
}
}
else if (len == 4)
{
if (ne->play < n)
{
t = (struct lis *)malloc (sizeof (struct lis) * 1);
t->play = ne->play + 1;
ne->next = t;
t->last = ne;
ne = ne->next;
ne->next = NULL;
}
}
else if (len == 3)
{
if (ne != head)
{
t = ne;
ne = ne->last;
free (t);
ne->next = NULL;
}
}
printf ("%d\n",ne->play);
}
}

return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：155384次
• 积分：3514
• 等级：
• 排名：第9350名
• 原创：200篇
• 转载：6篇
• 译文：0篇
• 评论：24条
盯着他们点→_→
最新评论