用c++代码写游泳时间之差的计算
时间: 2024-09-28 12:16:03 浏览: 25
3 游泳池_计算游泳池面积_
在C++中,你可以创建一个函数来计算两个游泳者完成比赛的时间差。假设你有两个结构体Swimmer,包含姓名(name)和完成时间(time),你可以这样做:
```cpp
#include <iostream>
#include <string>
struct Swimmer {
std::string name;
double time; // 时间以秒为单位
};
double calculateTimeDifference(Swimmer swimmer1, Swimmer swimmer2) {
return swimmer2.time - swimmer1.time;
}
int main() {
Swimmer swimmerA = {"Alice", 500.0};
Swimmer swimmerB = {"Bob", 480.0}; // 假设这是两个游泳者的成绩
double timeDiff = calculateTimeDifference(swimmerA, swimmerB);
std::cout << "Swimmer " << swimmerA.name << " finished "
<< timeDiff << " seconds ahead of " << swimmerB.name << "." << std::endl;
return 0;
}
```
在这个例子中,`calculateTimeDifference`函数接收两个`Swimmer`对象作为输入,并返回它们之间的时间差。在`main`函数中,我们创建了两个游泳者实例并计算了他们的成绩差距。
阅读全文