【算法笔记】入门篇一

PAT1042

#include <iostream>
#include <string>

using namespace std;
int main() {
    int K;
    int start[55], end[55], next[55];
    cin >> K;

    for (int i = 1; i <= 54; i++) { // 第i个元素总是放到第next[i]个元素上
        cin >> next[i];
    }

    for (int i = 1; i <= 54; i++) {
        start[i] = i;
    }

    for (int j = 0; j < K; j++) {
        for (int i = 1; i <= 54; i++) {
            end[next[i]] = start[i];   
        }

        for (int i = 1; i <= 54; i++) {
            start[i] = end[i];
        }
    }

    string s[55] = {" ", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "S11", "S12", "S13",
                     "H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "H11", "H12", "H13",
                     "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13",
                     "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13",
                     "J1", "J2"};

    for (int i = 1; i <= 54; i++) {
        cout << s[start[i]];
        if (i != 54) cout << " ";
    }
}

PAT1046

这题很简单,求两点间最短路径,时间限制是O(n),一开始写了一个平方复杂度的代码,最后一个超时了,用数组存起从v1到某点距离,这样就不用每次都计算了√。

#include <iostream>
#include <cmath>

using namespace std;
int main() {
    int N, M, sum = 0;
    cin >> N;
    int d[N+1], D[N+1]; 

    for (int i = 1; i <= N; i++) {
        cin >> d[i];
    }

    D[1] = 0;
    for (int i = 2; i <= N; i++) {
        D[i] = D[i-1] + d[i-1];
    }
    sum = D[N] + d[N];

    cin >> M;
    int v1, v2, temp;
    for (int i = 0; i < M; i++) {
        cin >> v1 >> v2;
        temp = abs(D[v1] - D[v2]);
        temp = temp < (sum - temp) ? temp : (sum - temp);
        cout << temp << endl;
    }
}

PAT1009

多项式,注意最后系数为零不用输出,总是有这种。。不难但是要考虑一些比较特殊的测试用例。

#include <algorithm>

using namespace std;
struct node {
    int exp;
    float c;
};

bool cmp(node n1, node n2) {
    return n1.exp > n2.exp;
}

int main() {
    int K1, K2;
    vector<node> p1, p2, p3;

    cin >> K1;
    for (int i = 0; i < K1; i++) {
        node p;
        cin >> p.exp >> p.c;
        p1.push_back(p);
    }

    cin >> K2;
    for (int i = 0; i < K2; i++) {
        node p;
        cin >> p.exp >> p.c;
        p2.push_back(p);
    }

    for (int i = 0; i < K1; i++) {
        for (int j = 0; j < K2; j++) {
            node p;
            p.exp = p1[i].exp + p2[j].exp;;
            p.c = p1[i].c * p2[j].c;
            p3.push_back(p);
        }
    }
    
    sort(p3.begin(), p3.end(), cmp);
    int e = p3[0].exp;
    int idx = 0, k = 1;
    while (k < p3.size()) {
        if (e == p3[k].exp) {
            p3[idx].c += p3[k].c;
            p3.erase(p3.begin() + k);
        } else {
            e = p3[k].exp;
            idx = k;
            k ++;
        }
    }

    k = 0;
    while (k < p3.size()) {
        if (p3[k].c == 0.0) {
            p3.erase(p3.begin() + k);
        } else {
            k ++;
        }
    }

    cout << p3.size();
    for (int k = 0; k < p3.size(); k++) {
        printf(" %d %.1f", p3[k].exp, p3[k].c);
    }
}

PAT1011

真·送分题

#include <iostream>
#include <algorithm>


using namespace std;
int main() {
    float a, b, c, m, result = 1.0;
    for (int i = 0; i < 3; i++) {
        cin >> a >> b >> c;
        m = max(max(a,b), c);
        result *= m;
        if (a == m) cout << "W";
        else if (b == m) cout << "T";
        else cout << "L";
        cout << " ";
    }

    result = (result * 0.65 - 1) * 2;
    printf("%.2f", result);
}

PAT1006

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

using namespace std;
struct stu {
    string id;
    string in;
    string out;
};

bool cmp1(stu s1, stu s2) {
    string str1 = s1.in, str2 = s2.in;
    int hour1, hour2;
    int min1, min2;
    int sec1, sec2;
    hour1 = stoi(str1.substr(0, 2));
    hour2 = stoi(str2.substr(0, 2));
    min1 = stoi(str1.substr(3, 2));
    min2 = stoi(str2.substr(3, 2));
    sec1 = stoi(str1.substr(6, 2));
    sec2 = stoi(str2.substr(6, 2));

    if (hour1 < hour2) return true;
    else if (hour1 == hour2 && min1 < min2) return true;
    else if (hour1 == hour2 && min1 == min2 && sec1 < sec2) return true;
    else return false;
}

bool cmp2(stu s1, stu s2) {
    string str1 = s1.out, str2 = s2.out;
    int hour1, hour2;
    int min1, min2;
    int sec1, sec2;
    hour1 = stoi(str1.substr(0, 2));
    hour2 = stoi(str2.substr(0, 2));
    min1 = stoi(str1.substr(3, 2));
    min2 = stoi(str2.substr(3, 2));
    sec1 = stoi(str1.substr(6, 2));
    sec2 = stoi(str2.substr(6, 2));

    if (hour1 < hour2) return true;
    else if (hour1 == hour2 && min1 < min2) return true;
    else if (hour1 == hour2 && min1 == min2 && sec1 < sec2) return true;
    else return false;
}

int main() {
    int M;
    vector<stu> s;
    cin >> M;

    for (int i = 0; i < M; i++) {
        stu student;
        cin >> student.id >> student.in >> student.out;
        s.push_back(student);
    }

    sort(s.begin(), s.end(), cmp1);
    cout << s[0].id << " ";

    sort(s.begin(), s.end(), cmp2);
    cout << s[s.size() - 1].id;
}

PAT1036

#include <iostream>
#include <cmath>

using namespace std;

struct stu{
    string name;
    string id;
};

int main() {
    int s_M = 101, s_F = -1;
    stu man, female;
    int N;
    cin >> N;

    string name, sex, id;
    int score;
    for (int i = 0; i < N; i++) {
        cin >> name >> sex >> id >> score;
        if (sex == "M" && score < s_M) {
            man.name = name;
            man.id = id;
            s_M = score;
        } else if (sex == "F" && s_F < score) {
            female.name = name;
            female.id = id;
            s_F = score;
        }
    }

    if (s_F != -1) cout << female.name << " " << female.id << endl;
    else cout << "Absent" << endl;
    if (s_M != 101) cout << man.name << " " << man.id << endl;
    else cout << "Absent" << endl;

    if (s_F != -1 && s_M != 101) cout << abs(s_F - s_M);
    else cout << "NA";
}

PAT1031

#include <iostream>
#include <string>

using namespace std;
int main() {
    string str;
    int len, n1, n2, n3;
    cin >> str;

    len = str.length();
    n1 = (len + 2) / 3;
    n3 = n1;
    n2 = len - n1 * 2 + 2;

    for (int i = 0; i < n1 - 1; i++) {
        cout << str[i];
        for (int j = 0; j < n2 - 2; j++) {
            cout << " ";
        }
        cout << str[len - 1 - i] << endl;
    }

    for (int i = 0; i < n2; i++) {
        cout << str[n1 - 1 + i];
    }
}

PAT1019

进制转换:

#include <iostream>
#include <vector>

using namespace std;
int main() {
    int N, b;
    int mob, num;
    vector<int> ans;

    cin >> N >> b;
    num = N;
    while (num != 0) {
        mob = num % b;
        num = num / b;
        ans.push_back(mob);
    }

    int flag = 1;
    for (int i = 0, j = ans.size()-1; i <= j; i++, j--) {
        if (ans[i] != ans[j]) {
            flag = 0;
            break;
        }
    }

    if (flag) cout << "Yes" << endl;
    else cout << "No" << endl;

    for (int i = ans.size() - 1; i >= 0; i--) {
        cout << ans[i];
        if (i != 0) cout << " ";
    }
}

PAT1058

#include <iostream>
#include <string>

using namespace std;
int main() {
    int a[3], b[3], c[3];
    int carry;
    scanf("%d.%d.%d %d.%d.%d", &a[0], &a[1], &a[2], &b[0], &b[1], &b[2]);

    c[2] = (a[2] + b[2]) % 29; // 当前位
    carry = (a[2] + b[2]) / 29; // 进位
    c[1] = (a[1] + b[1] + carry) % 17;
    carry = (a[1] + b[1] + carry) / 17;
    c[0] = a[0] + b[0] + carry;

    cout << c[0] << '.' << c[1] << '.' << c[2];
}

PAT1061

主要考的字符匹配。ascll码的转换,感觉printf与scanf更灵活,可以按格式输入输出。
排除错误:

  1. 根据自己理解的逻辑检查代码。
  2. 仔细阅读题目,考虑边缘测试用例。
#include <iostream>
#include <string>

using namespace std;
int main() {
    string str1, str2, str3, str4;
    string day[7] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
    
    cin >> str1 >> str2 >> str3 >> str4;

    int flag = 0;
    for (int i = 0; i < str1.length() && i < str2.length(); i++) {
        if (str1[i] == str2[i] && !flag && str1[i] >= 'A' && str1[i] <= 'G') { // 保证不超出边界
            cout << day[str1[i] - 'A'] << " ";
            flag = 1;
            continue;
        }
        if (str1[i] == str2[i] && flag) {
            if (str1[i] >= '0' && str1[i] <= '9') {
                printf("%02d", str1[i] - '0'); 
                break;
            }
            if (str1[i] >= 'A' && str1[i] <= 'N') {
                printf("%02d", str1[i] - 'A' + 10);
                break;
            }
        }
    }
    
    for (int i = 0; i < str3.length() && i < str4.length(); i++) {
        if (str3[i] == str4[i]) {
            if ((str3[i] >= 'A' && str3[i] <= 'Z') || (str3[i] >= 'a' && str3[i] <= 'z')) {
                printf(":%02d", i);
                break;
            }
        }
    }
}

PAT1073

这题主要复杂在有效位位数要保持一致,所以只能以字符串的形式处理输出,不能以数字的形式处理输出。

#include <iostream>
#include <string>
#include <cmath>

using namespace std;
int main() {
    int exp, idx;
    int count = 0; // 记录有效位位数
    string num, ans, s, note1, note2;

    cin >> s;

    note1 = s[0];
    idx = s.find('E') + 1;
    note2 = s[idx];
    
    num = s.substr(1, idx - 2);
    num.erase(num.find('.'), 1);
    exp = stoi(s.substr(idx + 1, s.length() - idx - 1));
    count = s.find('E') - s.find('.') - 1 - exp;

    if (note1 == "-") cout << '-';

    if (note2 == "-") {
        cout << "0.";
        for (int i = 0; i < exp - 1; i++) {
            cout << "0";
        }
        cout << num;
    }

    if (note2 == "+") {
        if (count > 0) {
            num.insert(num.length() - count, ".");
            cout << num;
        } else {
            cout << num;
            for (int i = 0; i < abs(count); i++) {
                cout << "0";
            }
        }
    }                  
}

PAT1005

#include <iostream>
#include <string>

using namespace std;
int main() {
    string num[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    string N, str_sum;
    int sum = 0;
    cin >> N;

    for (int i = 0; i < N.length(); i++) {
        sum += N[i] - '0';
    }

    str_sum = to_string(sum);
    int temp;
    for (int i = 0; i < str_sum.length(); i++) {
        temp = str_sum[i] - '0';
        cout << num[temp];
        if (i != str_sum.length() - 1) cout << " ";
    }
}

PAT1035

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

using namespace std;
struct user {
    string id;
    string password;
};
int main () {
    int N;
    vector<user> v;
    string id, password;

    cin >> N;
    int flag;
    for (int i = 0; i < N; i++) {
        cin >> id >> password;
        flag = 0;
        for (int j = 0; j < password.length(); j++) {
            if (password[j] == '1') {
                password[j] = '@';
                flag = 1;
            }
            else if (password[j] == '0') {
                password[j] = '%';
                flag = 1;
            }
            else if (password[j] == 'l') {
                password[j] = 'L';
                flag = 1;
            }
            else if (password[j] == 'O') {
                password[j] = 'o';
                flag = 1;
            }
        }

        user u;
        u.id = id;
        u.password = password;

        if (flag == 1) v.push_back(u);
    }

    if (v.size() == 0) {
        if (N == 1) printf("There is 1 account and no account is modified");
        else printf("There are %d accounts and no account is modified", N);
    }
    else {

PAT1077

寻找公共后缀,注意大小对齐。这题debug好久,,测试点3一直不过,后来发现,原来是因为字符串设置太小了,0-256,应设置字符数组大小257.

int main() {
    int N, end, start;
    char s1[257], s2[257];
    cin >> N;

    getchar(); // 读入换行符
    cin.getline(s1, 257);
    start = 0;
    for (int i = 1; i < N; i++) {
        cin.getline(s2, 257);
        end = strlen(s1);
        if (strlen(s2) < end - start) start = end - strlen(s2);
        for (int j = strlen(s2) - 1; j >= 0 && end > start; j--, end--) {
            if (s1[end-1] != s2[j]) {
                start = end;
                break;
            }
        }
    }

    if (start >= strlen(s1)) cout << "nai";
    else {
        for (int i = start; i < strlen(s1); i++) {
            printf("%c", s1[i]);
        }
    }
}

把求公共后缀转化为求公共前缀版本:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
int main() {
    int N, end, len1, len2;
    char s1[257], s2[257];
    cin >> N;

    getchar(); // 读入换行符
    cin.getline(s1, 257);
    len1 = strlen(s1);
    end = len1; // 左闭右开
    reverse(s1, s1 + len1);
    for (int i = 1; i < N; i++) {
        cin.getline(s2, 257);
        len2 = strlen(s2);
        reverse(s2, s2 + len2);
        if (len2 < end) end = len2;
        for (int j = 0, k = 0; j < len2 && k < end; j++, k++) {
            if (s1[k] != s2[j]) {
                end = k;
                break;
            }
        }
    }

    if (end == 0) cout << "nai";
    else {
        for (int i = end - 1; i >= 0; i--) {
            printf("%c", s1[i]);
        }
    }
}

PAT1082

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

using namespace std;
int main() {
    string num[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
    string l1[4] = {"00", "11", "Wan", "Yi"};
    string l2[5] = {"22", "33", "Shi", "Bai", "Qian"};   
    int start = 0, end, len, r; 
    string s;
    vector<string> ans;

    cin >> s;
                     
    if (s[0] == '-') { // 为负数的情况
        cout << "Fu" << " ";
        s.erase(0, 1);
    }

    if (s.length() == 1 && s == "0") cout << num[0]; // 为0的情况

    len = s.length();
    r = len / 4 + 1;
    end = len - (r - 1) * 4;
    int flag; // 多个零只读一个
    int flag2; // 排除万位级全为零的情况
    for (int i = 0; i < r; i++) {
        flag = 0;
        flag2 = 0;
        for (int j = start; j < end; j++) {
            if (s[j] != '0') flag2 = 1; // 当前级读到一个不为0的数位
            if (s[j] == '0') { // 读到0,标记
                flag = 1;
                continue;
            }
            if (flag == 1 && s[j] != '0') {
                ans.push_back("ling");
                flag = 0;
            }
            ans.push_back(num[s[j] - '0']);
            if (j != end - 1) ans.push_back(l2[end - j]);
        }
        if (r - i > 1 && flag2) ans.push_back(l1[r - i]);
        start += end - start;
        end += 4;
    }
    
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i];
        if (i != ans.size() - 1) cout << " ";
    }          
}
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值