1. 程式人生 > 實用技巧 >【演算法】模擬 普及/提高- acoj.com

【演算法】模擬 普及/提高- acoj.com

目錄

10022. 潛伏者

解題思路

AC程式碼

#include <iostream>
#include <string>

using namespace std;

int main() {
    string e, o, t;
    cin >> e >> o >> t;
    int len = e.length();
    char e2o[26] = {0}, o2e[26] = {0};
    for (int i = 0; i < len; i++) {
        if (e2o[e[i]-'A'] == 0) {
            if (o2e[o[i]-'A'] != 0) {
                cout << "Failed" << endl;
                return 0;
            }
            e2o[e[i]-'A'] = o[i];
            o2e[o[i]-'A'] = e[i];
        } else if (e2o[e[i]-'A'] != o[i]) {
            cout << "Failed" << endl;
            return 0;
        }
    }
    for (int i = 0; i < 26; i++)
        if (e2o[i] == 0) {
            cout << "Failed" << endl;
            return 0;
        }
    len = t.length();
    for (int i = 0; i < len; i++)
        cout << e2o[t[i]-'A'];
    cout << endl;
    return 0;
}

10025. HackSon的趣味題

解題思路

AC程式碼

#include <iostream>

using namespace std;

int gcd(int a, int b) {
    if (a % b == 0)
        return b;
    return gcd(b, a % b);
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        int a0, a1, b0, b1;
        cin >> a0 >> a1 >> b0 >> b1;
        int p = a0 / a1, q = b1 / b0, res = 0;
        for (int x = 1; x*x <= b1; x++) {
            if (b1 % x == 0) {
                if (x % a1 == 0 && gcd(x/a1, p) == 1 && gcd(b1/x, q) == 1)
                    ++res;
                int y = b1 / x;
                if (y == x)
                    continue;
                if (y % a1 == 0 && gcd(y/a1, p) == 1 && gcd(b1/y, q) == 1)
                    ++res;
            }
        }
        cout << res << endl;
    }
    return 0;
}

12016. 迴文平方數

解題思路

AC程式碼

#include <iostream>
#include <sstream>
#include <algorithm>

using namespace std;

char i2c(int n, int b) {
    if (b <= 10)
        return n + '0';
    if (n < 10)
        return n + '0';
    else
        return n - 10 + 'A';
}

string convert(int n, int b) {
    ostringstream oss;
    while (n > 0) {
        oss << i2c(n % b, b);
        n /= b;
    }
    string s = oss.str();
    reverse(s.begin(), s.end());
    return s;
}

bool isPalindrome(string &s) {
    int len = s.length();
    for (int i = 0; i < len/2; i++)
        if (s[i] != s[len-i-1])
            return false;
    return true;
}

int main() {
    int b;
    cin >> b;
    for (int i = 1; i <= 300; i++) {
        string bi = convert(i, b), bs = convert(i*i, b);
        if (isPalindrome(bs))
            cout << bi << " " << bs << endl;
    }
    return 0;
}

12033. 炸彈人

解題思路

AC程式碼

#include <iostream>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    const int MAXN = 50;
    char maze[MAXN][MAXN];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> maze[i][j];
    int maxNum = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (maze[i][j] != '.')
                continue;
            int num = 0;
            for (int k = i-1; k >= 0 && maze[k][j] != '#'; k--)
                if (maze[k][j] == 'G')
                    ++num;
            for (int k = j-1; k >= 0 && maze[i][k] != '#'; k--)
                if (maze[i][k] == 'G')
                    ++num;
            for (int k = j+1; k < m && maze[i][k] != '#'; k++)
                if (maze[i][k] == 'G')
                    ++num;
            for (int k = i+1; k < n && maze[k][j] != '#'; k++)
                if (maze[k][j] == 'G')
                    ++num;
            if (num > maxNum)
                maxNum = num;
        }
    }
    cout << maxNum << endl;
    return 0;
}

12060. 成績單

解題思路

AC程式碼

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 130000;
struct Stu {
    string name;
    int score;
} stu[MAXN];
vector<string> full;

bool isHigher(const Stu &s1, const Stu &s2) {
    return s1.name < s2.name;
}

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    int seg[16] = {0};
    for (int i = 0; i < n; i++) {
        cin >> stu[i].name >> stu[i].score;
        int score = stu[i].score;
        ++seg[score/10];
    }
    sort(stu, stu+n, isHigher);
    for (int i = 0; i < 15; i++)
        cout << seg[i] << " ";
    cout << endl;
    for (int i = 0; i < n; i++) {
        cout << stu[i].name << " " << stu[i].score << endl;
        if (stu[i].score == 150)
            full.push_back(stu[i].name);
    }
    cout << full.size() << endl;
    if (full.size() == 0)
        cout << "No" << endl;
    else {
        for (string &name : full)
            cout << name << endl;
    }
    return 0;
}