Codeforces 734A not working properly

It does compile, but if I put 4 AAAD, it shows friendship and if i put 4 ADAD shows Danik,but when i type 4 DDDA shows Danik, as it should. Whats the mistake?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include<stdio.h>
int main(){
	int a=0, d=0, n, i;
	char b[1000000];
	scanf("%d", &n);
	for(i=1;i<=n;i++)
		scanf("%c", &b[i]);
	for(i=1;i<=n;i++)
	switch(b[i]){
		case 'A': a++;
		case 'D': d++;
	}
	if(a>d)
		printf("Anton");
 	else if(a<d)
		printf("Danik");
	else if(a==d)
		printf("Friendship");
	return 0;
}
Last edited on
You probably would've gotten help earlier if you had described the problem. I had to google "Codeforces 734A" to see what you're trying to do.

A couple of problems. Firstly, the way you are reading the characters you don't skip the space (or newline) between the number and the letters in the input, so you will read the space/newline as the first "character". You can fix that by putting a space before the " %c" in the scanf format, which causes it to skip spaces before reading a character.

You should also remember that array indices start at 0 and go to one less than the size of the array. So your loop should start at 0 and go to i < n.

And you forgot the "break"s in your switch cases, so every 'A' is counted as both an A and a D.

Putting all that together yields something like:

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
#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);

    char b[100000];  // this only needs to be one hundred thousand
    for (int i = 0; i < n; i++)
        scanf(" %c", &b[i]);

    int a = 0, d = 0;
    for (int i = 0; i < n; i++)
        switch (b[i]) {
            case 'A': a++; break;
            case 'D': d++; break;
        }

    if (a > d)
        printf("Anton\n");
    else if(a < d)
        printf("Danik\n");
    else // a and d must be equal
        printf("Friendship\n");

    return 0;
}

Last edited on
Topic archived. No new replies allowed.