Language : C++ 4.8.2
#include<stdio.h> #include<string.h> int main(void) { int n; int sum; // 记录每一组数据里面所有空格的个数 int count; // 记录单行的个数 int length; int min_space; char str[30]; while(1) { min_space = 25; sum = 0; scanf("%d\n", &n); // 注意这里scanf里面的‘\n‘,它不是那种常见的bug,而是为下面的fgets做铺垫的,否则fgets的第一次读取会向str中填充一个换行符然后以‘\0‘结尾。 if(n == 0) break; int m = n; while(m--) // 一开始没有保存n的值,而是直接n--,导致后面printf计算的时候杯具了,输出结果一看,立即意识到了这块有问题,遂改之。 { count = 0; fgets(str, sizeof(str), stdin); length = strlen(str) - 1; // fgets读取字符长度和实际字符长度在空间足够的情况下是有差别的。 for(int i = 0; i < length; i++) { if(str[i] == ‘ ‘) { sum++; count++; } } if(min_space > count) min_space = count; } printf("%d\n", sum-n*min_space); } return 0; }
时间: 2024-08-05 22:13:13