L1-064 估值一亿的AI核心代码 (20 分) (屎山)

题目

在这里插入图片描述
在这里插入图片描述
输入样例:

6
Hello ?
 Good to chat   with you
can   you speak Chinese?
Really?
Could you show me 5
What Is this prime? I,don 't know

输出样例:

Hello ?
AI: hello!
 Good to chat   with you
AI: good to chat with you
can   you speak Chinese?
AI: I can speak chinese!
Really?
AI: really!
Could you show me 5
AI: I could show you 5
What Is this prime? I,don 't know
AI: what Is this prime! you,don't know

思路

这道题细节确实扣的有点细哦~~~
有几个坑要注意一下:
1.什么是独立的单词?不只是前后是空格,前后如果是标点都算是独立的!
2.将数据存入b的时候如果没有注意将最后一位设置结束符,就可能会炸一个点···
3.gets()输入操作在PTA中是不能用的,所以我采用了cin.get(a,n).get()作为连续输入的工具,可以学习一下

代码

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pre(i,a,b) for(int i=a;i>=b;--i)
#define m(x) memset(x,0,sizeof x)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI acos(-1)
typedef long long ll;
const int INF =  0x3f3f3f3f;
const int mod = 1e9+7;
const int maxn = 1e3+10;

//判断是不是标点,是则返回true
bool isBiaodian(char ch)
{
    if(ch>='a'&&ch<='z')return false;
    else if(ch>='A'&&ch<='Z')return false;
    else if(ch>='0'&&ch<='9')return false;
    else return true;
}

char Change(char ch)
{
    //如果是大写转换为小写
    if(ch>='A'&&ch<='Z')
    {
        if(ch=='I')return 'I';
        else return (char)(ch+'a'-'A');
    }
    //不是则原样输出
    else return ch;
}

int main()
{
    int n;
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        char a[maxn],b[maxn];
        int k = 0;
        cin.get(a,maxn).get();
        printf("%s\n",a);
        int len = (int)strlen(a);
        //cnt_s统计空格个数,f统计是否出现过非空格字符
        int cnt_s = 0,f = 0;
        rep(i, 0, len-1)
        {
            if(a[i]==' ')
            {
                cnt_s++;
            }else if(isBiaodian(a[i]))
            {
                f = 1;
                cnt_s=0;
                b[k++] = a[i];
            }else
            {
                if(f&&cnt_s)b[k++] = ' ';
                b[k++] = Change(a[i]);
                f = 1;
                cnt_s = 0;
            }
        }
        b[k] = '\0';//这个不加会炸一个点,需要标记字符串结束
        //cout << b << endl;
        cout << "AI: ";
        rep(i,0,k-1)
        {
            //printf("%c",b[i]);
            if(b[i]=='?')printf("!");
            else if(b[i]=='I'&&(i==0||isBiaodian(b[i-1]))&&isBiaodian(b[i+1]))printf("you");
            else if(b[i]=='m'&&b[i+1]=='e'&&(i==0||isBiaodian(b[i-1]))&&isBiaodian(b[i+2])){printf("you");i+=1;}
            else if(b[i]=='c'&&b[i+1]=='a'&&b[i+2]=='n'&&b[i+3]==' '&&b[i+4]=='y'&&b[i+5]=='o'&&b[i+6]=='u'&&(i==0||isBiaodian(b[i-1]))&&isBiaodian(b[i+7])){printf("I can");i+=6;}
            else if((b[i]=='c'&&b[i+1]=='o'&&b[i+2]=='u'&&b[i+3]=='l'&&b[i+4]=='d'&&b[i+5]==' '&&b[i+6]=='y'&&b[i+7]=='o'&&b[i+8]=='u')&&
                    (i==0||isBiaodian(b[i-1]))&&(isBiaodian(b[i+9]))){printf("I could");i+=8;}
            else printf("%c",b[i]);
        }
        cout << endl;
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值