AtCoder Beginner Contest 170 A I will explain the problem "Five Variables".
Problem URL: https://atcoder.jp/contests/abc170/tasks/abc170_a
Five variables $ x_1, x_2, x_3, x_4, x_5 $ are given. The original variable was $ x_i = i $, but only one was assigned $ 0 $. Now, answer which variable was assigned $ 0 $.
-The inputs $ x_1, x_2, x_3, x_4, x_5 $ are possible combinations after being substituted.
It is a method to perform conditional branching whether it is $ 0 $ for all values. This allows you to find out which variable has $ 0 $ assigned to it.
Let the sequence of $ x_1, x_2, x_3, x_4, x_5 $ be the sequence $ (X) $. In fact, taking advantage of the fact that the sum of the first five variables is $ 15 $, 15 minus the sum of $ X $ </ b> You can see that is the value of the variable to which $ 0 $ is assigned. Therefore, this should be calculated and output.
Below are examples of solutions in Python3, C ++, and Java. (I used the solution of solution 2)
{A.py}
x = list(map(int,input().split()))
print(15 - sum(x))
{A.cpp}
#include<bits/stdc++.h>
using namespace std;
int main(){
int ans = 15;
for (int i = 0; i < 5; i++){
int x;
cin >> x;
ans = ans - x;
}
cout << ans << endl;
}
(Don't forget the line breaks !!)
{A.java}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int ans = 15;
for (int i = 0; i < 5; i++){
int x = scan.nextInt();
ans = ans - x;
}
System.out.println(ans);
}
}
Recommended Posts