【算法入门赛】A.坐标变换(推荐学习)C++题解与代码

比赛链接:https://www.starrycoding.com/contest/8

题目描述

武汉市可以看做一个二维地图。

e e e掌握了一项特异功能,他可以“瞬移”,每次瞬移需要分别设定 x x x y y y的偏移量 d x dx dx d y dy dy,瞬移完成后位置会从 ( x , y ) (x, y) (x,y)变为 ( x + d x , y + d y ) (x + dx, y + dy) (x+dx,y+dy)

现在已知牢 e e e初始在 ( 0 , 0 ) (0, 0) (0,0),并且进行了 n n n次瞬移,给出每次瞬移的 d x dx dx d y dy dy,请问牢 e e e最终位置在哪里?

输入格式

第一行一个整数 T ( 1 ≤ T ≤ 100 ) T(1 \le T \le 100) T(1T100)表示样例个数。

对于每一个样例:

第一行 1 1 1个整数 n ( 1 ≤ n ≤ 1 0 5 ) n(1 \le n \le 10^5) n(1n105)

接下来 n n n行,每行描述一次“瞬移”,两个整数表示 d x , d y ( − 1 0 3 ≤ d x , d y ≤ 1 0 3 ) dx, dy(-10^3 \le dx, dy \le 10^3) dx,dy(103dx,dy103)

数据保证 1 ≤ ∑ n ≤ 2 × 1 0 5 1 \le \sum n \le 2 \times 10^5 1n2×105

输出格式

对于每组测试样例,两个整数 x , y x, y x,y表示牢 e e e最终位置 ( x , y ) (x, y) (x,y)

输入样例1

7
1
10 8
3
6 -9
1 3
-3 0
4
-1 -5
9 3
-7 8
-10 8
3
-2 -10
-6 2
-8 6
2
-10 2
-4 9
1
-4 -1
2
6 -9
4 4

输出样例1

10 8
4 -6
-9 14
-16 -2
-14 11
-4 -1
10 -5

题解

语法题,考察输入输出、循环结构、数学运算。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll p = 998244353;
const int N = 2e5 + 9;

void solve()
{
    int n;
    cin >> n;
    ll x = 0, y = 0;
    for (int i = 1; i <= n; ++i)
    {
        ll dx, dy;
        cin >> dx >> dy;
        x += dx, y += dy;
    }
    cout << x << ' ' << y << '\n';
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _;
    cin >> _;
    while (_--)
        solve();
    return 0;
}

StarryCoding是面向计算机专业学生的综合学习与刷题平台,欢迎同学们的加入!
百度搜索:StarryCoding

在这里插入图片描述

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-11 10:50:11       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-11 10:50:11       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-11 10:50:11       82 阅读
  4. Python语言-面向对象

    2024-05-11 10:50:11       91 阅读

热门阅读

  1. 2024年记录

    2024-05-11 10:50:11       33 阅读
  2. sql分页查询

    2024-05-11 10:50:11       34 阅读
  3. pytorch 梯度更新过程

    2024-05-11 10:50:11       26 阅读
  4. PyTorch与深度学习:从入门到精通

    2024-05-11 10:50:11       30 阅读
  5. pytorch2ONNX时,AdaptiveAvgPool2d的相关问题

    2024-05-11 10:50:11       23 阅读
  6. 【python】Flask开发感悟

    2024-05-11 10:50:11       24 阅读
  7. 【软考】scrum的步骤

    2024-05-11 10:50:11       29 阅读
  8. 【C++】每日一题 103 二叉树的锯齿形层序遍历

    2024-05-11 10:50:11       34 阅读
  9. K8S 删除pod的正确步骤

    2024-05-11 10:50:11       42 阅读
  10. 500行代码实现贪吃蛇(2)

    2024-05-11 10:50:11       23 阅读
  11. 右键使用VSCode打开文件/文件夹目录

    2024-05-11 10:50:11       33 阅读
  12. [openwrt-21.02]MT7981+MT7976 WiFi debug指令

    2024-05-11 10:50:11       53 阅读
  13. 图像处理、计算机视觉和深度学习,区别与联系

    2024-05-11 10:50:11       30 阅读