蓝桥杯2024年第十五届省赛真题-小球反弹

种地  论坛元老 | 2025-4-20 07:51:45 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1845|帖子 1845|积分 5535


以下两个解法感觉都靠谱,并且网上的题解每个人答案都不一样,目前无法判定哪个是正确答案。
方法一:模拟
代码参考博客
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. using namespace std;
  5. int main() {
  6.     const int maxX = 343720;
  7.     const int maxY = 233333;
  8.     const int vx = 15;
  9.     const int vy = 17;
  10.     int x = 0, y = 0;
  11.     int time = 0;
  12.     double totalDistance = 0;
  13.     bool xPositive = true, yPositive = true;
  14.     while (true) {
  15.             //根据方向判断是+距离还是-距离
  16.         x += xPositive ? vx : -vx;
  17.         y += yPositive ? vy : -vy;
  18.         time++;
  19.         //移动的距离
  20.         double segmentLength = sqrt(vx * vx+ vy * vy);
  21.         // 检查是否碰到边界并反弹
  22.         if (x > maxX || x < 0) {
  23.             xPositive = !xPositive; // 反转X方向
  24.             x = (x > maxX) ? maxX : 0; // 将坐标调整到边界上
  25.         }
  26.         if (y > maxY || y < 0) {
  27.             yPositive = !yPositive; // 反转Y方向
  28.             y = (y > maxY) ? maxY : 0; // 将坐标调整到边界上
  29.         }
  30.         // 累加路径长度
  31.         totalDistance += segmentLength;
  32.         // 假设当粒子回到原点时停止模拟
  33.         if (x == 0 && y == 0) {
  34.             break;
  35.         }
  36.     }
  37.     // 输出总路径长度
  38.     cout << "总路径为:" << totalDistance << endl;
  39.     return 0;
  40. }
复制代码
答案:14261800000
方法二:数学物理题
把速度分解成x方向和y方向,已知x方向来回一趟的时间是2343720/15,y方向来回一趟的时间是2233333/17,已知小球要回到原点,即x和y方向要同时回到原点,时间就是2343720/15和2233333/17的整数倍,即最小公倍数。就能把时间t求出来,然后乘以速度sqrt(15^2+17^2)就是总路程。
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4.     int a = 2*343720/15;
  5.     int b = 2*233333/17;
  6.     int i = 1;
  7.     while(i*a%b!=0){
  8.         i++;
  9.     }
  10.     double t = i*a;
  11.     double s = t*sqrt(pow(15, 2)+pow(17, 2));
  12.     cout<<fixed<<setprecision(2)<<s<<endl;
  13.     return 0;
  14. }
复制代码
输出:28520969829.65
复习:最大公因数GCA(Greatest Common Divisor)和最小公倍数LCM(Least Common Multiple)
  1. int my_gcd(int a,int b){
  2.     if(a%b==0)return b;
  3.     else return my_gcd(b, a%b);
  4. }
  5. int my_lcm(int a,int b){
  6.     int i=1;
  7.     while(i*a%b!=0)i++;
  8.     return i*a;
  9. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

种地

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表