[关闭]
@sensitive-cs 2016-10-19T08:47:08.000000Z 字数 534 阅读 774

C - Postcards and photos

分析:

一个字符串只包含两个字母,从第一个起开始取,每当遇到不同的字母或者取的相同的字母达到5时,取字母的次数便增加一,然后从结束位置的下一个开始取。

思路:

统计每个字母连续出现的个数,用一个数组b保存,当大于5时重新计数。比如说qqwwwqwqwwwwww,则它们出现的字数就为2,3,1,1,1,5,1。
最后,对于数组b中的每一个数,如果小于等于5,则result+1;若大于5且对5
取余不为0,则result += b[i]/5+1;若取余为0,则result += b[i]/5。

代码:

  1. #include <stdio.h>
  2. char a[110];
  3. char b[110];
  4. int main()
  5. {
  6. int i = 0,j = 0,result = 0;
  7. scanf("%s",a);
  8. while (a[i] != '\0')
  9. {
  10. char flag = a[i];
  11. while(a[i] == flag)
  12. {
  13. b[j]++;
  14. i++;
  15. }
  16. j++;
  17. }
  18. for (i = 0;i < j;i++)
  19. {
  20. if (b[i] < 5)
  21. result++;
  22. else if (b[i] % 5 == 0)
  23. result += b[i] / 5;
  24. else
  25. result += (b[i] / 5 + 1);
  26. }
  27. printf("%d\n",result);
  28. return 0;
  29. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注