Gửi bài giải
Điểm:
1,00 (OI)
Giới hạn thời gian:
1.0s
Giới hạn bộ nhớ:
256M
Tác giả:
Dạng bài
Ngôn ngữ cho phép
C, C#, C++, Go, Java, Pascal, Perl, PHP, Python, Ruby, Rust, Scratch, Swift
Cho mảng ~A~ gồm ~n~ số nguyên, hãy viết chương trình tìm và in ra các số nguyên tố có trong mảng
Input
- Dòng đầu tiên là số nguyên dương ~n~
- Dòng tiếp theo là ~n~ số nguyên tương ứng là các phần tử của mảng ~A~
Trong đó
- ~n \le 10^4~
- ~|A_{ij}| \le 1000~
Output
Xuất ra các số nguyên tố có trong mảng, yêu cầu:
- Theo thứ tự tăng dần
- Chỉ in ra 1 lần cho dù số nguyên tố đó xuất hiện nhiều hơn 1 lần
- Các số nguyên tố cách nhau bằng 1 dấu cách
Sample
Input #1
4
1 2 2 3
Output #1
2 3
Bình luận
Bình luận này đã bị ẩn vì có quá nhiều phản ứng tiêu cực. Nhấn để xem.
include<stdio.h>
include<math.h>
long long b[10000000]={0}; long long nt(long long n) { if(n<2) return 0; else { for(int i=2;i<=sqrt(n);i++) { if(n%i==0) return 0; } return 1;
} int main() { long long n; scanf("%lld", &n); long long a[n+1]; for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); } for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { if(a[i]>a[j]) { int tg=a[i]; a[i]=a[j]; a[j]=tg; } } }
}
Có rất nhiều cách làm, đây là một cách:
include<bits/stdc++.h>
using namespace std; bool ktnt(int so) { if(so<2) return false; for(int i=2;i<=sqrt(so);i++) if(so%i==0) return false; return true; } int main() { int n; cin>>n; int a[n]; for(int i=0;i<n;i++) cin>>a[i];
}
case 4 la j the moi nguoi
hình như là in ra tăng dần bạn ạ
include <bits/stdc++.h>
using namespace std;
long long a,A[100005],B[1000005];
bool KTSNT(long long a)
{
if(a<2 || a==4)return false;
if(a>5)
{
if(a%2==0 || a%3==0)return false;
else
{
for(int i=5;i<=sqrt(a);i+=6)
if(a%i==0 || a%(i+2)==0)return false;
}
}
return true;
}
int main()
{
cin>>a;
for(int i=0;i<a;i++)cin>>A[i];
sort(A,A+a);
B[1000005]={0};
for(int i=0;i<a;i++)
{
if(KTSNT(A[i])==true)
{
if(B[A[i]]==0)
{
cout<<A[i]<<" ";B[A[i]]=1;
}
}
}
return 0;
}
include <bits/stdc++.h>
typedef long long ll; typedef double dou;
using namespace std;
map<ll, ll> mp; bool check(ll n){ if(n <= 1 || n % 2 == 0 || n % 3 == 0){ return n == 2 || n == 3; } for(ll i = 5; i <= sqrt(n); i += 6){ if(n % i == 0 || n % (i + 2) == 0){ return false; } } return true; } ll n, a[100006]; int main(){ cin >> n; for(int i = 1; i <= n; i++){ cin >> a[i]; mp[a[i]]++; } sort(a + 1, a + n + 1); for(int i = 1; i <= n; i++){ if(check(a[i]) == true && mp[a[i]] != 0){ cout << a[i] << ' '; mp[a[i]] = 0; } } return 0; } code C++ của em các bạn có thể than khảo nhé ^^
include <bits/stdc++.h>
define f() {iosbase::syncwith_stdio(0); cin.tie(0); cout.tie(0);}
using namespace std;
bool check(int n){ if(n < 2){ return false; } for(int i = 2; i < n; i++){ if(n % i == 0){ return false; } } return true; }
int main(){ int n; cin >> n; set<int> a; for(int i = 0; i < n; i++){ int x; cin >> x; a.insert(x); } for(auto& c : a){ if(check(c)){ cout << c << " "; } } return 0; } c++11
include <iostream>
include <algorithm>
typedef unsigned int ust; const ust max = 1000;
bool isPrime(ust n) { if (n < 2) return false; for (ust i = 2; i*i <= n; ++i) { if (n % i == 0) return false; } return true; }
void removeDuplicates(ust arr[], ust& size) { for (ust i = 0; i < size; ++i) { for (ust j = i + 1; j < size; ) { if (arr[i] == arr[j]) { // Di chuyển các phần tử sau phần tử trùng lặp về trước for (ust k = j; k < size - 1; ++k) { arr[k] = arr[k + 1]; } --size; // Giảm kích thước mảng } else { ++j; } } } std::sort(arr, arr + size); }
int main() { ust n; std::cin >> n; ust a[max]; for (ust i = 0; i < n; ++i) { std::cin >> a[i]; } ust foundPrime[max]; ust foundP = 0; for (ust i = 0; i < n; ++i) { if ((isPrime(a[i]))) { foundPrime[foundP++] = a[i]; } } removeDuplicates(foundPrime, foundP); for (ust i = 0; i < foundP; ++i) { std::cout << foundPrime[i] << " "; } return 0; }
**
**
include <stdio.h>
include <math.h>
typedef long long ll; typedef double dou;
define MAX_SIZE 100006
int cmpfunc(const void* a, const void* b) { return ((ll)a - (ll)b); }
int check(ll n) { if (n <= 1 || n % 2 == 0 || n % 3 == 0) { return (n == 2 || n == 3); } for (ll i = 5; i <= sqrt(n); i += 6) { if (n % i == 0 || n % (i + 2) == 0) { return 0; } } return 1; }
int main() { ll n, a[MAX_SIZE]; scanf("%lld", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } qsort(a + 1, n, sizeof(ll), cmpfunc); for (int i = 1; i <= n; i++) { if (check(a[i]) == 1 && a[i] != 0) { printf("%lld ", a[i]); a[i] = 0; } } return 0; } code của mình bạn có thể tham khảo nha ^^
test 1 có bao nhiêu số vậy mn, ai đó liệt kê ra cho mik với :(
21 số nha b
mình code bài này bị quá thời gian, mn có thể xem giúp mình được k ạ, cảm ơn mn rất nhiều !
include <iostream>
include <math.h>
using namespace std;
int n, x, i; int a[100000001];
bool kt_snt(int x){ int i; if (x == 2) return true; for (i = 2; i <= int(sqrt(x)); i++){ if (x % i == 0) return false; } return true; }
int main(){ cin >> n; for (i = 0; i < n; i++){ cin >> x; if (x > 1) a[x]++; } for (i = 2; i <= 100000000; i++){ if (a[i] != 0){ if (kt_snt(i)) cout << i << " "; } } return 0; }
import java.util.*;
public class LearnMap { static boolean isPrime(int n) { if (n < 2) return false; int flag = 0; for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { flag++; } }
}