[Mảng 1 Chiều Cơ Bản]. Bài 17. Mảng đối xứng
View as PDF
Submit solution
Points:
1.00 (partial)
Time limit:
1.0s
Memory limit:
256M
Input:
stdin
Output:
stdout
Problem source:
Problem type
Allowed languages
C, C#, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch
Cho mảng số nguyên A[] gồm N phần tử, kiểm tra xem mảng có đối xứng hay không?

Đầu vào
Dòng đầu tiên là số nguyên dương N
Dòng thứ 2 gồm N số nguyên viết cách nhau một vài khoảng trắng
Giới hạn
1<=N<=10^6
0<=A[i]<=10^6
Đầu ra
- In ra YES hoặc NO tương ứng với mảng đối xứng hoặc không.
Ví dụ :
Input 01
5
1 2 3 2 1
Output 01
YES
Comments
include <iostream>
include <vector>
using namespace std;
int main() {
ios::syncwithstdio(false);
cin.tie(nullptr);
int num; cin >> num;
vector<int> vec(num);
for (int &ele : vec) {
cin >> ele;
}
for (int idx = 0; idx <= num / 2; idx++) {
if (vec[idx] != vec[num - 1 - idx]) {
cout << "NO";
return 0; }
}
cout << "YES";
}
full AC
include <bits/stdc++.h>
using namespace std;
int main() { iosbase::syncwith_stdio(false); cin.tie(NULL); int n;cin>>n; int a[n]; map<int,int>mp; for (int i=0;i<n;i++){ cin>>a[i]; mp[a[i]]++; } int sum=0; if (n%2==0){ for (int i=0;i<n;i++){ if (mp[a[i]]%2==1){ sum=1; break; } } if (sum==0){ cout<<"YES"; } else cout<<"NO"; } else { for (int i=0;i<n;i++){ if (mp[a[i]%2==1]){ sum++; } } if (sum==1){ cout<<"YES"; } else cout<<"NO"; } return 0; }
ý tưởng cho ae nào bí nhé : FULL AC:
đầu tiên ae tạo 1 biến cờ hiệu đặt giá trị là 1 hoặc true cũng được, sau đó ae tạo 1 vòng lặp chạy từ 0 tới n/2 nhé, vì chỉ cần tới nửa mảng là đủ để kiếm tra rồi, sau đó ae kiểm tra xem phần tử i có khác với a[n-1-i] ko, n-1-i là phần tử đối diện của phần tử i nhé VD: i = 0 thì n - 1 - i = 5 - 1 - 0 là bằng 4 thì nó đối diện với i, nếu khác thì ae đặt biến cờ hiệu thành 0 hoặc false nhé, rồi ae break câu lệnh luôn vì đã tìm ra được cặp khác nhau rồi thì mảng ko có đối xứng nữa cho nên lặp thêm cũng chỉ tốn thời gian, rồi ae ở ngoài vòng lặp kiểm tra xem biến cờ hiệu có bằng false ko nếu bằng thì cout ra no , còn ko thì cout ra yes.
include <bits/stdc++.h>
67
This comment is hidden due to too much negative feedback. Show it anyway.
HAI CON TRỎ LÀ KHOẺ NHÉ AE
include <bits/stdc++.h>
using namespace std; using ll = long long;
int main() { ios::syncwithstdio(false); cin.tie(nullptr); // viết code ở đây int n ; cin >> n; int a[n]; for (auto &i : a) cin >> i; int i=0,j=n-1; bool done=false; while(i<=j){ if(a[i]!=a[j]) done=true; i++; j--; } if(done) cout << "NO"; else{ cout << "YES"; } return 0; }
include <cstdio>
constexpr int MAXN = 1000000; int arr[MAXN];
int main() { int n; if (scanf("%d", &n) != 1) return 1;
}
FULL AC
include <bits/stdc++.h>
using namespace std; int doixung(int a[], int n) { int mid = n / 2, i = 1, j = n; while (i <= mid && j > mid) { if (a[i] == a[j]) { i++; j--; } else return 0; } return 1; } int main() { int n; cin >> n; int a[n]; for (int i = 1; i <= n; i++) { cin >> a[i]; } if (doixung(a, n)) { cout << "YES"; } else cout << "NO"; return 0; }
include <iostream>
include <vector>
using namespace std;
int main() { int n; cin >> n; vector<int> a(n); for (int &x : a) cin >> x; for (int i = 0; i < n / 2; i++) { if (a[i] != a[n - i - 1]) { cout << "NO\n"; return 0; } }
}
hay
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.