大家好,又见面了,我是你们的朋友全栈君。
#include "stdafx.h"
#include <iostream>
// 碰撞后速度
void txpz(double m1, double m2, double & v1, double & v2)
{
double v1_ = ((m1-m2)* v1 + 2 * m2 * v2) / (m1 + m2);
double v2_ = ((m2-m1) * v2 + 2 * m1 * v1) / (m1 +m2);
v1 = v1_;
v2 = v2_;
}
// 完全弹性碰撞( 返回碰撞次数 )
int cishu(double m1, double m2)
{
double v1 = -1.0;
double v2 = 0;
int nTime = 0;
while (true) {
if ((v1 >= 0) && (v2 < v1))
{
break;
}
txpz(m1, m2, v1, v2);
nTime ++;
if (v2 < 0)
{
v2 = -v2;
nTime ++;
}
}
using namespace std;
cout<<m1<< "\t"<<m2<< "\t"<< nTime<<endl;
return nTime;
}
int main()
{
for (int i = 1; i < 8; i ++)
{
cishu(pow(100,i) * 1.0, 1.0);
}
return 0;
}
看一下一个有趣的代码
// 下面再放一份LUA版本
function txpz(t)
local m1 = t.m1
local m2 = t.m2
local v1 = t.v1
local v2 = t.v2
t.v1 = ((m1-m2) * v1 + 2 * m2 * v2) / (m1 + m2);
t.v2 = ((m2-m1) * v2 + 2 * m1 * v1) / (m1 + m2);
end
function cishu(t)
t.v1 = -1.0;
t.v2 = 0;
local nTime = 0;
while true do
if t.v1 >= 0 and t.v2 < t.v1 then
break;
end
txpz(t);
nTime = nTime + 1;
if t.v2 < 0 then
t.v2 = -t.v2;
nTime = nTime + 1;
end
end
return nTime;
end
print(cishu{m1 = 1.0, m2 = 1.0})
print(cishu{m1 = 100.0, m2 = 1.0})
print(cishu{m1 = 10000.0, m2 = 1.0})
print(cishu{m1 = 1000000.0, m2 = 1.0})
print(cishu{m1 = 100000000.0, m2 = 1.0})
print(cishu{m1 = 100000000.0, m2 = 0.01})
print(cishu{m1 = 100000000.0, m2 = 0.0001})
print(cishu{m1 = 100000000.0, m2 = 0.000001})
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/197730.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有