输入样例:
1234567 368782971
输出样例:
3695Q8118
思路很容易就能想到。注意,如果两个数字长度不相等,需要在短的那一个数字前面补零。
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <map> #include <queue> #include <stack> #include <algorithm> using namespace std; void encryption(vector<int> s1,vector<int> s2); int main() { vector<int> s1,s2; char c; c=getchar(); while(c!=‘ ‘&&c!=‘\n‘) { s1.push_back(c-‘0‘); c=getchar(); } c=getchar(); while(c!=‘ ‘&&c!=‘\n‘) { s2.push_back(c-‘0‘); c=getchar(); } //补全两个字符串的长度 if(s1.size()>s2.size()) { while(s1.size() != s2.size()) s2.insert(s2.begin() ,0); } if(s1.size()<s2.size()) { while(s1.size() != s2.size()) s1.insert(s1.begin() ,0); } encryption(s1,s2); return 0; } void encryption(vector<int> s1,vector<int> s2) { reverse(s1.begin(),s1.end()); reverse(s2.begin(),s2.end()); for(int i=0; i<s2.size(); i++) { if((i+1)%2==0) { s2[i]-=s1[i]; if(s2[i]<0) s2[i]+=10; } else s2[i]=(s2[i]+s1[i])%13; } reverse(s2.begin(),s2.end()); for(vector<int>::iterator itor=s2.begin(); itor!=s2.end() ; itor++) { switch(*itor) { case 10: cout<<‘J‘; break; case 11: cout<<‘Q‘; break; case 12: cout<<‘K‘; break; default: cout<<*itor; } } }
原文:https://www.cnblogs.com/FDProcess/p/9245844.html