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

以下两个解法感觉都靠谱,并且网上的题解每个人答案都不一样,目前无法判断哪个是正确答案。

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

cpp 复制代码
#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方向来回一趟的时间是2 233333/17,已知小球要回到原点,即x和y方向要同时回到原点,时间就是2343720/15和2233333/17的整数倍,即最小公倍数。就能把时间t求出来,然后乘以速度sqrt(15^2+17^2)就是总路程。

cpp 复制代码
#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)

cpp 复制代码
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;
}
相关推荐
元亓亓亓3 天前
LeetCode热题100--105. 从前序与中序遍历序列构造二叉树--中等
算法·leetcode·职场和发展
测试老哥3 天前
Selenium 使用指南
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
仙俊红3 天前
LeetCode每日一题,20250914
算法·leetcode·职场和发展
前端小超超3 天前
capacitor配置ios应用图标不同尺寸
ios·蓝桥杯·cocoa
睡不醒的kun3 天前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌4 天前
LeetCode 1446.连续字符
算法·leetcode·职场和发展
武子康4 天前
AI-调查研究-76-具身智能 当机器人走进生活:具身智能对就业与社会结构的深远影响
人工智能·程序人生·ai·职场和发展·机器人·生活·具身智能
Nan_Shu_6144 天前
Web前端面试题(1)
前端·面试·职场和发展
YuTaoShao4 天前
【LeetCode 每日一题】3000. 对角线最长的矩形的面积
算法·leetcode·职场和发展
007php0074 天前
Redis高级面试题解析:深入理解Redis的工作原理与优化策略
java·开发语言·redis·nginx·缓存·面试·职场和发展