码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 每日一道算法题


    题目

    坐标移动_牛客题霸_牛客网 (nowcoder.com)

    c语言

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. //#include
    4. int main()
    5. {
    6. char str[10001] = { 0 };
    7. //strcpy(str, "A10;S20;W10;D30;X;A1A;B10A11;;A10;");
    8. scanf("%s", str);
    9. char *p = str;
    10. int x = 0;
    11. int y = 0;
    12. int step = 0;
    13. char d = 0; //方向
    14. while (*p != '\0')
    15. {
    16. //匹配方向
    17. if (!d)
    18. {
    19. if (*p== 'A' || *p == 'D' || *p == 'W' || *p == 'S')
    20. {
    21. d = *p;
    22. *p++;
    23. continue;
    24. }
    25. else
    26. {
    27. while (*p != '\0' && *p != ';')
    28. {
    29. *p++;
    30. }
    31. }
    32. }
    33. //匹配到方向
    34. if (d)
    35. {
    36. if (*p >= '0' && *p <= '9')
    37. {
    38. step *= 10;
    39. step += (*p - '0');
    40. }
    41. else if (*p == ';')
    42. {
    43. if (d == 'A') x -= step;
    44. else if (d == 'D') x += step;
    45. else if (d == 'W') y += step;
    46. else if (d == 'S') y -= step;
    47. step = 0;
    48. d = 0;
    49. }
    50. else
    51. {
    52. step = 0;
    53. d = 0;
    54. }
    55. }
    56. p++;
    57. }
    58. printf("%d,%d", x, y);
    59. return 0;
    60. }

    c++

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. int main()
    7. {
    8. //"A10;S20;W10;D30;X;A1A;B10A11;;A10;"
    9. string s, c;
    10. //getline(cin, s);
    11. //cout<
    12. while (getline(cin, s))
    13. {
    14. stringstream ss(s);
    15. pair<int, int> x_y(0, 0);
    16. while (getline(ss, c, ';'))
    17. {
    18. //cout << c << endl;
    19. if (c.empty()) continue;
    20. string _ = c.substr(1);
    21. //cout << _ << endl;
    22. if (regex_match(_, regex("[0-9]*")))
    23. {
    24. switch (c[0])
    25. {
    26. case 'A':x_y.first -= stoi(_); break;
    27. case 'D':x_y.first += stoi(_); break;
    28. case 'W':x_y.second += stoi(_); break;
    29. case 'S':x_y.second -= stoi(_); break;
    30. default:break;
    31. }
    32. }
    33. }
    34. cout << x_y.first << ',' << x_y.second;
    35. }
    36. return 0;
    37. }

    python

    1. s=input()
    2. s=s.split(';')
    3. x=y=0
    4. for _ in s:
    5. if not _:
    6. continue
    7. try:
    8. if _[0]=='A':
    9. x-=int(_[1:])
    10. elif _[0]=='D':
    11. x+=int(_[1:])
    12. elif _[0]=='W':
    13. y+=int(_[1:])
    14. elif _[0]=='S':
    15. y-=int(_[1:])
    16. else:
    17. continue
    18. except:
    19. continue
    20. print(f"{x},{y}")

  • 相关阅读:
    tensorflow卷积层操作
    从 Vue2向Vue3的迁移
    凌鲨整体架构
    基于springboot实现自习室预订系统的设计与实现项目【项目源码+论文说明】
    vue3 prop验证类型
    数据库习题
    PyQt5快速开发与实战 9.5 PyQtGraph在PyQt中的应用 && 9.6 Plotly在PyQt中的应用
    一起学数据结构(11)——快速排序及其优化
    CC2642 OAD文件合成
    夸克扫描王识别精度领跑行业 愿携手各方伙伴探索AIGC应用新范式
  • 原文地址:https://blog.csdn.net/weixin_65816128/article/details/139699685
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号