B1048 数字加密

题目要求

本题要求实现一种数字加密方法。首先固定一个加密用正整数 A,对任一正整数 B,将其每 1 位数字与 A 的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对 13 取余——这里用 J 代表 10、Q 代表 11、K 代表 12;对偶数位,用 B 的数字减去 A 的数字,若结果为负数,则再加 10。这里令个位为第 1 位。

输入格式:

输入在一行中依次给出 A 和 B,均为不超过 100 位的正整数,其间以空格分隔。

输出格式:

在一行中输出加密后的结果。

输入样例:

1234567 368782971

输出样例:

3695Q8118

错误代码

#include<iostream>
#include<string>
using namespace std;
int main() {
	string str1, str2, str3;
	cin >> str1 >> str2;
	int len1 = str1.size();
	int len2 = str2.size();
	int len = len1> len2? len1 : len2;
	for (int i = len - 1; i >= 0; i--) {
		if (i % 2 == 0) {  //实际上的奇数位
			int temp1 = ((str1[i] - '0)' + (str2[i] - '0')) % 13);
			if (temp1 < 10) str3[i] = temp1 +'0';
			if (temp1 == 10)  str3[i] = 'J';
			if (temp1 == 11)  str3[i] = 'Q';
			if (temp1 == 12)  str3[i] = 'K';
		}
		else {  //实际上的偶数位
			int temp2 = (str2[i]-'0') - (str1[i]-'0');
			if (temp2 < 0) str3[i] = temp2 + 10+ '0';
			else str3[i] = temp2 + '0 ';
		}
	}
	for (int i = 0; i < len; i++) {
		cout << str3[i];
	}
}

出现错误:string subscript out of range   

string 类型不允许出现以下这种赋值情况:

str[i] = ' A ';

要用

str += ' c';

修改后的代码(仍错)

为什么错呢……

#include<iostream>
#include<string>
using namespace std;
int main() {
	string str1, str2, str3;
	cin >> str1 >> str2;
	int len1 = str1.size();
	int len2 = str2.size();
	int len = len1> len2? len1 : len2;
	for (int i = len - 1; i >= 0; i--) {
		if (i % 2 == 0) {  //实际上的奇数位
			int temp1 = ((str1[i] - '0') + (str2[i] - '0')) % 13;
			if (temp1 < 10)  str3+= temp1+'0';
			if (temp1 == 10)  str3+= 'J';
			if (temp1 == 11)  str3+= 'Q';
			if (temp1 == 12)  str3+= 'K';
		}
		else {  //实际上的偶数位
			int temp2 = (str2[i] - '0' )+ (str1[i] - '0');
			if (temp2 < 0) str3+= temp2+ 10+'0';
			else str3+= temp2+'0';
		}
	}
	for (int i = 0; i < len; i++) {
		cout << str3[i];
	}
}

正确代码1

#include<iostream>
using namespace std;
int main()
{
	string A, B, result;
	cin >> A >> B;
    /* 倒转A和B */
	reverse(A.begin(), A.end());
	reverse(B.begin(), B.end());
    
    /* 使A和B位数相等 */
	if (A.length() < B.length())
		A += string(B.length() - A.length(), '0');
	else if (A.length() > B.length())
		B += string(A.length() - B.length(), '0');
    
	int len = B.length();
	for (int i = 0; i != len; i++) {
		if (i % 2 == 0) {
			int temp = (A[i] + B[i] - '0' - '0') % 13;
			if (temp == 10) result += 'J';
			else if (temp == 11)  result += 'Q';
			else if (temp == 12) result += 'K';
			else result += temp + '0';
		}
		else
			result += (B[i] - A[i] + 10) % 10 + '0';
	}
	reverse(result.begin(), result.end());
	cout << result;
}

 以下写法不知道为什么错误

int temp2 = (B[i] - '0') + (A[i] - '0');
if (temp2 < 0)
	result += temp2 + 10 + '0';
else 
    result += temp2 + '0';

正确代码2

#include <stdio.h>
#include <string.h>
const char map[13] = { '0','1','2','3','4','5','6','7','8','9','J','Q','K' };
int main()
{
    char A[101], B[101], result[101];
    scanf("%s %s", A, B);
    int count = 0, i=strlen(A)-1, j = strlen(B)-1;
    while(i>=0 || j>=0){
        int A_i = i>=0?A[i]-'0':0;
        int B_j = j>=0?B[j]-'0':0;
        if( count%2 == 0 )
            result[count++] = (A_i + B_j)%13;
        else
            result[count++] = (B_j - A_i + 10)%10;
        i--, j--;
    }
    for(int i=count-1; i>=0; i--)
        printf("%c", map[result[i]]);
}

 

 参考:https://blog.csdn.net/weixin_41256413/article/details/81741527

posted @ 2022-02-08 22:25  第厘  阅读(16)  评论(0编辑  收藏  举报