其他分享
首页 > 其他分享> > PTA1048-C语言-数字加密

PTA1048-C语言-数字加密

作者:互联网

#include <stdio.h>
#include <string.h>
char odd(char a,char b)
{
    int c,d;
    c = (int)(a - '0');
    d = (int)(b - '0');
    int sum = 0;
    sum = (c + d)%13;
    //printf("sumodd = %d\n",sum);
    switch (sum)
    {
    case 0:return '0';break;
    case 1:return '1';break;
    case 2:return '2';break;
    case 3:return '3';break;
    case 4:return '4';break;
    case 5:return '5';break;
    case 6:return '6';break;
    case 7:return '7';break;
    case 8:return '8';break;
    case 9:return '9';break;
    case 10:return 'J';break;
    case 11:return 'Q';break;
    case 12:return 'K';break;
    default:
        break;
    }
}

char even(char a, char b)
{
    int c,d;
    c = (int)(a - '0');
    d = (int)(b - '0');
    if((d-c) < 0)
        return (char)((d-c+10)+'0');
    else
        return (char)((d-c)+'0');
        
}
int main(void)
{
    char all[202];
    char A[101];
    char B[101];
    gets(all);
    int len = strlen(all);
    int i ;
    int j = 0;
    for(i = 0; all[i] != ' '; i ++)
    {
        A[i] = all[i];
    }
    int lenA = i;
    i ++;
    for(; i < len ; i ++)
    {
        B[j] = all[i];
        j ++;
    }
    int lenB = j;
    //===============================
    char outstr[101];
    int len_max = 0;
    if(lenA > lenB)
    { 
        len_max = lenA;
        for(i = 0; i < lenB; i ++)
        {
            if(i%2 == 0)
                outstr[i] = odd(A[lenA - 1 -i],B[lenB - 1 - i]);
            else
                outstr[i] = even(A[lenA - 1 - i],B[lenB - 1 -i]);
        }
        for(i ; i < lenA; i ++)
        {
            if(i%2 == 0)
                outstr[i] = odd(A[lenA - 1 -i],'0');
            else
                outstr[i] = even(A[lenA - 1 - i],'0');
        }
    }
    else
    {
        len_max = lenB;
        for(i = 0; i < lenA; i ++)
        {
            if(i%2 == 0)
                outstr[i] = odd(A[lenA - 1 -i],B[lenB - 1 - i]);
            else
                outstr[i] = even(A[lenA - 1 - i],B[lenB - 1 -i]);
        }
        
        for(i; i  < lenB; i ++)//测试点2和5
        {
            
             if(i%2 == 0)
                outstr[i] = odd('0',B[lenB - 1 - i]);
            else
                outstr[i] = even('0',B[lenB - 1 -i]);
           
        }
    }
    //===============================
    
   
    for(i = 0; i < len_max; i ++)
    {
        printf("%c",outstr[len_max - i -1]);
    }
    return 0;
}

标签:case,char,return,lenB,int,C语言,break,PTA1048,加密
来源: https://blog.csdn.net/weixin_43729835/article/details/120797128