算法笔记实战 模拟

本文详细分析了一系列编程挑战,包括整数溢出处理、程序运行时间计算、数组元素循环右移、数字图像分类和一元多项式求导等。通过实例代码展示了如何解决这些问题,涉及到了数据类型的适配、字符串处理、循环和条件判断等核心编程概念。同时,文章还探讨了如何优化代码以提高效率,如避免超时、减少复杂度和利用结构体存储多个信息。
摘要由CSDN通过智能技术生成

A + B 和 C

在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 80;
int t, a, b, c;

int main()
{
    cin >> t;
    
    for (int i = 1; i <= t; i ++ ) {
        scanf("%d%d%d", &a, &b, &c);
        
        if (a + b > c) printf("Case #%d: true\n", i);
        else printf("Case #%d: false\n", i);
    }
    
    return 0;
}

错误之处在于数据范围爆炸了int,要用long long

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 80;
long long t, a, b, c;

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

int 范围是 -263到263-1, 爆了 int 用 long long

部分 A+B

在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long LL;

const int N = 80;

LL a, b, pa, pb;
int da, db;

int main()
{
    cin >> a >> da >> b >> db;
    
    while (a) {
        
        int t = a % 10;
        if (t == da) {
            pa = 10 * pa + da;
        }
        a /= 10;
    }
    
    while (b) {
        
        int t = b % 10;
        if (t == db) {
            pb = 10 * pb + db;
        }
        b /= 10;
    }
    
    LL res = pa + pb;
    
    cout << res << endl;
    
    return 0;
}

B1026 程序运行时间

在这里插入图片描述
近似函数round在cmath中

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 80;

LL a, b;
LL hh, mm, ss;

int main()
{
    cin >> a >> b;
    
    if (a > b) {
        LL t = a;
        a = b;
        b = t;
    }
    
    LL d = b - a;
    d = round(d / 100.0);
    
    hh = d / 3600;
    d %= 3600;
    
    mm = d / 60;
    d %= 60;
    
    ss = d;
    
    printf("%02lld:%02lld:%02lld\n", hh, mm, ss);
    
    return 0;
}

问题在于输出怎么填充0 %02d 整数,占两位,不足填前导0

B1046 划拳

在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 110;

int n;
int aS, aD, bS, bD;

int main()
{
    cin >> n;
    
    int resA = 0, resB = 0;
    while (n -- ) {
        
        scanf("%d %d %d %d", &aS, &aD, &bS, &bD);
        if ((aD == aS + bS) && (bD != aD)) {
            resB ++ ;
        } else if ((bD == aS + bS) && (bD != aD)) {
            resA ++ ;
        }
    }
    
    cout << resA << ' ' << resB << endl;
    
    return 0;
}

B1008 数组元素循环右移

在这里插入图片描述
错误代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 110;

int n, m;
int a[2 * N];

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
    
    for (int i = n - 1; i >= 0; i -- ) a[i + m] = a[i];
    for (int i = 0; i < m; i ++ ) a[i] = a[n + i];
    
    for (int i  = 0; i < n; i ++ ) {
        printf("%d", a[i]);
        if (i != n - 1) printf(" ");
    }
    
    printf("\n");
    
    return 0;
}

错误原因 M 是 可能大于 N 的
因此 m = n % m;

数字图像分类

在这里插入图片描述
通过部分测评

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 1010;

int n;
int a[10], d[N];

int main()
{
    while (scanf("%d", &n) != EOF) {
        
        bool flag = false; // 监测 A2 类
        int con = 1; // A2 类的运算符
        int cnt = 0; // 计算 A4 类的个数
        
        for (int i = 0; i < n ; i ++ ) scanf("%d", &d[i]);
        
        for (int i = 0; i < n ; i ++ ) {
            if ((d[i] % 5 == 0) && (d[i] % 2 == 0)) a[1] += d[i]; 
            else if (d[i] % 5 == 1) {
                a[2] = a[2] + con * d[i];
                flag = true;
                con = -1 * con;
            }
            else if (d[i] % 5 == 2) a[3] ++ ;
            else if (d[i] % 5 == 3) {
                a[4] += d[i];
                cnt ++ ;
            }
            else a[5] = max(a[5], d[i]);
        }
        
        if (a[1]) printf("%d ", a[1]);
        else printf("N ");
        if (flag) printf("%d ", a[2]);
        else printf("N ");
        if (a[3]) printf("%d ", a[3]);
        else printf("N ");
        if (a[4]) printf("%.1f ", float(a[4]) / cnt);
        else printf("N ");
        if (a[5]) printf("%d\n", a[5]);
        else printf("N\n");
    }
    
    return 0;
}

其实可以同时开一个计数数组和一个累加数值数组

B1018 锤头剪刀布

在这里插入图片描述

简单做法,没有考虑字典序输出

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int n;
int a[3], b[3];
int aCnt[3], bCnt[3];

int main()
{
    cin >> n;
    while (n -- ) {
        
        char l, r;
        cin >> l >> r;

        if ((l == 'J') && (r == 'B')) a[0] ++, b[2] ++ , aCnt[0] ++ ;
        if ((l == 'C') && (r == 'J')) a[0] ++, b[2] ++ , aCnt[1] ++ ;
        if ((l == 'B') && (r == 'C')) a[0] ++, b[2] ++ , aCnt[2] ++ ;
        
        if ((r == 'J') && (l == 'B')) b[0] ++, a[2] ++ , bCnt[0] ++ ;
        if ((r == 'C') && (l == 'J')) b[0] ++, a[2] ++ , bCnt[1] ++ ;
        if ((r == 'B') && (l == 'C')) b[0] ++, a[2] ++ , bCnt[2] ++ ;
        
        if ((r == 'J') && (r == l)) a[1] ++, b[1] ++;
        if ((r == 'C') && (r == l)) a[1] ++, b[1] ++;
        if ((r == 'B') && (r == l)) a[1] ++, b[1] ++;
        
    }
    
    printf("%d %d %d\n", a[0], a[1], a[2]);
    printf("%d %d %d\n", b[0], b[1], b[2]);
    
    if (aCnt[0] > aCnt[1] && aCnt[0] > aCnt[2]) printf("J ");
    else if (aCnt[1] > aCnt[0] && aCnt[1] > aCnt[2]) printf("C ");
    else printf("B ");
    
    if (bCnt[0] > bCnt[1] && bCnt[0] > bCnt[2]) printf("J\n");
    else if (bCnt[1] > bCnt[0] && bCnt[1] > bCnt[2]) printf("C\n");
    else printf("B\n");
    
    return 0;
}

使用scanf()读入c的时候有\n的问题,要使用 getchar 吸收掉换行符,同时可以做字母和数字之间的映射,减少复杂度。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int change(char c) {
    if (c == 'B') return 0;
    if (c == 'C') return 1;
    if (c == 'J') return 2;
}

int main()
{
    char mp[3] = {'B', 'C', 'J'}; // mp[0] : 'B', mp[1] : 'C', mp[2] : 'J'
    int n;
    scanf("%d", &n);
    
    int time_A[3] = {0}, time_B[3] = {0}; // 记录 A B 三种局势的次数
    int hand_A[3] = {0}, hand_B[3] = {0}; // 记录三种手势获胜的次数
    
    char c1, c2;
    int k1, k2;
    
    for (int i = 0; i < n; i ++ ) {
        getchar();
        scanf("%c %c", &c1, &c2);
        
        k1 = change(c1);
        k2 = change(c2);
        
        if ((k1 + 1) % 3 == k2) { // 甲获胜
            time_A[0] ++ ;
            time_B[2] ++ ;
            hand_A[k1] ++ ;
        } else if (k1 == k2) { // 平局
            time_A[1] ++ ;
            time_B[1] ++ ;
        } else { // 乙获胜
            time_A[2] ++ ;
            time_B[0] ++ ;
            hand_B[k2] ++ ;
        }
    }
    
    printf("%d %d %d\n", time_A[0], time_A[1], time_A[2]);
    printf("%d %d %d\n", time_B[0], time_B[1], time_B[2]);
    
    int id1 = 0, id2 = 0;
    for (int i = 0; i < 3; i ++ ) {
        if (hand_A[i] > hand_A[id1]) id1 = i;
        if (hand_B[i] > hand_B[id2]) id2 = i;
    }
    
    printf("%c %c\n", mp[id1], mp[id2]);
    
    return 0;
}

标准答案版

shuffling Machine

在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 60;

int n;
int go[N], back[N], form[N];

int main()
{
    cin >> n;
    char* mp[] = {"a", "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 ++ ) scanf("%d", &form[i]), go[i] = i, back[i] = i;
    
    while (n -- ) {
        for (int i = 1; i <= 54; i ++ ) {
            back[form[i]] = go[i];
        }
        memcpy(go, back, sizeof(go));
    }
    
    for (int i = 1; i <= 54; i ++ ) {
        printf("%s", mp[go[i]]);
        if (i != 54) {
            printf(" ");
        }
    }
    printf("\n");
    
    return 0;
}

是memcpy 而不是 memcopy 少一个字母 o
存放字符串用指针形变量 申明方式是char * name[] =

Shortest Distance

我写的代码的问题,这份代码超时
在这里插入图片描述
修改后
在这里插入图片描述

B1010 一元多项式求导

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 1000 + 10;

int m, n;
double a[N], b[N];
bool st[N] = {false};

int main()
{
    cin >> m;
    int k;
    double x;
    while (m -- ) {
        scanf("%d %lf", &k, &x);
        a[k] = x;
    }
    
    cin >> n;
    while (n -- ) {
        scanf("%d %lf", &k, &x);
        b[k] = x;
    }
    
    int cnt = 0;
    for (int i = 0; i <= 1000; i ++ ) {
        if (a[i] != 0 || b[i] != 0) {
            a[i] += b[i];
            if (a[i] != 0) {
                st[i] = true;
                cnt ++ ;
            }
        }
    }
    
    printf("%d ", cnt);
    for (int i = 1000; i >= 0; i -- ) {
        if (st[i]) {
            printf("%d %.1lf", i, a[i]);
            if (cnt != 1) {
                printf(" ");
            }
            cnt -- ;
        }
    }
    printf("\n");
    
    return 0;
}

A1009 多项式乘法

在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 2000 + 10;

struct pointMent {
    double res; // 答案
    double cof; // 系数
}p[N];

int main()
{   
    int n, k;
    double x;
    
    cin >> n;
    for (int i = 0; i < n; i ++ ) {
        scanf("%d %lf", &k, &x);
        p[k].cof = x;
    }
    
    cin >> n;
    for (int i = 0; i < n; i ++ ) {
        scanf("%d %lf", &k, &x);
        
        for (int j = 0; j <= 1000; j ++ ) {
            p[j + k].res += p[j].cof * x; 
        }
    }
    
    int cnt = 0;
    for (int i = 0; i < N; i ++ ) {
        if (p[i].res != 0.0) cnt ++ ;
    }
    
    printf("%d", cnt);
    for (int i = N; i >= 0; i -- ) {
        if (p[i].res != 0.0) {
            printf(" %d %.1f", i, p[i].res);
        }
    }
    printf("\n");
    
    return 0;
}

double类型和0比较要用0.0比较,同一个位置要存储多个信息的时候可以使用结构体

B1041 考试座位号

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 1000 + 10;

struct student {
    long long id; // 准考证考
    int test; // 试机号
    int exam; // 考试号
}stu[N];

int n, m;

int main()
{   
    cin >> n;
    for (int i = 0; i < n; i ++ ) {
        scanf("%lld %d %d", &stu[i].id, &stu[i].test, &stu[i].exam);
    }
    
    cin >> m;
    for (int i = 0; i < m; i ++ ) {
        int test;
        scanf("%d", &test);
        
        for (int j = 0; i < N; j ++ ) {
            if (stu[j].test == test) {
                printf("%lld %d", stu[j].id, stu[j].exam);
                
                if (i != m - 1) {
                    printf("\n");
                }
                break;
            }
        }
    }
    printf("\n");
    
    return 0;
}

使用结构体是关键

B1004成绩排名

在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 1000 + 10;

struct student {
    char name[20]; // 姓名
    char id[20]; // 学号
    int score; // 考试成绩
}stu[N];

int n;

int main()
{   
    cin >> n;
    for (int i = 0; i < n; i ++ ) {
        scanf("%s %s %d", stu[i].name, stu[i].id, &stu[i].score);
    }
    
    int minId = 0;
    int maxId = 0;
    for (int i = 0; i < n; i ++ ) {
        if (stu[i].score > stu[maxId].score) maxId = i;
        if (stu[i].score < stu[minId].score) minId = i;
    }
    printf("%s %s\n", stu[maxId].name, stu[maxId].id);
    printf("%s %s\n", stu[minId].name, stu[minId].id);
    
    return 0;
}

B1028 人口普查

在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

struct student {
    char name[20]; // 姓名
    LL birthday; // 生日
}stu[N];

int n;

int main()
{   
    cin >> n;
    
    int cnt = 0;
    int minId = -1;
    int maxId = -1;
    for (int i = 0; i < n; i ++ ) {
        int yy, mm, dd;
        scanf("%s %d/%d/%d", stu[i].name, &yy, &mm, &dd);
        stu[i].birthday = yy * 10000 + mm * 100 + dd;
        if (stu[i].birthday >= 18140906 && stu[i].birthday <= 20140906) {
            cnt ++ ;
            if (stu[i].birthday > stu[minId].birthday || minId == -1) {
                minId = i;
            }
            
            if (stu[i].birthday < stu[maxId].birthday || maxId == -1) {
                maxId = i;
            }
        }
    }
    if (cnt) {
        printf("%d %s %s\n", cnt, stu[maxId].name, stu[minId].name);
    } else {
        printf("0\n");   
    }
    
    return 0;
}

加上对所有都不合法的特判

World Cup Betting

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

struct student {
    char name[20]; // 姓名
    LL birthday; // 生日
}stu[N];

int n;

int main()
{   
    char mp[4] = {'W', 'T', 'L'};
    double p[4][4] = {};
    for (int i = 0; i < 3; i ++ ) {
        scanf("%lf %lf %lf", &p[i][0], &p[i][1], &p[i][2]);
    }
    
    double res = 1;
    for (int i = 0; i < 3; i ++ ) {
        int I = 0;
        double val = p[i][I];
        for (int j = 0; j < 3; j ++ ) {
            if (p[i][j] > val) {
                val = p[i][j];
                I = j;
            }
        }
        printf("%c ", mp[I]);
        res *= val;
    }
    res = (res * 0.65 - 1) * 2;
    printf("%.2lf\n", res);
    
    return 0;
}

接收double数据类型一定要用%lf

A1006 sign in and sign out

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

struct person {
    char name[20]; // 姓名
    int start; // 进入
    int end; // 离开
}p[N];

int n;

int main()
{   
    cin >> n;
    
    int minId = 0;
    int maxId = 0;
    for (int i = 0; i < n; i ++ ) {
        int h1, m1, s1, h2, m2, s2;
        scanf("%s %d:%d:%d %d:%d:%d", p[i].name, &h1, &m1, &s1, &h2, &m2, &s2);
        p[i].start = h1 * 3600 + m1 * 60 + s1;
        p[i].end = h2 *3600 + m2 * 60 + s2;
        
        if (p[minId].start > p[i].start) {
            minId = i;
        }
        
        if (p[maxId].end < p[i].end) {
            maxId = i;
        }
    }
    
    printf("%s %s\n", p[minId].name, p[maxId].name);
    
    return 0;
}

A1036 boys vs girls

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

struct person {
    char name[20]; // 姓名
    char sexy; // 性别
    char lesson[20]; // 课程
    int score; // 得分
}p[N];

int n;

int main()
{   
    cin >> n;
    
    
    int minId = -1;
    int maxId = -1;
    
    for (int i = 0; i < n; i ++ ) {
        scanf("%s %c %s %d", &p[i].name, &p[i].sexy, &p[i].lesson, &p[i].score);
        
        if (p[i].sexy == 'M' && (minId == -1 || p[minId].score > p[i].score)) {
            minId = i;
        }
        
        if (p[i].sexy == 'F' && (maxId == -1 || p[maxId].score < p[i].score)) {
            maxId = i;
        }
    }
    
    if (maxId == -1) {
        printf("Absent\n");
    } else {
        printf("%s %s\n", p[maxId].name, p[maxId].lesson);
    }
    
    if (minId == -1) {
        printf("Absent\n");
    } else {
        printf("%s %s\n", p[minId].name, p[minId].lesson);
    }
    
    if (maxId == -1 || minId == -1) {
        printf("NA\n");
    } else {
        printf("%d\n", p[maxId].score - p[minId].score);
    }
    
    return 0;
}

B1027 打印沙漏

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int n;
char c;

int main()
{   
    cin >> n >> c;
    
    int row = 1;
    int res = 0;
    
    while ((2 * row - 1 + 1) / 2 * row * 2 - 1 <= n ) {
        row ++ ;
    }
    
    row -- ;
    res = n - ((2 * row - 1 + 1) / 2 * row * 2 - 1);
    
    for (int i = 1; i <= row; i ++ ) {
        for (int j = i - 1; j > 0; j -- ) printf(" ");
        for (int j = 2 * (row - i + 1) - 1; j >= 1; j -- ) printf("%c", c);
        printf("\n");
        
    }
    
    for (int i = 2; i <= row; i ++ ) {
        for (int j = row - i; j > 0; j -- ) printf(" ");
        for (int j = 2 * i - 1; j > 0; j -- ) printf("%c", c);
        printf("\n");
    }
    
    printf("%d\n", res);
    
    return 0;
}

A1031 Hello world for U

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 1e3;

int n;
char c[N][N], a[N];

int main()
{   
    scanf("%s", a);
    int len = strlen(a);
    int side = (len + 2) / 3;
    int bottom = (len + 2) - side * 2;
    
    for (int i = 0; i < side; i ++ ) {
        c[i][0] = a[i];
        c[i][bottom - 1] = a[side + bottom + side - 3 - i];
    }
    
    for (int i = 0; i < bottom; i ++ ) {
        c[side - 1][i] = a[side - 1 + i];
    }
    
    for (int i = 0; i < side; i ++ ) {
        for (int j = 0; j < bottom; j ++ ) {
            if (c[i][j] != NULL) {
                printf("%c", c[i][j]);
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }
    
    return 0;
}

B1002 写出这个数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 110;

char a[N];
char b[N][N];
int n, cnt[20];

int main()
{   
    cin >> a;
    int len = strlen(a);
    
    char mp[][20] = {"lin", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
    for (int i = 0; i < len; i ++ ) {
        cnt[a[i] - '0'] ++ ;
    }
    
    for (int i = 0; i <= 9; i ++ ) {
        n += i * cnt[i];
    }
    
    int num = 0;
    while (n) {
        int r = n % 10;
        strcpy(b[num], mp[r]);
        num ++ ;
        n /= 10;
    }
    
    for (int i = num - 1; i >= 0; i -- ) {
        printf("%s", b[i]);
        if (i != 0) {
            printf(" ");
        }
    }
    
    printf("\n");
    
    return 0;
}

字符串复制strcpy(des, src)

B1014福尔摩斯的约会

错误

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 70;

char a[10][N];

int main()
{   
    char dayMap[][N] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
    
    for (int i = 0; i < 4; i ++ ) {
        scanf("%s", a[i]);
    }
    
    int flag = 0;
    int len = strlen(a[0]);
    for (int i = 0; i < len; i ++ ) {
        if (a[0][i] == a[1][i] && (a[0][i] >= 'A' && a[0][i] <= 'G') && flag == 0) {
            printf("%s ", dayMap[a[0][i] - 'A']);
            flag ++ ;
            continue;
        }
        
        if (a[0][i] == a[1][i] && flag == 1) {
            if (a[0][i] >= '0' && a[0][i] <= 9) {
                printf("0%c:", a[0][i]);
            } else if (a[0][i] >= 'A' && a[0][i] <= 'N') {
                printf("%d:", a[0][i] - 'A' + 10);
            } else {
                continue;
            }
            
            flag ++ ;
            continue;
        }
        
        if (flag == 2) {
            break;
        }
    }
    
    len = strlen(a[2]);
    for (int i = 0; i < len; i ++ ) {
        if (a[2][i] == a[3][i] && ((a[2][i] >= 'a' && a[2][i] <= 'z') || (a[2][i] >= 'A' && a[2][i] <= 'Z'))) {
            printf("%02d\n", i);
        }
    }
    
    return 0;
}

B1024科学计数法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 10010;

char str[N];

int main()
{   
    scanf("%s", str);
    int len = strlen(str);
    if (str[0] == '-') printf("-");
    
    int pos = 0; // 存放 E 的位置
    while (str[pos] != 'E') {
        pos ++ ;
    }
    
    int exp = 0; // 存放指数
    for (int i = pos + 2; i < len; i ++ ) {
        exp = exp * 10 + (str[i] - '0');
    }
    
    if (exp == 0) {
        for (int i = 1; i < pos; i ++ ) {
            printf("%c", str[i]);
        }
    }
    
    if (str[pos + 1] == '-') { // 如果是负指数
        printf("0.");
        for (int i = 0; i < exp - 1; i ++ ) {
            printf("0");
        }
        
        printf("%c", str[1]); // 输出除了小数点以外的数据
        for (int i = 3; i < pos; i ++ ) {
            printf("%c", str[i]);
        }
    } else { // 如果指数为正
        for (int i = 1; i < pos; i ++ ) { // 输出小数点移动之后的数
            if (str[i] == '.') continue;
            printf("%c", str[i]);
            if (i == exp + 2 && pos - 3 != exp) {
                printf(".");
            }
        }
        
        // 如果指数 exp 较大,输出多余的 0
        for (int i = 0; i < exp - (pos - 3); i ++ ) {
            printf("0");
        }
    }

    printf("\n");
    return 0;
}

B1048数字加密

在这里插入图片描述

在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

const int N = 110;

char A[N], B[N], ans[N];
char mp[20] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K'};

void reverse(char s[]) {
    int len = strlen(s);
    
    for (int i = 0; i < len / 2; i ++ ) {
        char temp = s[i];
        s[i] = s[len - 1 - i];
        s[len - 1 - i] = temp;
    }
}

int main()
{   
    cin >> A >> B;
    reverse(A);
    reverse(B);
    
    int lenA = strlen(A);
    int lenB = strlen(B);

    int len = lenA >= lenB ? lenA : lenB;
    for (int i = 0; i < len; i ++ ) {
        char a = i < lenA ? A[i] : '0';
        char b = i < lenB ? B[i] : '0';
        
        if (i % 2 == 0) { // 奇数
            ans[i] = mp[(a - '0' + b - '0') % 13];
        } else { // 偶数
            ans[i] = (b - a + 10) % 10 + '0';
        }
    }
    
    reverse(ans);
    puts(ans);

    return 0;
}

正确做法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值