[Mảng 1 Chiều Cơ Bản]. Bài 16. Liệt kê
Xem dạng PDF
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
Input:
stdin
Output:
stdout
Tác giả:
Nguồn bài:
Dạng bài
Ngôn ngữ cho phép
C, C#, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch
Cho mảng số nguyên A[] gồm N phần tử, hãy đếm số lượng các phần tử thỏa mãn các yêu cầu sau:
Số lượng số nguyên tố trong dãy
Số lượng số thuận nghịch trong dãy
Số lượng số chính phương trong dãy
Số lượng số có tổng chữ số của nó là số nguyên tố.
Đầ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<=100
0<=A[i]<=10000
Đầu ra
- In ra 4 dòng số lượng số tương ứng với 4 yêu cầu trên
Ví dụ :
Input 01
6
4 1682 5972 6331 9872 3956
Output 01
0
1
1
4
Bình luận
include <stdio.h>
include <math.h>
int nt(int n){ if (n<2){ return 0; } for (int i=2;i<=sqrt(n);i++){ if (n%i==0){ return 0; } } return 1; } int cp(int n){ int c=sqrt(n); if(cc==n){ return 1; } return 0; } int rev(int n){ int k=0; while(n){ k=k10+n%10; n=n/10; } return k; } int checkrev(int n){ int m=n; if (m==rev(n)){ return 1; } return 0; } int sum(int n){ int sum=0; while (n){ sum+=n%10; n=n/10; } if (nt(sum)){ return 1; } return 0; } int main(void){ int cnt1,cnt2,cnt3,cnt4; cnt1=cnt2=cnt3=cnt4=0; int n; scanf("%d",&n); int a[n]; for (int i=0;i<n;i++){ scanf("%d",&a[i]); } for (int i=0;i<n;i++){ if(nt(a[i])){ cnt1++; } if(cp(a[i])){ cnt2++; } if(checkrev(a[i])){ cnt3++; } if(sum(a[i])){ cnt4++; } } printf("%d\n%d\n%d\n%d",cnt1,cnt3,cnt2,cnt4); }
include <bits/stdc++.h>
using namespace std; bool ngto(long long n) { if(n < 2) return false; for(int i = 2; i * i <= n; i++) { if(n % i == 0) return false; } return true; } bool ds(long long n) { string s = to_string(n); int l = 0, r = s.size() - 1; while(l < r) { if(s[l] != s[r]) return false;
} bool cp(long long n) { if(n < 1) return false; long long g = sqrt(n); return (g * g == n); } int tongcs(long long n) { long long res = 0; while(n > 0) { res += n % 10; n /= 10; } return res; } bool tongnt(long long n) { long long g = tongcs(n); if(ngto(g)) return true; return false; }
int main() { iosbase::syncwith_stdio(false); cin.tie(nullptr); long long n; cin >> n; vector<long long> a(n); for(int i = 0; i < n; i++) cin >> a[i]; int d1 = 0, d2 = 0, d3 = 0, d4 = 0; for(int i = 0; i < n; i++) { if(ngto(a[i])) d1++; if(ds(a[i])) d2++; if(cp(a[i])) d3++; if(tongnt(a[i])) d4++; } cout << d1 << endl << d2 << endl << d3 << endl << d4; }
DÀI CHỨ KO KHÓ NHA AE
include <bits/stdc++.h>
using namespace std; using ll = long long;
bool nguyen_to(int n){ if(n < 2) return false; for(int i = 2; i <= sqrt(n); i++){ if(n % i == 0) return false; } return true; }
bool thuannghich(int n){ string s = tostring(n); string s2 = s; reverse(s2.begin(), s2.end()); return s == s2; }
bool chinh_phuong(int n){ int x = sqrt(n); return x * x == n; }
bool tongnguyento(int n){ string s = tostring(n); int tong = 0; for(char c : s){ tong += c - '0'; } return nguyento(tong); }
int main() { ios::syncwithstdio(false); cin.tie(nullptr);
}
yahh bài gây lười
include <bits/stdc++.h>
void solve() { int n,a=0,s=0; string s1, s2; cin>>n; vector<int> v(n); for(int&i:v)cin>>i; for(int i=0;i<n;i++) if(isPrime(v[i])) a++; cout << a << el; a = 0; for(int i=0;i<n;i++){ s1 = tostring(v[i]); s2 = s1; reverse(s1.begin(), s1.end()); if(s1 == s2) a++; } cout << a << el; a = 0; for(int i=0;i<n;i++) if(isPerfectSqr(v[i])) a++; cout << a << el; a = 0; for(int i=0;i<n;i++){ s = 0; for(char c : tostring(v[i])) s += c - '0'; if(isPrime(s)) a++; } cout << a; }
include <bits/stdc++.h>
using namespace std;
define ll long long
ll cnt=0,s=0,chinhphuong=0,thuannghich=0,ngto=0,tong=0,b; ll doixung(ll n) { ll r=0,o=n; while(n>0) { r=r10+(n%10); n/=10; } return r==o; } bool snt(ll n) { if(n<=1) return false; if(n<=3) return true; if(n%2==0||n%3==0) return false; for(ll i=5;ii<=n;i+=6) { if(n%i==0||n%(i+2)==0) return false; } return true; } ll scp(ll n) { ll sqrtN=sqrt(n); return sqrtN*sqrtN==n; } ll tongso(ll n) { ll s=0; while(n>0) { s+=n%10; n/=10; } return s; } int main() { iosbase::syncwith_stdio(false); cin.tie(NULL); ll n; cin>>n; vector<ll> a(n); for(ll i=0;i<n;i++) { cin>>a[i]; if(snt(a[i])) ngto++; if(doixung(a[i])) thuannghich++; if(scp(a[i])) chinhphuong++; if(snt(tongso(a[i]))) tong++; } cout<<ngto<<" "<<thuannghich<<" "<<chinhphuong<<" "<<tong<<"\n"; return 0; }
import java.util.Scanner; public class m1clkesnttncp { public static boolean nTo(int n) { if(n < 2 ) { return false; } for(int i = 2; i<=Math.sqrt(n); i++) { if(n % i == 0) { return false; } } return true; } public static boolean tNghich(int n) { int m = 0, tmp = n; while(n != 0) { m = m * 10 + n % 10; n /= 10; } if(tmp == m) { return true; } return false; } public static boolean cPhuong(int n) { int can = (int)Math.sqrt(n); if(can * can == n) { return true; } return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0; i<n; i++) { a[i] = sc.nextInt(); } int count = 0, counttn = 0, countcp = 0, countt = 0; for(int i = 0; i<n; i++) { if(nTo(a[i])) { ++count; } if(tNghich(a[i])) { ++counttn; } if(cPhuong(a[i])) { ++countcp; } } for(int i = 0; i<n; i++) { int b = 0, j = a[i]; while(j > 0) { b += j % 10; j /= 10; } if(nTo(b)) { ++countt; } } System.out.println(count); System.out.println(counttn); System.out.println(countcp); System.out.println(countt); } }
FULL AC
include<bits/stdc++.h>
using namespace std; int nguyento(int n) { if(n<2) return 0; for(int i=2;i<n;i++) if(n%i==0) return 0; return 1; } int tong(int n) { int s=0; while(n!=0) { s+=n%10; n/=10; } return s; } int thuannghich(int n) { int m = 0, tmp = n; while(n != 0){ m = m * 10 + n % 10; n /= 10; } if(tmp == m){ return 1; } return 0; } int main() { int nt=0,tn=0,cp=0,tong1=0; int n; cin>>n; int a[n+9]; for(int i=0;i<n;i++) { cin>>a[i]; if (nguyento(a[i])) ++nt; if (thuannghich(a[i])) ++tn; if (sqrt(a[i])==trunc(sqrt(a[i]))) ++cp; if(nguyento(tong(a[i]))) ++tong1; } cout<<nt<
include <iostream>
include <vector>
include <cmath>
include <iomanip>
include <set>
include <map>
include <algorithm>
bool cp(int n) { int x = sqrt(n); if (x * x == n) return 1; else return 0; } bool isprime(int n) { if (n < 2) return 0; for (int i = 2; i <=sqrt(n); i++) { if (n % i == 0) return 0; } return 1; } bool palin(int n) { int org = n; // loi thuong gap vi n da bi thay doi int x = 0; while (n) { int tmp = 0; tmp = n % 10; x = x * 10 + tmp; n /= 10; } if (x== org) return 1; else return 0; } bool sip(int n) {
} using namespace std;
int main(){ int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0; int n; cin >> n; vector<int> a(n); for (int& x : a) cin >> x; for (int i = 0; i < n; i++) { if (isprime(a[i])) cnt1++; else if (palin(a[i])) cnt2++; else if (cp(a[i])) cnt3++; else if (sip(a[i])) cnt4++; } cout << cnt1 << endl; cout << cnt2 << endl; cout << cnt3 << endl; cout << cnt4 ; return 0; }
sao bi wrong ans nhieu test case qua :<
Bình luận này đã bị ẩn vì có quá nhiều phản ứng tiêu cực. Nhấn để xem.