A - Same 解説 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".
- 「競プロ典型 90 問」(Typical 90 Problems of Competitive Programming) is a collection of typical 90 competitive programming problems; unfortunately, currently the problem statements are all Japanese.
Naive approach
\(A _ 1,A _ 2,\ldots,A _ N\) are all equal if and only if any two elements in \(A\) are equal.
Thus, it is sufficient to read \(A\), inspect every pairs of elements, and determine if they have the same value.
The following is sample code.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N);
for(int i = 0; i < N; ++i) // Read A
cin >> A[i];
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
if(A[i] != A[j]){ // If there is a pair of different values,
cout << "No" << endl; // print No and
return 0; // exit
}
// If no pair has different values,
cout << "Yes" << endl; // print Yes and
return 0; // exit
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N);
for(auto&& a : A) // Read A
cin >> a;
for(auto a : A)
for(auto b : A)
if(a != b){ // If there is a pair of different values,
cout << "No" << endl; // print No and
return 0; // exit
}
// If no pair has different values,
cout << "Yes" << endl; // print Yes and
return 0; // exit
}
N = int(input())
A = list(map(int, input().split())) # Read A
for a in A:
for b in A:
if a != b: # If there is a pair of different values,
print('No') # print No and
break # escape from the loop
else: # If no pair has different values,
continue # continue the loop
break
else: # If no pair appeared to be different,
print('Yes') # print Yes
A faster way
\(A _ 1,A _ 2,\ldots,A _ N\) are all equal if and only if \(A _ 1\) equals all of \(A _ 2,A _ 3,\ldots,A _ N\).
Thus, it is sufficient to read \(N\) and \(A _ 1\) first, and determine if each of the remaining elements equals \(A _ 1\).
Here, it is convenient to print No
and terminate the program once a different value is found, and print Yes
after all the elements have been scanned.
The following is sample code.
#include <iostream>
using namespace std;
int main() {
int N, A1; // Read the initial element of A beforehand
cin >> N >> A1;
for (int i = 1; i < N; ++i) { // For each of the remaining elements of A,
int A;
cin >> A; // read it,
if (A != A1) { // and if they are not equal, print No and exit
cout << "No" << endl;
return 0;
}
}
// If the execution reaches here after scanning all the elements, print Yes
cout << "Yes" << endl;
return 0;
}
N = int(input())
A1, *A = map(int, input().split()) # Split A into the first and the remaining values
for a in A:
if A1 != a: # Print No if any of the element is different from the first
print('No')
break
else: # If `break` not occurred, print Yes
print('Yes')
投稿日時:
最終更新: