【UVa】[1585]Score

Problem Description

Loading...

Problem Analysis

​ Use i to count consecutive Os; when an X appears, reset i to zero.

scanf version:

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
#include<string.h>
int main() {
	int n,sum,l,t;
	char s[100];
	scanf("%d",&n);
	while(n--) {
		scanf("%s",s);
		l=strlen(s);
		for(int i=t=sum=0; i<l; i++) {
			if(s[i]=='X')
				t=0;
			else if(s[i]=='O') {
				sum+=++t;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}

getchar version:

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
int main() {
	int n,sum,i;
	char t;
	scanf("%d\n",&n);
	while(n--) {
		i=sum=0;
		while(t=getchar(),t!='\n') {
			if(t=='X')
				i=0;
			else if(t=='O') {
				sum+=++i;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}

To be honest, I don’t know why I couldn’t get it right the first time I wrote it.

Then today I suddenly wrote it out directly for some mysterious reason.

Problem link:【UVa】[1585]Score

Loading...