我和你
时间限制:
1000 ms | 内存限制:
65535 KB
-
描述
-
将一句话中的所有的“我”换成“你”,“你”换成“我”。
-
输入
-
多组测试数据
每组给出一句中文,每段少于100个汉字
以单独一个0结束
输出
- 对于每组测试数据,输出替换后的句子。 样例输入
-
我爱你 我是中国人 1234我 0
样例输出
-
你爱我 你是中国人 1234你
提示
注意:每个中文在windows下占两个字节,ubuntu默认使用UTF-8编码,每个中文占三个字节。本OJ判题环境为ubuntu
当然此题如果知道了对于一个中文字符来说,当在char型数组中表示时,那么其对应的字节的值会小于0.(数与字符不舍的情~)。我也不明白这是为什么。先当个知识点记下 吧。如果知道了这个,这个题就比较好解答了。。
从另一个方面来讲,不知道也不影响此题的解决。。
#include<iostream> #include<cstdio> #include<cstdlib> #include<vector> #include<algorithm> #include<stack> #include<queue> #include<set> #include<map> #include<string> #include<cstring> using namespace std; const int mx=11000; char sete[mx]; //使用string解决比较简单,因为string类中取字串,和比较的函数。 int main() { string str,sustr; while(cin>>str){ if(str.compare("0")==0) break; int len=str.length(); int llen=strlen("我"); for(int i=0;i<len;){ if(str[i]<0){ //小于0证明从这一位开始这是个中文。 sustr=str.substr(i,llen); i+=llen; if(sustr.compare("我")==0){ printf("你"); } else if(sustr.compare("你")==0){ printf("我"); } else cout<<sustr; } else printf("%c",str[i++]); } printf("\n"); } return 0; }
-
多组测试数据