——————————————————————————
《Updated 2015/07/15》
回头来看了看这题,觉得以前写的还是略显凌乱,写了一个用STL-String的
AC-Code:
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
//http://blog.youkuaiyun.com/okcd00/article/details/27373475
using namespace std;
int main()
{
string s,ans="";
cin>>s;
int len=s.length();
while(s[len-1]=='/')len--;
s=s.substr(0,len);
for(int i=0;i<len;i++)
{
if(s[i]!='/') ans=ans+s[i];
else if(i+1<len && s[i+1]!='/') ans=ans+s[i];
}
if(ans.length()==0)cout<<'/'<<endl;
else cout<<ans<<endl;
return 0;
}
——————————————————————————
看到这道题,吾辈欣喜了一小会~,哇咔咔,水题,秒~
然后写了这个:
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
bool flag=true;
char tmp;
while(scanf("%c",&tmp)!=EOF)
{
if(tmp=='/')flag=true;
else if(tmp!='/'&&flag==true)
{
printf("/%c",tmp);
flag=false;
}
else if(tmp!='/'&&flag==false)printf("%c",tmp);
}
return 0;
}
然后……WA了…… 原因是///a//这样的case输出了/a/ 多了一个结尾的/,百思不得其解
然后,
wrong output format Unexpected end of file - token expected
再然后:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char str[1000];
while(scanf("%s", str) != EOF) //get the whole line
{
int len = strlen(str); //length
for( int i=0; i<len; i++ )
{
if(str[i] == '/') //把连在一起的'/'换成'.'只留一个'/'
{
i++;
while(i<len && str[i] == '/') //下一个(不超出字符串)是否为'/'
{
str[i] = '.';
i++;
}
i--;
}
}
int k = 0;
char ans[1000];
for( int i=0; i<len; i++ ) if(str[i] != '.') ans[k++] = str[i];
while(k>0 && ans[k-1] == '/') k--;
ans[k] = '\0';//end
if(k == 0) printf("/\n");
else printf("%s\n", ans);
}
return 0;
}