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

文章讨论了两种计算粒子在二维空间中沿特定方向移动的总路径长度的方法:一种是通过模拟粒子运动,另一种是利用数学物理原理分解速度并找到最小公倍数。两种方法分别给出了答案14261800000和28520969829.65。
摘要由CSDN通过智能技术生成

在这里插入图片描述
以下两个解法感觉都靠谱,并且网上的题解每个人答案都不一样,目前无法判断哪个是正确答案。

方法一:模拟
代码参考博客

#include <iostream>
#include <cmath>
#include <vector>
 
using namespace std;
 
int main() {
    const int maxX = 343720;
    const int maxY = 233333;
    const int vx = 15;
    const int vy = 17;
 
    int x = 0, y = 0;
    int time = 0;
    double totalDistance = 0;
    bool xPositive = true, yPositive = true;
 
    while (true) {
    	//根据方向判断是+距离还是-距离
        x += xPositive ? vx : -vx;
        y += yPositive ? vy : -vy;
        time++;
        //移动的距离
        double segmentLength = sqrt(vx * vx+ vy * vy);
        // 检查是否碰到边界并反弹
        if (x > maxX || x < 0) {
            xPositive = !xPositive; // 反转X方向
            x = (x > maxX) ? maxX : 0; // 将坐标调整到边界上
        }
        if (y > maxY || y < 0) {
            yPositive = !yPositive; // 反转Y方向
            y = (y > maxY) ? maxY : 0; // 将坐标调整到边界上
        }
 
        // 累加路径长度
        totalDistance += segmentLength;
 
        // 假设当粒子回到原点时停止模拟
        if (x == 0 && y == 0) {
            break;
        }
    }
 
    // 输出总路径长度
    cout << "总路径为:" << totalDistance << endl;
 
    return 0;
}

答案:14261800000

方法二:数学物理题
把速度分解成x方向和y方向,已知x方向来回一趟的时间是2343720/15,y方向来回一趟的时间是2233333/17,已知小球要回到原点,即x和y方向要同时回到原点,时间就是2343720/15和2233333/17的整数倍,即最小公倍数。就能把时间t求出来,然后乘以速度sqrt(15^2+17^2)就是总路程。

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a = 2*343720/15;
    int b = 2*233333/17;
    int i = 1;
    while(i*a%b!=0){
        i++;
    }
    double t = i*a;
    double s = t*sqrt(pow(15, 2)+pow(17, 2));
    cout<<fixed<<setprecision(2)<<s<<endl;
    return 0;
}

输出:28520969829.65

复习:最大公因数GCA(Greatest Common Divisor)和最小公倍数LCM(Least Common Multiple)

int my_gcd(int a,int b){
    if(a%b==0)return b;
    else return my_gcd(b, a%b);
}

int my_lcm(int a,int b){
    int i=1;
    while(i*a%b!=0)i++;
    return i*a;
}
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蒋的学习笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值