zinber
问题分析; 注意一点:1---a,2---b,3---c ... 25----y, 0 ------z 如何简单的实现这一对于呢?主要是把o对应到z不好弄。 代码: #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <vector> #include <queue> #include <stack> #include <map> #include <string> #include <algorithm> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { int n; scanf("%d",&n); getchar(); while(n--){ char input[200]; gets(input); //cout<<input<<endl; int count=0; int length=strlen(input); for(int i=0;i<length;i++){ if(input[i]>='a' && input[i]<='z'){ count++; } } //cout<<count<<endl; if(count& == 0){ printf("z\n"); }else{ printf("%c\n",(count&)-1+'a'); } } return 0; }优秀代码: 01. #include <cstdio> 02. #include <cctype> 03. #include <cstring> 04. int main(){ 05. int n,i; 06. char arr[201]; 07. scanf("%d",&n); 08. while(n--){ 09. scanf("%s",arr); 10. int l=strlen(arr),r=0; 11. for(int i=0;i!=l;i++) 12. if(islower(arr[i])) 13. r++; 14. r%=26; 15. printf("%c\n",r==0?'z':96+r); 16. } 17. } 对比分析: 优秀代码用了 islower函数来判断一个字符是否为小写字母。 和我写的if应该差不多。优秀代码在处理数字与字母映射输出时用了 r==0? 'z' :96+r 原理上和我的都是利用'a'字符的ascii码,不过它用了?运算符看起来更简洁一些。