D - Postcards and photos
Polycarpus has postcards and photos hung in a row on the wall. He decided to put them away to the closet and hang on the wall a famous painter's picture. Polycarpus does it like that: he goes from the left to the right and removes the objects consecutively. As Polycarpus doesn't want any mix-ups to happen, he will not carry in his hands objects of two different types. In other words, Polycarpus can't carry both postcards and photos simultaneously. Sometimes he goes to the closet and puts the objects there, thus leaving his hands free. Polycarpus must put all the postcards and photos to the closet. He cannot skip objects. What minimum number of times he should visit the closet if he cannot carry more than 5 items?
The only line of the input data contains a non-empty string consisting of letters"С" and"P" whose length does not exceed100 characters. If thei-th character in the string is the letter"С", that means that thei-th object (the numbering goes from the left to the right) on Polycarpus' wall is a postcard. And if thei-th character is the letter"P", than the i-th object on the wall is a photo.
Print the only number — the minimum number of times Polycarpus has to visit the closet.
CPCPCPC
7
CCCCCCPPPPPP
4
CCCCCCPPCPPPPPPPPPP
6
CCCCCCCCCC
2
In the first sample Polycarpus needs to take one item to the closet 7 times.
In the second sample Polycarpus can first take 3 postcards to the closet; then 3 more. He can take the 6 photos that are left in the similar way, going to the closet twice.
In the third sample Polycarpus can visit the closet twice, both times carrying 3 postcards. Then he can take there 2 photos at once, then one postcard and finally, he can carry the last 10 photos if he visits the closet twice.
In the fourth sample Polycarpus can visit the closet twice and take there all 10 postcards (5 items during each go).
题意:从墙上取下明信片或照片,规定按从左到右顺序每回只能取下一种(要么是明信片,要么是照片),且所取下的某种东西不能超过五个(即最多5个)。
### 注意:ave 应为 实数型,应为sum/n 可能为实数,开始时我就定义成int型了,然后 WA 了。
法 1. #include<stdio.h> #include<string.h> int main() { int i, j, len, count, num; char str[102], t; scanf("%s", str); num = 1; count = 0; t = str[0]; len = strlen(str); for(j=1; j<len; j++) { if(t == str[j]) { count++; if(count == 5) { num++; count = 0; } } else { t = str[j]; num++; count = 0; } } printf("%d\n", num); return 0; }
法 2.
#include <stdio.h> #include <string.h> int main() { int i,n=0; char s[103]; int c; scanf("%s",s); int l=strlen(s); i=0; int j=0; for(i=0;i<l;){ for(j=i+1;j<=i+4;j++){ if(s[i]!=s[j]) break; } n++; i=j; } printf("%d\n",n); return 0; }第二个为同学的代码,也挺好的,所以贴出来。
法 3.(这也是一种很好的思路,不用考虑ave 的类型了)
#include<stdio.h> int main() { int a[200005],b[200005],n,sum=0,num=0,i; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a[i]); sum+=a[i]; } for(i=1;i<=n;i++) { if(a[i]*n==sum) { b[++num]=i; } } printf("%d\n",num); if(num==0) return 0; for(i=1;i<=num;i++) { if(i==1)printf("%d",b[i]); else printf(" %d",b[i]); } printf("\n"); return 0; }