题解 | #简单密码#
简单密码
http://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
简单密码:C语言解法
如果用很多if语句去判断小写字母会显得代码很臃肿,可以尝试找一下规律,字母三个为一组(特殊的可以之后在判断),下一组代表的数字字符加一,那么我们可以除3获得这个除数加上初始字符的值,即可获得小写字母代表的数字字符
#include<stdio.h>
int main(){
int i = 0;
char pd[100] = {0};
while(~scanf("%s\n",&pd)){
while(pd[i] != '\0'){
if(pd[i]>='a' && pd[i]<='p'){
pd[i] = (pd[i]-'a')/3 + '2';
}else if(pd[i]>='q' && pd[i]<='z'){
pd[i] = (pd[i]-'q')/3 + '7';
if(pd[i] > '9') pd[i] -= 1;
}else if(isupper(pd[i])){
pd[i] += ('z'-'Z'+1);
if(pd[i] > 'z') pd[i] = 'a';
}
i++;
}
printf("%s\n",pd);
}
}