#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int d1, d2, d3;
cin >> d1 >> d2 >> d3;
int ans = min({d1+d2+d3, d1+d1+d2+d2, d1+d3+d3+d1, d2+d3+d3+d2});
cout << ans << endl;
return 0;
}
Explanation:
- The first two lines include the necessary header files for input/output and algorithm functions in C++.
- The
using namespace std;
statement allows us to use the standard library functions without explicitly specifying the std::
prefix. - The
main()
function is the entry point of the program. - Three integer variables
d1
, d2
, and d3
are declared to store the input values. - The
cin
statement is used to read three integers from the user and store them in the variables d1
, d2
, and d3
. - The
min()
function is used to find the minimum value among a list of four expressions. - The first expression
d1+d2+d3
calculates the sum of all three distances. - The second expression
d1+d1+d2+d2
calculates the sum of the first distance twice and the second distance twice. - The third expression
d1+d3+d3+d1
calculates the sum of the first distance twice and the third distance twice. - The fourth expression
d2+d3+d3+d2
calculates the sum of the second distance twice and the third distance twice. - The result of the
min()
function is stored in the variable ans
. - The
cout
statement is used to output the value of ans
to the console. - The
endl
manipulator is used to insert a newline character after the output. - Finally, the
return 0;
statement indicates that the program has finished execution and returns a value of 0 to the operating system.