赞
踩
以上图片来自新浪微博。
本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:
输入格式:
输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。
输出格式:
按题面要求输出,每个 AI 的回答前要加上 AI: 和一个空格。
输入样例:
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
这题做吐我了,对于字符串的处理很麻烦,但是不是很难。
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace std; int n; string str; int d(char x){ if(x>='A' && x<='Z') return -1; if(x>='a' && x<='z') return -2; if(x>='0' && x<='9') return -3; if(x==' ') return 2; return 1; } string convert(string str){ string b = " "; for(int i = 0; i < str.length(); i ++){ if(str[i]>='A' && str[i]<='Z' && str[i]!='I'){ str[i] += 32; } if(str[i]=='?') str[i] = '!'; } b += str; b += " "; str.clear(); str += " "; for(int i = 1; i < b.length()-1; i++){ if(b[i]==' '&& b[i-1]==' ')//消除连续空格 continue; else if(d(b[i+1])==1 && b[i]==' ')//排除标点符号前空格 continue; else str += b[i]; } str += ' '; return str; } int main(){ cin >> n; getchar(); while(n--){ getline(cin, str); cout << str << endl; cout << "AI: "; int flag = 0; for(int i = 0; i < str.length(); i ++){ if(str[i]!=' '){ flag = 1; break; } } if(flag==0){//排除全是空格 cout << endl; continue; } str = convert(str); //cout << str << endl; string temp; for(int i = 0; i < str.length(); i ++){ temp += str[i]; if(d(str[i])>0 && str[i+1]=='m'&& str[i+2]=='e' && d(str[i+3])>0){ i+=2; temp += "you"; }else if(d(str[i])>0 && str[i+1]=='I' && d(str[i+2])>0){ i++; temp += "you"; }else if(d(str[i])>0 && str[i+1]=='c' && str[i+2]=='a' && str[i+3]=='n' && str[i+4]==' ' && str[i+5]=='y' && str[i+6]=='o' && str[i+7]=='u' && d(str[i+8])>0){ i += 7; temp += "I can"; }else if(d(str[i])>0 && str[i+1]=='c' && str[i+2]=='o' && str[i+3]=='u' && str[i+4]=='l' && str[i+5]=='d' && str[i+6]==' ' && str[i+7]=='y' && str[i+8]=='o' && str[i+9]=='u' && d(str[i+10])>0){ i += 9; temp += "I could"; } } //cout << temp << endl; //cout << temp.length() << endl; str=""; str += temp; int len; for(int i = str.length()-1; i >= 0; i --){//找到最后一个不为空格的索引 if(str[i]!=' '){ len = i; break; } } int cnt = 0; for(int i = 0; i <= len; i ++){ if(i>0 && str[i]==' ' && d(str[i+1])==1) continue; if(str[i]!=' '){ cout << str[i]; cnt++; }else if(cnt>0){ cout << " "; } } cout << endl; } return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。