PAT- BASIC – 15POINTS

标题标明注意点及相关启发

1001 简单计数器

#include<iostream>
using namespace std;

int cnt = 0;

int main(){
    int n;
    while(~scanf("%d", &n)) {
        while(n != 1) {
            if(n % 2 == 0) n /= 2;
            else n = (3 * n + 1) / 2;
            cnt++;
        }
        printf("%d", cnt);
    }

    return 0;
}

1006 取模与基本输入输出

#include <iostream>
using namespace std;

int main()
{
    int n;
    int m[3];
    while (~scanf("%d", &n)) {

        for (int i = 0; i < 3; i++) {
            m[i] = n % 10;
            n /= 10;
        }

        for (int i = 2; i >= 0; i--) {
            int j = 1;
            while (m[i]--) {
                if (i == 2) printf("B");
                else if (i == 1) printf("S");
                else printf("%d", j++);
            }
        }

        printf("\n");
    }

}

1011 变量范围

#include <iostream>
using namespace std;

int main(){
    long long a, b, c;
    int n;
    while(~scanf("%d", &n)){
        int i = 1;
        while(n--){
            scanf("%lld%lld%lld", &a, &b, &c);
            if(a + b > c) printf("Case #%d: true", i++);
            else printf("Case #%d: false", i++);
            printf("\n");
        }
    }
    return 0;
}

1016 大整数与十进制计算

#include<iostream>
using namespace std;

long long P(char x[], int y)
{
    long long sum = 0;
    for(int i = 0; x[i]; i++)
        if(x[i] - '0' == y)
            sum = 10 * sum + y;
    return sum;
}

int main() {
    char a[10], c[10];
    int b, d;
    while(~scanf("%s%d%s%d", &a, &b, &c, &d))
        printf("%lld\n", P(a, b) + P(c, d));
    return 0;
}

1021 简单基数排序思想

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

int main() {
    int n[10];
    char s[1000];
    memset(n, 0, sizeof(n));
    while(~scanf("%s", &s)) {
        for(int i = 0; s[i]; i++)
            n[s[i] - '0']++;
        for(int i = 0; i < 10; i++)
            if(n[i]) printf("%d:%d\n", i, n[i]);
    }
    return 0;
}

1026 四舍五入及输出补齐

#include <iostream>
using namespace std;

int main() {
    int c1, c2;
    while(~scanf("%d%d", &c1, &c2)) {
        int s = (c2 - c1 + 50) / 100;
        int hh = s / 3600;
        int mm = s % 3600 / 60;
        int ss = s % 60;
        printf("%02d:%02d:%02d\n", hh, mm, ss);
    }
    return 0;
}

1031 注意变量发生变化

#include <iostream>
using namespace std;

int main() {
    int w[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    char z[] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
    char s[18];
    int n;
    while(~scanf("%d", &n)) {
        int cnt = n;
        while(n--) {
            int sum = 0;
            bool f = true;
            scanf("%s", &s);

            for(int i = 0; i < 17; i++) {
                if(s[i] >= '0' && s[i] <= '9')
                    sum += w[i] * (s[i] - '0');
                else { f = false; break;};
            }

            if(f && z[sum % 11] == s[17]) cnt--;
            else printf("%s\n", s);
        }
        if(cnt == 0) printf("All passed\n");
    }
    return 0;
}

1036 空格输入1型循环四舍五入

#include <iostream>
using namespace std;

int main()
{
    int n;
    char c;
    scanf("%d %c", &n, &c);
    int end = (n + 1) / 2;
    for (int i = 1; i <= end; i++) {
        for (int j = 0; j < n; j++) {
            if (i == 1 || i == end) printf("%c", c);
            else if (j == 0 || j == n - 1) printf("%c", c);
            else printf(" ");
        }
        printf("\n");
    }

    return 0;
}

1041 循环体内存分配及指针数组

#include <iostream>
using namespace std;

int main()
{
    int n, m;
    scanf("%d", &n);
    int exam[n + 1];
    string atmt[n + 1];
    while (n--)
    {
        int e, a;
        char s[16];
        scanf("%s%d%d", &s, &a, &e);
        exam[a] = e;
        atmt[a] = s;
    }

    scanf("%d", &m);
    while (m--)
    {
        int t;
        scanf("%d", &t);
        printf("%s %d\n", atmt[t].c_str(), exam[t]);
    }
    return 0;
}

1046 变量必赋初值及考虑异或应用

#include <iostream>
using namespace std;
int main() {
    int n, a1, b1, a2, b2, c1 = 0, c2 = 0;
    scanf("%d", &n);
    while(n--) {
        scanf("%d%d%d%d", &a1, &b1, &a2, &b2);
        int r = a1 + a2;
        int d1 = r == b1;
        int d2 = r == b2;
        if(d1 > d2) { c2++; continue; }
        if(d1 < d2) c1++;
    }
    printf("%d %d\n", c1, c2);
    return 0;
}

1051 数学函数、输出小数舍入、简化

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    double r1, p1, r2, p2, a, b;
    scanf("%lf%lf%lf%lf", &r1, &p1, &r2, &p2);
    a = r1 * r2 * cos(p1 + p2);
    b = r1 * r2 * sin(p1 + p2);

    if (a < 0 && a > -0.005) a = 0.0;
    if (b < 0 && b > -0.005) b = 0.0;
    printf("%.2f%+.2fi", a, b);

    return 0;
}

1056 枚举下标、问题转化

int main() {
    int n, a, sum = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a);
        sum += a;
    }
    printf("%d\n", sum * 11 * (n - 1));
    return 0;
}

1061 输入时&符、二维数组指针

#include <iostream>
using namespace std;

int main() {
    int n, m, x, sum = 0;
    scanf("%d%d", &n, &m);
    int s[m], a[m], r[m];
    for (int i = 0; i < m; i++)
        scanf("%d", s + i);

    for (int i = 0; i < m; i++)
        scanf("%d", a + i);

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            scanf("%d", &x);
            if (x == a[j]) sum += s[j];
        }
        printf("%d\n", sum);
        sum = 0;
    }
    return 0;
}

1071 运算符优先级

#include <iostream>
using namespace std;

int main()
{
    int t0, k, n1, b, t, n2;
    scanf("%d%d", &t0, &k);
    while (k--)
    {
        scanf("%d%d%d%d", &n1, &b, &t, &n2);
        if (t0 <= 0) { printf("Game Over.\n"); break; }
        else if (t0 < t) printf("Not enough tokens.  Total = %d.\n", t0);
        else if ((b == 0 && n1 > n2) || (b == 1 && n1 < n2)) { t0 += t; printf("Win %d!  Total = %d.\n", t, t0); }
        else { t0 -= t; printf("Lose %d.  Total = %d.\n", t, t0); }
    }
    return 0;
}

1076 空格换行处理、注意索引、简化控制

#include <iostream>
using namespace std;
int main() {
    char s[4];
    while (~scanf("%s", &s))
        if (s[2] == 'T')
            printf("%d", s[0] - 'A' + 1);
    return 0;
}

1081 控制结构、getline

#include <iostream>
using namespace std;

int main() {
    bool nF, aF, qF;
    string s;
    getline(cin, s);
    while(getline(cin, s)) {
        nF = aF = qF =false;
        if(s.length() < 6) {
            puts("Your password is tai duan le.");
            continue;
        }
        for(int i = 0; s[i]; i++) {
            if((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
                aF = true;
            else if(s[i] >= '0' && s[i] <= '9')
                nF = true;
            else if(s[i] != '.') {
                qF = true;
                break;
            }
        }
        if (qF)            puts("Your password is tai luan le.");
        else if(aF && nF)  puts("Your password is wan mei.");
        else if(aF)        puts("Your password needs shu zi.");
        else               puts("Your password needs zi mu.");
    }
    return 0;
}

1086 注意0的处理

#include <iostream>
using namespace std;

int main () {
    int a, b;
    scanf("%d%d", &a, &b);
    a = a * b;
    if(!a) { printf("0\n"); return 0; }
    while(!(a % 10)) a /= 10;
    while(a) { printf("%d", a % 10); a /= 10; }
    printf("\n");
    return 0;
}

1091 不同类型之间运算转换

#include <iostream>
using namespace std;

int main()
{
    int m, n;
    long long k, l, s, t;
    scanf("%d", &m);
    while (m--) {
        scanf("%lld", &k);
        t = k;

        for (s = 1; t; s *= 10)
            t /= 10;

        for (n = 1; n < 10; n++) {
            l = n * k * k;
            if (l % s == k) {
                printf("%d %lld\n", n, l);
                break;
            }
        }
        if (n == 10) printf("No\n");
    }
    return 0;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

©2018-2024 Howell版权所有 备案号:冀ICP备19000576号