字母小游戏
2017-06-08 12:14
260 查看
字母小游戏
时间限制:1000 ms | 内存限制:65535 KB难度:0描述给你一个乱序的字符串,里面包含有小写字母(a--z)以及一些特殊符号,请你找出所给字符串里面所有的小写字母的个数, 拿这个数对26取余,输出取余后的数字在子母表中对应的小写字母(0对应z,1对应a,2对应b....25对应y)。输入第一行是一个整数n(1<n<1000)表示接下来有n行的字符串m(1<m<200)需要输入输出输出对应的小写字母 每个小写字母单独占一行样例输入2 asdasl+%$^&ksdhkjhjksd adklf&(%^(alkha样例输出
q j来源[zinber]原创上传者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%26 == 0){ printf("z\n"); }else{ printf("%c\n",(count%26)-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码,不过它用了?运算符看起来更简洁一些。