3.10 杭电复试题2012

1.写一程序将十进制数转变为十六进制数输出。每行输入一个十进制数,当输入数位0时,输出结束。

#include<stdio.h>
#include<string.h>
int n,i;
char sixteen[10000];
void main()
{
    while(scanf("%d",&n)!=EOF&&n)
    {
        int k=0;
        memset(sixteen,'\0',sizeof(sixteen));
        while(n)
        {
            if(n%16<10)
                sixteen[k++]=n%16+'0';
            else
                sixteen[k++]=n%16+55;
            n/=16;
        }
        for(i=k-1;i>=0;i--)
            printf("%c",sixteen[i]);
        printf("\n");
    }
}

2.Worm is an old computer game.There are many versions,but all involve maneuvering a "worm"around the screen,trying to avoid running the worm into itself or an obstacle.

  We'll simulate a very simplified version here.The game will be played on a 50×50 board,numbered so that the square at the upper left is numbered(1,1).The worm is initially a string of 20 connected squares.Connected squares are adjacent horizontally or vertically.The worm starts stretched out horizontally in positions(25,11)through(25,30),with the head of the worm at (25,30).The worm can move either East(E),West(W),North(N) or South(S),but will never move back on itself.So,in the initial position ,a W move is not possible.Thus the only two squares occupied by the worm that change in any move are its head and tail.Note that the head of the worm can move to the square just vacated by the worm's tail.

  You will be given a series of moves and will simulate the moves until either the worm runs into itself,the worm runs off the board,or the worm successfully negotiates its list of moves.In the first two cases you should ignore the remaining moves in the list.

Input

There will be mutiple problems instances.The input for each problem instance will be on two lines.The first line is an integer n(n<100)indicating the number of moves to follow.(A value of n=0 indicates end of input.)The next line contains n characters(either E,W,N or S)with no spaces separating the letters,indicating the sequence of moves.

Output 

Generate one line of output for each problem instance.The output line should be one of the follow three:

The worm ran into itself on move m.

The worm ran off the board on move m.

The worm successfully made all m moves.

where m is for you to determine and the first move is move1.

Sample Input

18

NWWWWWWWWWWSESSSWS

20

SSSWWNENNNNNWWWWSSSS

30

EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

13

SWWWWWWWWWNEE

0

Sample Output

The worm successfully made all 18 moves.

The worm ran into itself on move 9.

The worm ran off the board on move 21.

The worm successfully made all 13 moves.

#include<stdio.h>
#include<string.h>
int matrix[52][52];
char route[10000];
int n,f,i;
void find_tail(int *t1,int *t2,int step)
{
    if(step<20)
        *t2=*t2+1;
    else
    {
        if(route[step-20]=='E')
            *t2=*t2+1;
        if(route[step-20]=='N')
            *t1=*t1-1;
        if(route[step-20]=='W')
            *t2=*t2-1;
        if(route[step-20]=='S')
            *t1=*t1+1;
    }
}
void main()
{
    while(scanf("%d",&n)!=EOF&&n)
    {
        getchar();
        int head1=25,head2=30,tail1=25,tail2=11;
        memset(matrix,0,sizeof(matrix));
        for(i=11;i<=30;i++)
            matrix[25][i]=1;
        for(i=0;i<n;i++)
            scanf("%c",&route[i]);
        f=0;
        for(i=0;i<n;i++)
        {
            if(route[i]=='E')
            {
                if(matrix[head1][head2+1]==1)
                {
                    if(head2+1==tail2&&head1==tail1)
                        break;
                    printf("The worm ran into itself on move %d.\n",i+1);
                    f=1;
                    break;
                }
                if(head2==50)
                {
                    printf("The worm ran off the board on move %d.\n",i+1);
                    f=2;
                    break;
                }
                matrix[head1][++head2]=1;
                matrix[tail1][tail2]=0;
                find_tail(&tail1,&tail2,i+1);
            }
            if(route[i]=='W')
            {
                if(matrix[head1][head2-1]==1)
                {
                    if(head2-1==tail2&&tail1==head1)
                        break;
                    printf("The worm ran into itself on move %d.\n",i+1);
                    f=1;
                    break;
                }
                if(head2==1)
                {
                    printf("The worm ran off the board on move %d.\n",i+1);
                    f=2;
                    break;
                }
                matrix[head1][--head2]=1;
                matrix[tail1][tail2]=0;
                find_tail(&tail1,&tail2,i+1);
            }
            if(route[i]=='S')
            {
                if(matrix[head1+1][head2]==1)
                {
                    if(head1+1==tail1&&head2==tail2)
                        break;
                    printf("The worm ran into itself on move %d.\n",i+1);
                    f=1;
                    break;
                }
                if(head1==50)
                {
                    printf("The worm ran off the board on move %d.\n",i+1);
                    f=2;
                    break;
                }
                matrix[++head1][head2]=1;
                matrix[tail1][tail2]=0;
                find_tail(&tail1,&tail2,i+1);
            }
            if(route[i]=='N')
            {
                if(matrix[head1-1][head2]==1)
                {
                    if(head1-1==tail1&&head2==tail2)
                        break;
                    printf("The worm ran into itself on move %d.\n",i+1);
                    f=1;
                    break;
                }
                if(head1==1)
                {
                    printf("The worm ran off the board on move %d.\n",i+1);
                    f=2;
                    break;
                }
                matrix[--head1][head2]=1;
                matrix[tail1][tail2]=0;
                find_tail(&tail1,&tail2,i+1);
            }
            if(f!=0)
                break;
    //        printf("(%d,%d)(%d,%d)\n",head1,head2,tail1,tail2);
        }
        if(!f)
            printf("The worm successfully made all %d moves.\n",n);
    }
}

这其实大概就是讲了一个贪吃蛇的游戏,让你判断这条长20格的贪吃蛇会不会撞墙,会不会撞到自己,还是顺利行动了。

就用一个矩阵来表示,1表示贪吃蛇身子所在的位置,0表示贪吃蛇不在的位置,如果头撞到了1所在的位置就是撞到了自己,如果头超出了50*50的格子,就是撞墙了。就是读入一格方向,头往那个所在方向的格子记1,尾所在的位置变0.

我中间出的错就是 没注意如果贪吃蛇的头下一步要碰到他的尾的话,其实是没事的,因为头跟尾一起动的,就是第四组数据。

总结这题真的不难,但是很多小细节真的很容易出错。。所以做了好久好久。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值