[HDU] [3783] ZOJ

Problem Description

Loading...

Problem Analysis

The idea is to count the occurrences of ‘Z’, ‘O’, and ‘J’ respectively.
Then, as long as any count remains, output them in ZOJ order.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
#include<string.h>
int main() {
	char s[100];
	int i;
	int z,o,j;
	while(scanf("%s",s),s[0]!='E') {
		for(i=z=o=j=0; i<strlen(s); i++) {
			if(s[i]=='Z')
				z++;
			else if(s[i]=='O')
				o++;
			else if(s[i]=='J')
				j++;
		}
		for(i=0; i<strlen(s); i++) {
			if(z>0) {
				printf("Z");
				z--;
			}
			if(o>0) {
				printf("O");
				o--;
			}
			if(j>0) {
				printf("J");
				j--;
			}
		}
		printf("\n");
	}
	return 0;
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...