Try   HackMD

ALGI Exordium: Editorial

ExordiumTuyển tập 25 Bài Code Nhập môn (chọn lọc từ sách 300 bài code thiếu nhi) do The Algitect (ALGI Project) sinh test chấm nhằm hỗ trợ các bạn học sinh mới học lập trình nắm chắc cách sử dụng ngôn ngữ lập trình (đặc biệt là ở bậc THCS).

Phân loại: Tính toán cơ bản - Câu điều kiện - Vòng lặp

Problem [01]

#include <bits/stdc++.h>
using namespace std;

int main(){
    long long a, b;
    cin >> a >> b;
    
    cout << a + b << '\n';
    cout << a - b << '\n';
    cout << b - a << '\n';
    cout << a * b;
    
    return 0;
}

Problem [02]

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a, b, c, t;
    cin >> a >> b >> c;
    
    if (a < b) {
        t = a;
        a = b;
        b = t;
    }
    
    if (a < c) {
        t = a;
        a = c;
        c = t;
    }
    
    if (b < c) {
        t = b;
        b = c;
        c = t;
    }
    
    cout << c << ' ' << b << ' ' << a;
    
    return 0;
}

Problem [03]

#include <bits/stdc++.h>
using namespace std;

int main(){
    long long a, b;
    cin >> a >> b;
    
    cout << (-b) / a;

    return 0;
}

Problem [04]

#include <bits/stdc++.h>
using namespace std;

int main(){
    long long hour;
    cin >> hour;
    
    long long week = 24*7, day = 24;
    
    cout << hour / week << " ";
    hour %= week;
    
    cout << hour / day << " ";
    hour %= day;
    
    cout << hour << " ";

    return 0;
}

Problem [05]

#include <bits/stdc++.h>
using namespace std;
 
int main() { 
    long long d, m, y, top;
    cin >> d >> m >> y;
    
    if (m < 1 || m > 12) { 
        cout << "Khong hop le"; 
        return 0; 
    }
    
    if (m == 4 || m == 6 || m == 9 || m == 11) {
        top = 30;
    }
    else if (m == 2) {
        top = 28;
        if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
            top++;
        }
    }
    else {
        top = 31;
    }
    
    if (d < 1 || d > top) { 
        cout << "Khong hop le"; 
        return 0; 
    } 
    
    cout << "Hop le";
    
    return 0;
}

Problem [06]

#include <bits/stdc++.h>
using namespace std;
 
int main() { 
    int h, m, s, timer; 

    cin >> h >> m >> s; 
    timer = 3600 * h + 60 * m + s; 

    cin >> h >> m >> s; 
    timer -= 3600 * h + 60 * m + s; 

    if (timer < 0) timer = -timer;

    cout << timer / 3600 << " ";
    cout << (timer % 3600) / 60 << " ";
    cout << (timer % 3600) % 60;
    
    return 0; 
} 

Problem [07]

#include <bits/stdc++.h>
using namespace std;
 
int main() { 
    int n; 
    cin >> n;
    
    int count = 0;
    long long sum = 0;
    for (int i = 1; i <= n; i++) {
        if (n % i == 0) { 
            cout << i << " ";
            
            count++; 
            sum += i; 
        } 
    }

    cout << count << "\n" << sum;
    
    return 0;
}

Problem [08]

#include <bits/stdc++.h>
using namespace std;
 
int main() { 
    long long n, t; 
    long long u, sum = 0, count = 0; 

    cin >> n;
    t = n;
    
    do { 
        count++; 
        u = t % 10;
        sum += u;
    }
    while (t /= 10); 

    do {
        t = t * 10 + n % 10;
    }
    while (n /= 10); 

    cout << count << "\n";
    cout << n % 10 << "\n";
    cout << u << "\n";
    cout << sum << "\n";
    cout << t;
  
    return 0;
}

Problem [09]

#include <bits/stdc++.h>
using namespace std;
 
int main() { 
    long long a, b;
    cin >> a >> b;
    
    cout << __gcd(a, b) << "\n";
    cout << a / __gcd(a, b) * b;
    
    return 0; 
} 

Problem [10]

#include <bits/stdc++.h>
using namespace std;
 
int main() { 
    double xA, yA, xB, yB; 
    cin >> xA >> yA >> xB >> yB;
    
    double res = sqrt((xB - xA) * (xB - xA) + (yB - yA) * (yB - yA));
    cout << fixed << setprecision(4) << res; 

    return 0;
}

Phân loại: Tính toán nâng cao - Mảng một chiều

Problem [11]

#include <bits/stdc++.h>
using namespace std;

int n, res;

bool check(int x) {
    for (int j = 2; j < x; j++) {
        if (x % j==0) {
            return false;
        }
    }
    
    return true;
}

int main() {
    cin >> n;
    for (int i = 2; ; i++){
        if (check(i)) {
            cout << i << " ";
            res++;
        }
        
        if (res == n) break;
    }
    return 0;
}

Problem [12]


Problem [13]

#include <bits/stdc++.h>
using namespace std;

int n, res;

int main() {
    cin >> n;
    for (int a = 1; a <= n; a++) {
        for (int b = a+1; b <= n; b++) {
            for (int c = b+1; c <= n; c++) {
                if (a*a + b*b == c*c) {
                    res++;
                }
            }
        }
    }

    cout << res;
    
    return 0;
}

Problem [14]

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    
    if (n % 2 != 0) {
        cout << ((1 + n) * ((n + 1) / 2)) / 2;
    }
    else {
        cout << ((2 +n ) * (n / 2)) / 2;
    }
    
    return 0;
}

Problem [15]

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6;
int n, m, q, res;
int A[N+5];

int main() {
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin>>A[i];
    }
    
    for (int i = 1; i <= q; i++) {
        int id, x;
        cin >> id >> x;
        A[id] = x;
    }
    
    for (int i = 1; i <= n; i++) {
        cout << A[i] << " ";
    }
    
    return 0;
}

Problem [16]

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6;
int n, m, q, k;
double res, ans;
int A[N+5];

int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> A[i];
        res += A[i];
    }
    
    for (int i = 1; i <= k; i++) {
        int x;
        cin >> x;
        ans += A[x];
    }

    cout << fixed << setprecision(5) << (res / n) << "\n";
    cout << fixed << setprecision(5) << (ans / k);
    return 0;
}

Problem [17]

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6;
int n, m, q, k;
int A[N*2+5];

int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> A[i];
        A[i+n] = A[i];
    }

    k %= n;
    for (int i = 1+k; i <= k+n; i++) {
        cout << A[i] << " ";
    }
    
    cout << "\n";
    for(int i= n-k+1; i <= n-k+n; i++) {
        cout << A[i] << " ";
    }
    
    return 0;
}

Problem [18]

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6;
int n, m, q, k, sl1, gt1, sl2 = INT_MAX, gt2;
long double res, ans;
int A[N+5], D[N+5];

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> A[i];
        D[A[i]]++;
    }

    for (int i = 1; i <= n; i++) {
        int x = A[i];
        
        if (D[x] > sl1){
            sl1 = D[x];
            gt1 = x;
        }
        
        if (D[x] == sl1) {
            gt1 = max(gt1,x);
        }

        if (D[x] < sl2) {
            sl2 = D[x];
            gt2 = x;
        }
        
        if (D[x]==sl2) {
            gt2 = min(gt2,x);
        }
    }

    cout << gt1 << " " << sl1 << "\n";
    cout << gt2 << " " << sl2;
    
    return 0;
}

Problem [19]

#include <bits/stdc++.h>
using namespace std;

const int N = 5e6;
int n;
bool check[N+5];

int main() {
    cin >> n;
    for (int i = 2; i*i <= n; i++) {
        if (check[i] == false) {
            for (int j = i*i; j<=n; j+=i) {
                check[j] = true;
            }
        }
    }

    for (int i = 2; i <= n; i++) {
        if (check[i] == false) {
            cout << i << " ";
        }
    }
    
    return 0;
}

Problem [20]

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6;
const long long M = 1e9 + 7;

int n;
long long F[N+1];
 
int main() { 
    F[1] = 1;
    F[2] = 1;

    cin >> n;
    for (int i = 3; i <= n; i++) {
        F[i] = (F[i-1] + F[i-2]) % M;
    }

    cout << F[n];

    return 0; 
} 

Phân loại: Khác

Problem [21]

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5;
int n, k, A[N+1];

int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> A[i];
    }

    sort(A+1, A+1+n);
    cout << A[k] << " " << A[n-k+1];

    return 0;
}

Problem [22]

#include <bits/stdc++.h>
using namespace std;

const int N = 1e3;
int n, m, q, A[N+1][N+1];

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> A[i][j];
        }
    }

    cin >> q;
    while (q--) {
        int i, j, x;
        cin >> i >> j >> x;
        A[i][j] = x;
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cout << A[i][j] << " ";
        }

        cout << "\n";
    }

    return 0;
    }

Problem [23]

#include <bits/stdc++.h>
using namespace std;

string s; char k;

int main() {
    cin >> s >> k;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == k) {
            cout << (i+1) << " ";
        }
    }

    return 0;
}

Problem [24]

#include <bits/stdc++.h>
using namespace std;
string s;

int main() {
    cin >> s;
    for (int i = 0; i < s.size()/2; i++) {
        if (s[i] != s[s.size() - i - 1]) {
            cout << "No";
            return 0;
        }
    }

    cout << "Yes";

    return 0;
}

Problem [25]

#include <bits/stdc++.h>
using namespace std;
int n;

void solve(int n, char s, char t, char mid) {
    if (n == 1) {
        cout << 1 << " " << s << " " << t << "\n";
        return;
    }

    solve(n - 1, s, mid, t);

    cout << n << " " << s << " " << t << "\n";

    solve(n - 1, mid, t, s);
}

int main() {
    cin >> n;
    solve(n, 'A', 'C', 'B');

    return 0;
}