坐标移动

题目:开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。

(https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29?tpId=37&tqId=21240&tPage=1&rp=&ru=%2Fta%2Fhuawei&qru=%2Fta%2Fhuawei%2Fquestion-ranking)

 思路:先检查当前组数据是否合法,再将其移动值(字符串)转化成数值,再判断并计算。

心得体会:一定要注意细节,特别是判断语句, 

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

#define MAX     10000

void move(char,int);

int result[2] = {0};

int main()
{
    char str[MAX] = {0},tmp[3];
    int i,j,len,step;

    step = 0;
   
    while(fgets(str,MAX,stdin))
    {
    	len = strlen(str);
		str[len--] = '\0';
	
    	for(i = 0;i < len;)
    	{
    	
    	    memset(tmp,0,3);
            if(i == 0)
                {
                	j = i;
                	i =1;
                }
            else
                {
                    while(str[i++] != ';');
                    j = i ;
                }
            if((str[j] == 'A' 
                || str[j] == 'D' 
                || str[j] == 'W' 
                || str[j] == 'S'))
                if((str[j+1] >= '0' && str[j+1] <= '9'))
                    if((str[j+2] >= '0' && str[j+2] <= '9'))
                    {
                        if(str[j+3] == ';')
                        {
                            tmp[0] = str[j+1];
                            tmp[1] = str[j+2];
                        }
                        else
                            continue;
                    }
                    else if(str[j+2] == ';')
                        {
                        	  tmp[0] = str[j+1];
                        }
                    else
                        continue;
                else
                    continue;
            else 
                continue;

                step = atoi(tmp);
                move(str[j],step);
    	    }

        printf("%d,%d\n",result[0],result[1]);
        memset(result,0,sizeof(result));
        //result[0] = 0;
        //result[1] = 0;    	
    }
    return 0;
}

void move(char dir,int step)
{
    switch(dir)
    {
    	case 'A':
        	result[0] -= step;
        	break;
   		case 'D':
        	result[0] += step;
        	break;
    	case 'W':
        	result[1] += step;
        	break;
    	case 'S':
        	result[1] -= step;
        	break;
        default:
        	break;
     }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值