pat-B1033-旧键盘打字

题目链接->link

思路

  1. 思路和B1029类似,但是有几个坑。
  2. 首先用hashmap数组存储不能输出的字符,设为false,初始化用memset(),初值为true,不能像B1029初始化为bool hashmap[]=false。
  3. 注意坏掉的字符统一用大写输入,那么要将其转换为小写,将小写的hashmap[]设为false。
  4. 当要输出大写字母时,仅当’+'号和其小写字母对于hashmap[]值为true时,才能输出。
  5. 测试点3是键盘不坏,即输入第一行为空时,要用gets()读取,因为scanf()读到空格就结束;但是pat的gcc不支持gets(),所以可以用fgets(str1,maxn,stdin)或者getline()读入。

代码

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>

using namespace std;
const int maxn=100005;
char str1[maxn],str2[maxn];
bool hashmap[128];

int main(){
    memset(hashmap,true,sizeof(hashmap));//存储字符是否可以输出
    fgets(str1, maxn, stdin);
    fgets(str2, maxn, stdin);
    int i;
    int len1=strlen(str1);
    for(i=0;i<len1;i++){//枚举不能输出的所有字符
        char c1=str1[i];
        if(c1>='A'&&c1<='Z')c1+=32;//大写转小写
        hashmap[c1]=false;//更新不能输出的字符
    }
    int len2=strlen(str2);
    for(i=0;i<len2;i++){//枚举应该输入的字符串
        char c2=str2[i];
        if(c2>='A'&&c2<='Z'){//如果要输入大写
            c2+=32;//大写转小写
            if(hashmap['+']==true&&hashmap[c2]==true){//如果+号和小写字母均为true才能输出
                printf("%c",c2-32);//小写还原成大写
            }
        }
        else if(hashmap[c2]==true){
            printf("%c",c2);//非大写字母,只要键位为true均可输出
        }
    }
    printf("\n");//没有字符可以输出,则输出空行
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值