Official
A - Robot Balance Editorial by en_translator
For beginners
- If you are new to learning programming and do not know where to start, please try Problem A "Welcome to AtCoder" from practice contest. There you can find a sample code for each language.
- Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in "AtCoder Beginners Selection".
- 「C++入門 AtCoder Programming Guide for beginners (APG4b)」 is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
- 「Python入門 AtCoder Programming Guide for beginners (APG4bPython)」 is a Python tutorial for competitive programmers. Again, this is only in Japanese.
This problem can be solved by implementing the following process:
- If the head weight is less than or equal to body weight, print \(0\).
- Otherwise, print \(H-B\).
This process can be implemented by using an if statement, or using an max statement after applying a slight deformation on the expressions.
The following is sample code.
Sample code using if statement
#include <iostream>
using namespace std;
int main() {
int H, B;
cin >> H >> B;
if (H <= B) { // If head weight is less than or equal to body weight,
cout << 0 << endl; // print 0
} else { // Otherwise,
cout << H - B << endl; // print H - B
}
return 0;
}
H, B = map(int, input().split())
if H <= B: # If head weight is less than or equal to body weight,
print(0) # print 0
else: # Otherwise,
print(H - B) # print H - B
Sample code using max function
The process above can be rephrased as:
- Print \(B-B\) if \(H\le B\).
- Print \(H-B\) otherwise.
In other words, we are asked to:
- print \(\max\lbrace H,B\rbrace-B\).
The problem can be solved by implementing this.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int H, B;
cin >> H >> B;
cout << max(H, B) - B << endl;
return 0;
}
H, B = map(int, input().split())
print(max(H, B) - B)
posted:
last update: