题解 | #字符串加解密#
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
- 通俗易懂,注意if else 得判断。
#include<bits/stdc++.h> using namespace std; int main(){ string encoder; string decoder; while(cin>>encoder>>decoder){ //先加密 string to_encoder=""; string to_decoder=""; for(int i = 0; i < encoder.size();i++){ if(encoder[i]>='a'&&encoder[i]<='z'){ if(encoder[i]=='z'){ to_encoder+= 'A'; }else{ to_encoder += toupper(encoder[i]+1); } }else if(encoder[i]>='A'&&encoder[i]<='Z'){ if(encoder[i]=='Z'){ to_encoder+= 'a'; }else{ to_encoder += tolower(encoder[i]+1); } }else if(encoder[i]>='0'&&encoder[i]<='9'){ if(encoder[i]=='9'){ to_encoder+= '0'; }else{ to_encoder += encoder[i] + 1; } }else{ to_encoder += encoder[i] ; } } for(int i = 0; i < decoder.size();i++){ if(decoder[i]>='a'&&decoder[i]<='z'){ if(decoder[i]=='a'){ to_decoder+= 'Z'; }else{ to_decoder += toupper(decoder[i]-1); } }else if(decoder[i]>='A'&&decoder[i]<='Z'){ if(decoder[i]=='A'){ to_decoder+= 'z'; }else{ to_decoder += tolower(decoder[i]-1); } }else if(decoder[i]>='0'&&decoder[i]<='9'){ if(decoder[i]=='0'){ to_decoder+= '9'; }else{ to_decoder += decoder[i] - 1; } }else{ to_decoder += decoder[i]; } } cout<<to_encoder<<endl; cout<<to_decoder<<endl; } return 0; }
大厂笔试题题解 文章被收录于专栏
主要是公司笔试题得一些总结