编程
工具
私人
其他
C++#include <vector>
#include <cmath>
struct Ball {
float x, y; // 当前位置
float vx, vy; // 当前速度
float radius; // 半径
};
void updateBalls(std::vector<Ball>& balls, float gravity) {
const float boundaryLeft = -450.0f, boundaryRight = 450.0f;
const float boundaryTop = -250.0f, boundaryBottom = 250.0f;
const float dampening = 0.8f;
const float restitution = 1.0f; // 完全弹性碰撞
// 计算预期新位置并应用重力
std::vector<std::pair<float, float>> newPositions;
for (auto& ball : balls) {
ball.vy += gravity;
newPositions.emplace_back(ball.x + ball.vx, ball.y + ball.vy);
}
// 处理小球间碰撞
for (size_t i = 0; i < balls.size(); ++i) {
for (size_t j = i + 1; j < balls.size(); ++j) {
Ball& b1 = balls[i];
Ball& b2 = balls[j];
auto& pos1 = newPositions[i], &pos2 = newPositions[j];
// 计算几何参数
const float dx = pos2.first - pos1.first;
const float dy = pos2.second - pos1.second;
const float distSq = dx*dx + dy*dy;
const float sumRadius = b1.radius + b2.radius;
const float minDistSq = sumRadius * sumRadius;
if (distSq == 0 || distSq > minDistSq) continue;
// 碰撞法向量计算
const float dist = sqrt(distSq);
const float nx = dx / dist, ny = dy / dist;
// 相对速度检测
const float vRelX = b2.vx - b1.vx;
const float vRelY = b2.vy - b1.vy;
const float vRelDotN = vRelX * nx + vRelY * ny;
if (vRelDotN >= 0) continue; // 分离状态不处理
// 质量计算(与半径平方成正比)
const float m1 = b1.radius * b1.radius;
const float m2 = b2.radius * b2.radius;
// 计算冲量系数
const float j = -(1 + restitution) * vRelDotN / (1/m1 + 1/m2);
// 更新速度
const float dvx1 = (j * nx) / m1;
const float dvy1 = (j * ny) / m1;
const float dvx2 = -(j * nx) / m2;
const float dvy2 = -(j * ny) / m2;
b1.vx += dvx1;
b1.vy += dvy1;
b2.vx += dvx2;
b2.vy += dvy2;
// 位置修正(按质量反比分摊重叠量)
const float overlap = sumRadius - dist;
const float ratio1 = m2 / (m1 + m2);
const float ratio2 = m1 / (m1 + m2);
pos1.first -= nx * overlap * ratio1;
pos1.second -= ny * overlap * ratio1;
pos2.first += nx * overlap * ratio2;
pos2.second += ny * overlap * ratio2;
}
}
// 处理边界碰撞
for (size_t i = 0; i < balls.size(); ++i) {
Ball& ball = balls[i];
auto& pos = newPositions[i];
if (pos.first - ball.radius < boundaryLeft) {
pos.first = boundaryLeft + ball.radius;
ball.vx = -ball.vx * dampening;
} else if (pos.first + ball.radius > boundaryRight) {
pos.first = boundaryRight - ball.radius;
ball.vx = -ball.vx * dampening;
}
if (pos.second - ball.radius < boundaryTop) {
pos.second = boundaryTop + ball.radius;
ball.vy = -ball.vy * dampening;
} else if (pos.second + ball.radius > boundaryBottom) {
pos.second = boundaryBottom - ball.radius;
ball.vy = -ball.vy * dampening;
}
}
// 更新最终位置
for (size_t i = 0; i < balls.size(); ++i) {
balls[i].x = newPositions[i].first;
balls[i].y = newPositions[i].second;
}
}
C++#include<bits/stdc++.h>
using namespace std;
double p,dp1[100010],dp2[100010],ans;
int n,m;
int main(){
cin>>n>>m>>p;n-=m*5;
dp1[0]=dp2[n-4]=dp2[n-3]=dp2[n-2]=dp2[n-1]=1;
for(int i=0;i<=n;i++)for(int j=1;j<=4;j++)dp1[i+j]+=dp1[i]*(1-p)/4;
for(int i=n-5;i>=0;i--)for(int j=1;j<=4;j++)dp2[i]+=dp2[i+j]*(1-p)/4;
for(int i=0;i<=3;i++)if(n>=i)ans+=dp1[n-i];
for(int i=0;i<=n-4;i++)ans+=dp1[i]*dp2[i];
printf("%.3lf%%",ans*pow(p,m)*100);
}
代码Zmh19680924


收起.