赛码网-打字100%AC代码(C语言)

————————————————————————————————————
⏩ 大家好哇!我是小光,嵌入式爱好者,一个想要成为系统架构师的大三学生。
⏩最近在准备秋招,一直在练习编程。
⏩本篇文章对赛码网的打字题目做一个详解。
⏩感谢你的阅读,不对的地方欢迎指正。
————————————————————————————————————
题目:
赛码网-打字100%AC代码(C语言)_第1张图片
分析:
只要判断出在输出本个字符时确认是使用了shift还是Capslock就可以了。
当出现中间大于等于两个连续的大写或者小写字母时我们就得使用Capslock,当出现一个时可以用shiift。
看代码:

#include "stdio.h"
int sgn(char c)
{
    if(c >= 'A' && c <='Z') return 1;
    else if(c >= 'a' && c <= 'z') return -1;
    return 0;
}
int main()
{
    int T,Cap = 0;
    char s[105];
    scanf("%d",&T);
    for(int i = 0; i < T;i++)
    {
        int j = 0,count = 0;
        scanf("%s",s);
//        printf("%s\n",s);
        for(;s[j];j++){
        	Cap = 0;
            count ++;
            if(!sgn(s[j])) continue;
            else if(!s[j+1]){
            	if(Cap == 0 && sgn(s[j]) ==1) count++;
            	else if(Cap == 1 && sgn(s[j]) == -1) count++;
			}
            else if(Cap == 1) {
				if(sgn(s[j])== 1) ;
				else if(sgn(s[j]) == -1 && sgn(s[j+1]) == -1) {//连续两个使用Cap切换
					Cap = 0;//切换小写
					count++; 
				}
				else if(sgn(s[j]) == -1 && sgn(s[j+1]) == 1){//只有一个使用shift
					count++;
				}
            }
            else if(Cap == 0){
            	if(sgn(s[j]) == -1) ;
            	else if(sgn(s[j]) == 1 && sgn(s[j+1]) == 1){//连续两个使用Cap切换
            		count++;
            		Cap = 1;//切换大写 
				}
				else if(sgn(s[j]) == 1 && sgn(s[j+1]) == -1){//只有一个使用shift
					count++;
				}
			}
        }
        printf("%d\n",count);
    }
    return 0;
}

你可能感兴趣的:(c语言,开发语言)