空白格式化展开目录
本次大赛采用了全自动机器测评系统。如果你的答案与标准答案相差了一个空格,很可能无法得分,所以要加倍谨慎!但也不必过于惊慌。因为在有些情况下,测评系统会把你的答案进行 “空白格式化”。其具体做法是:去掉所有首尾空白;中间的多个空白替换为一个空格。所谓空白指的是:空格、制表符、回车符。
以下代码实现了这个功能。仔细阅读代码,填写缺失的部分。
- void f(char* from, char* to)
- {
- char* p_from = from;
- char* p_to = to;
-
- while(*p_from==' ' || *p_from=='\t' || *p_from=='\n') p_from++;
-
- do{
- if(*p_from==' ' || *p_from=='\t' || *p_from=='\n'){
- do{p_from++;} while(*p_from==' ' || *p_from=='\t' || *p_from=='\n');
- if(____________________) *p_to++ = ' '; //填空位置
- }
- }while(*p_to++ = *p_from++);
- }
题解展开目录
自己写个主函数,定义一个 char 型数组赋值,带进去试试,因为 char 型数组的结尾是 '0',而且 f 函数里都没有提到这个 '0',所以可以怀疑 if 里面就是要你判断还没到 '0' 的位置,也就是 * p_from != '0'
代码展开目录
- #include <bits/stdc++.h>
- using namespace std;
- void f(char* from, char* to)
- {
- char* p_from = from;
- char* p_to = to;
- while(*p_from == ' '||*p_from == '\t'||*p_from == '\n')
- p_from++;
- do
- {
- if(*p_from == ' '||*p_from == '\t'||*p_from == '\n')
- {
- do
- {
- p_from++;
- }while(*p_from == ' '||*p_from == '\t'||*p_from == '\n');
- if(*p_from != '\0')//填空位置
- *p_to++ = ' ';
- }
- }while(*p_to++ = *p_from++);
- }
- int main()
- {
- char s[] = " 1 5 3 ";
- f(s,s);
- puts(s);
- }