您的位置:首页 > 其它

题目 1049 字符串去特定字符 九度Online Judge

2015-03-24 19:08 423 查看
题目描述:
输入字符串s和字符c,要求去掉s中所有的c字符,并输出结果。

输入:
测试数据有多组,每组输入字符串s和字符c。

输出:
对于每组输入,输出去除c字符后的结果。

样例输入:
heallo
a


样例输出:
hello


第一种方法:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string s;   //×Ö·û´®
while (cin>>s)
{
char c;
cin>>c;
for (int i = 0; i < s.length(); i++)
{
if (s[i] != c)
{
cout<<s[i];
}
}
cout<<endl;
}
return 0;
}
/**************************************************************
Problem: 1049
User: Carvin
Language: C++
Result: Accepted
Time:0 ms
Memory:1520 kb
****************************************************************/


第二种方法:

/*
这种思路:
比较两个字符串数组,如果相同的则用一个特定的字符去替换。由于是字符串,比较特殊,所以选择用'\n'去替换!
有个疑问的就是为什么再输入的时候用getline,第一次显示的结果是对的,当进行第二轮测试的时候就不对了!如
果有兴趣的可以自己试试。
*/

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1,str2;
int i,j;
//while(cin,str1)
while (cin>>str1)
{

int x=i;
//getline(cin,str2); //不知道为什么,这里和while循环里用getline时,第一次比较显示是对的,第二次就不对了!
//如果有大牛知道知道,还望告知一下!
cin>>str2;
for (int i=0;i<str2.size();i++)
{
for (int j=0;j<str1.size();j++)
{
if (str2[i]==str1[j])
{
//  t[j]=str1[j];
//  str1[j]=str1[j+1];
str1[j]='\n';
// cout<<str1[j+1];
//continue;
}
//else
//  cout<<str1[j];
}
}

for (int j=0;j<str1.size();j++)
{
if (str1[j]=='\n')
{
continue;
}
else
cout<<str1[j];
}
cout<<endl;
}
return 1;
}
/**************************************************************
Problem: 1049
User: Carvin
Language: C++
Result: Accepted
Time:0 ms
Memory:1520 kb
****************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: