原题链接:
字符串简单题,注意开有结尾有空格的情况否则pe or wa
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using std::string; 9 using std::vector;10 using std::cin;11 const int Max_N = 100010;12 char buf[Max_N], temp[Max_N];13 void reverse(char *src, int n){14 int i, j = n - 1;15 for (i = 0; i < j; i++, j--){16 char tp = src[i];17 src[i] = src[j];18 src[j] = tp;19 }20 }21 void solve(int n){22 int i, j;23 vector ans;24 for (i = 0; i < n; i++){25 if (buf[i] != ' ' || !i){26 j = 0;27 for (; buf[i] != ' ' && i < n; i++) temp[j++] = buf[i];28 temp[j] = '\0';29 reverse(temp, j);30 ans.push_back(temp);31 }32 }33 if (buf[n - 1] == ' ') ans.push_back("");34 int t = ans.size();35 for (i = 0; i < t; i++){36 printf("%s%c", ans[i].c_str(), i < t - 1 ? ' ' : '\n');37 }38 }39 int main(){40 #ifdef LOCAL41 freopen("in.txt", "r", stdin);42 freopen("out.txt", "w+", stdout);43 #endif44 int n;45 while (~scanf("%d", &n) && n){46 while (getchar() != '\n');47 gets(buf);48 solve(n); 49 }50 return 0;51 }