题目类型 简单题
题目意思
给你一个 字符数不超过100的 仅由小写字母与字符'/'构成的 字符串
然后把字符串的一些不必要的字符去掉了
1.如果出现连续的'/'那么只需要一个'/'即可
2.把路径名最后面那些'/'去掉了,除非路径名仅由一个'/'组成
解题方法
直接按意思构造结果串
参考代码 - 有疑问的地方在下方留言 看到会尽快回复的
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int main() { char str[1000]; while(scanf("%s", str) != EOF) { int len = strlen(str); for( int i=0; i<len; i++ ) { if(str[i] == '/') { i++; while(i<len && str[i] == '/') { str[i] = '.'; i++; } i--; } } int k = 0; char ts[1000]; for( int i=0; i<len; i++ ) if(str[i] != '.') ts[k++] = str[i]; while(k>0 && ts[k-1] == '/') k--; ts[k] = '\0'; if(k == 0) printf("/\n"); else printf("%s\n", ts); } return 0; }