Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

หาความยาวสะพานจากความเร็วและความยาวรถไฟใน C++

กำหนดความยาวรถไฟ L, ความเร็ว S, และเวลา T ที่รถไฟใช้ในการข้ามสะพาน หาความยาวสะพาน B

ตัวอย่าง: L=310, S=45 m/s, T=12 วิ → B = 230 m

วิธีแก้ปัญหา (Physics Formula)

ระยะทางที่รถไฟเคลื่อนที่ = ความยาวรถไฟ + ความยาวสะพาน

S × T = L + B

B = S × T - L

ตัวอย่างโค้ด C++

#include <iostream>
using namespace std;

int findBridgeLength(int L, int S, int T) {
    int B = (S * T) - L;
    return B;
}

int main() {
    int L = 150, S = 45, T = 25;
    cout << "The length of the bridge is " << findBridgeLength(L, S, T);
    return 0;
}

Output

The length of the bridge is 975