• PAT B1048 数字加密 (20)


    AC代码

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    const int max_n = 110;
    int main() {
        #ifdef ONLINE_JUDGE
        #else
            freopen("1.txt", "r", stdin);
        #endif // ONLINE_JUDGE
        char str[13] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K'};
        char A[max_n], B[max_n], ans[max_n];
        //把A,B中的每个元素都赋值'0'
    //    memset(A, '0', sizeof(A));
    //    memset(B, '0', sizeof(B));
        scanf("%s %s", A, B);
    //    printf("%s %s
    ", A, B);
    //    for(int i = 0; i < max_n; i++) {
    //        printf("%c", A[i]);
    //    }
    //    printf("
    ");
        int len_A = strlen(A);
        int len_B = strlen(B);
    //    printf("lenA:%d lenB:%d
    ", len_A, len_B);
        //直接用STL里面的reverse函数直接反转
        reverse(A, A + len_A);
        reverse(B, B + len_B);
    //    printf("reserve  A:%s B:%s
    ", A, B);
        int len = max(len_A, len_B);
    //    printf("max:%d
    ", len);
        for(int i = 0; i < len; i++) {
            int numA = i < len_A ? A[i] - '0' : 0;
            int numB = i < len_B ? B[i] - '0' : 0;
            if(i % 2 == 0) {
                ans[i] = str[(numA + numB) % 13];
    //            printf("%d:ans[%d] = %c
    ", i, i, ans[i]);
            }
            if(i % 2 == 1) {
                int temp = (numB - numA);
                if(temp < 0) ans[i] = str[temp + 10];
                else ans[i] = str[temp];
    //            printf("%d:temp = %d, ans[%d] = %c
    ", i, temp, i, str[temp]);
    //            if(i >= 7) printf("B[%d]:%d A[%d]:%d
    ", i, B[i]-'0', i, A[i]-'0');
            }
        }
        reverse(ans, ans + len);
        //puts(ans);
        //printf("%s", ans);
    //    printf("max:%d
    ", len);
        for(int i = 0; i < len; i++) {
            printf("%c", ans[i]);
        }
        return 0;
    }
    
  • 相关阅读:
    POJ 1860 Currency Exchange (Bellman ford)
    POJ 1502 MPI Maelstrom (最短路)
    2015 Multi-University Training Contest 2 1006(DFS)
    HDU 1495 非常可乐(枚举+DFS)
    HDU 5289 Assignment(单调队列)
    ACDream 1734 Can you make a water problem?(贪心)
    ACDream 1735 输油管道
    ACDream 1726 A Math game (折半查找)
    CSU 1602 Needle Throwing Game (投针问题)
    CSU 1604 SunnyPig (BFS)
  • 原文地址:https://www.cnblogs.com/isChenJY/p/11350473.html
Copyright © 2020-2023  润新知