鱼C论坛

 找回密码
 立即注册
查看: 1078|回复: 2

[已解决]有C语言大佬能看看这道题吗

[复制链接]
发表于 2024-3-7 10:09:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
求用C语言,不要用C++
最佳答案
2024-3-7 10:41:19
用 Python 解决的:

  1. def calculate_final_positions(n, m, movements, start_positions):
  2.     # 计算最终位置
  3.     final_positions = []
  4.     for x, y in start_positions:
  5.         for dx, dy in movements:
  6.             x += dx
  7.             y += dy
  8.         final_positions.append((x, y))
  9.     return final_positions

  10. # 示例输入
  11. n, m = 3, 2  # 移动次数和点的数量
  12. movements = [(10, 10), (0, 0), (10, -20)]  # 移动的变化量
  13. start_positions = [(1, -1), (0, 0)]  # 起始位置

  14. # 计算最终位置
  15. final_positions = calculate_final_positions(n, m, movements, start_positions)

  16. # 输出最终位置
  17. for position in final_positions:
  18.     print(position)  # 这会打印每个起始点的最终位置
复制代码


C 语言也可以:

  1. #include <stdio.h>

  2. // 结构体用于存储点的坐标
  3. typedef struct {
  4.     int x;
  5.     int y;
  6. } Point;

  7. // 函数用于计算最终位置
  8. void calculate_final_positions(int n, int m, Point movements[], Point start_positions[]) {
  9.     for (int i = 0; i < m; ++i) {
  10.         Point final_position = start_positions[i];
  11.         for (int j = 0; j < n; ++j) {
  12.             final_position.x += movements[j].x;
  13.             final_position.y += movements[j].y;
  14.         }
  15.         // 打印最终位置
  16.         printf("%d %d\n", final_position.x, final_position.y);
  17.     }
  18. }

  19. int main() {
  20.     int n, m;
  21.     scanf("%d %d", &n, &m); // 读取移动次数和点的数量

  22.     Point movements[n]; // 存储所有移动的数组
  23.     for (int i = 0; i < n; ++i) {
  24.         scanf("%d %d", &movements[i].x, &movements[i].y); // 读取移动的变化量
  25.     }

  26.     Point start_positions[m]; // 存储所有起始位置的数组
  27.     for (int i = 0; i < m; ++i) {
  28.         scanf("%d %d", &start_positions[i].x, &start_positions[i].y); // 读取起始位置
  29.     }

  30.     // 计算并打印所有点的最终位置
  31.     calculate_final_positions(n, m, movements, start_positions);

  32.     return 0;
  33. }
复制代码
屏幕截图 2024-03-07 100846.png
屏幕截图 2024-03-07 100855.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-7 10:09:54 | 显示全部楼层
当然可以帮助你解答C语言的问题!请告诉我你遇到了什么困惑,我会尽力帮你解答。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-7 10:41:19 | 显示全部楼层    本楼为最佳答案   
用 Python 解决的:

  1. def calculate_final_positions(n, m, movements, start_positions):
  2.     # 计算最终位置
  3.     final_positions = []
  4.     for x, y in start_positions:
  5.         for dx, dy in movements:
  6.             x += dx
  7.             y += dy
  8.         final_positions.append((x, y))
  9.     return final_positions

  10. # 示例输入
  11. n, m = 3, 2  # 移动次数和点的数量
  12. movements = [(10, 10), (0, 0), (10, -20)]  # 移动的变化量
  13. start_positions = [(1, -1), (0, 0)]  # 起始位置

  14. # 计算最终位置
  15. final_positions = calculate_final_positions(n, m, movements, start_positions)

  16. # 输出最终位置
  17. for position in final_positions:
  18.     print(position)  # 这会打印每个起始点的最终位置
复制代码


C 语言也可以:

  1. #include <stdio.h>

  2. // 结构体用于存储点的坐标
  3. typedef struct {
  4.     int x;
  5.     int y;
  6. } Point;

  7. // 函数用于计算最终位置
  8. void calculate_final_positions(int n, int m, Point movements[], Point start_positions[]) {
  9.     for (int i = 0; i < m; ++i) {
  10.         Point final_position = start_positions[i];
  11.         for (int j = 0; j < n; ++j) {
  12.             final_position.x += movements[j].x;
  13.             final_position.y += movements[j].y;
  14.         }
  15.         // 打印最终位置
  16.         printf("%d %d\n", final_position.x, final_position.y);
  17.     }
  18. }

  19. int main() {
  20.     int n, m;
  21.     scanf("%d %d", &n, &m); // 读取移动次数和点的数量

  22.     Point movements[n]; // 存储所有移动的数组
  23.     for (int i = 0; i < n; ++i) {
  24.         scanf("%d %d", &movements[i].x, &movements[i].y); // 读取移动的变化量
  25.     }

  26.     Point start_positions[m]; // 存储所有起始位置的数组
  27.     for (int i = 0; i < m; ++i) {
  28.         scanf("%d %d", &start_positions[i].x, &start_positions[i].y); // 读取起始位置
  29.     }

  30.     // 计算并打印所有点的最终位置
  31.     calculate_final_positions(n, m, movements, start_positions);

  32.     return 0;
  33. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-8 21:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表