278 - Chess

All about problems in Volume 2. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

User avatar
plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

Re: 278 Chess

Post by plamplam » Wed Jun 29, 2011 7:48 pm

Nice problem :D The knights part is quite tricky though. Shafaet DU....thanks a lot for the test cases :).....the Knights caught me off-guard hehe :wink:
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 278 Chess

Post by uDebug » Thu Feb 27, 2014 1:55 pm

Shafaet_du wrote:This will certainly help you:
Thanks for sharing these test cases.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

facug91
New poster
Posts: 6
Joined: Sat Apr 26, 2014 10:32 pm

Re: 278 Chess

Post by facug91 » Sun May 18, 2014 3:49 am

Hi! I have a problem with the code in the input part.

Code: Select all

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <list>
#include <stack>
#include <iomanip>
#include <bitset>

using namespace std;

int t, m, n, ans;
char piece;

int main() {
	
	scanf("%d", &t);
	
	while (t--) {
		
		scanf("%c %d %d", &piece, &m, &n);
		
		/* code to solve the problem */
		
	}
	
	return 0;
} 
I've had AC using 'cin' instead of 'scanf', but I don't know what the problem is there. When I execute it, it's like the first time it doesn't read anything, so it finishes in less turns than it should.
As I sad, I've already had AC with 'cin', but I would be glad to know why it doesn't work with 'scanf'.
Thanks! :)

----------------------------------------------------------------------------------

Well, I've already found why. 'scanf' ignore white spaces, but evidently not newlines, so 'piece' reads the newlines instead the char it should. I solved it putting 'getchar()' after each 'scanf' :lol:

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 278 Chess

Post by uDebug » Mon May 19, 2014 9:20 am

facug91 wrote:Well, I've already found why. 'scanf' ignore white spaces, but evidently not newlines, so 'piece' reads the newlines instead the char it should. I solved it putting 'getchar()' after each 'scanf'
You're correct in your observation.

What you did for this problem might work but don't count on putting a getchar() at the end of each line to "catch" the newline every time. The better way to do this is to use "\n" in scanf like so:

Code: Select all

int t, m, n, ans;
char piece;
int main() {
    /* The "\n" after the "%d" handles the newline character so it's not left in the input stream */
	scanf("%d\n", &t);
	
	while (t--) {
		 /* The "\n" after the "%d" handles the newline character so it's not left in the input stream */
		scanf("%c %d %d\n", &piece, &m, &n);
		
		/* Code to solve the problem */
	}
	return 0;
} 
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

jeffprog
New poster
Posts: 2
Joined: Mon Jan 19, 2015 3:42 am

278 - Chess

Post by jeffprog » Mon Jan 19, 2015 3:49 am

What is wrong at my code?

Code: Select all

#include <stdio.h>

int minimo(int a, int b);

int main(){

	int t;
	char p;
	int m;
	int n;
	int i;

	scanf("%d",&t);

	for(i=0;i<t;i++){

		fflush(stdin);

		scanf("%c %d %d", &p, &m, &n);

		if(p == 'Q'){

			printf("%d\n", minimo(m,n));
		}

		if(p == 'r'){

			printf("%d\n", minimo(m,n));
		}

		if(p == 'k'){

			printf("%d\n", (m*n + 1)/2);
		}

		if(p == 'K'){

			printf("%d\n", ((m+1)/2) * ((n+1)/2));
		}
	}

	return 0;
}

int minimo(int a, int b){

	if(a < b){

		return a;
	}
	else{

		return b;
	}
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 278 - Chess

Post by brianfry713 » Mon Jan 19, 2015 9:28 pm

Next time post in the existing thread.
Don't use: fflush(stdin);
Check input and AC output for thousands of problems on uDebug!

jeffprog
New poster
Posts: 2
Joined: Mon Jan 19, 2015 3:42 am

Problem 278 (Chess)

Post by jeffprog » Wed Jan 21, 2015 3:22 am

What is wrong at my code? Please Help me...

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int minimo(int a, int b);

int main(){

	int t;
	char p;
	int m;
	int n;
	int i;

	scanf("%d",&t);
	setbuf(stdin, NULL);

	for(i=0;i<t;i++){

		scanf("%c %d %d", &p, &m, &n);

		setbuf(stdin, NULL);

		if(p == 'Q'){

			printf("%d\n", minimo(m,n));
		}

		if(p == 'r'){

			printf("%d\n", minimo(m,n));
		}

		if(p == 'k'){

			printf("%d\n", (m*n + 1)/2);
		}

		if(p == 'K'){

			printf("%d\n", ((m+1)/2) * ((n+1)/2));
		}
	}

	return 0;
}

int minimo(int a, int b){

	if(a < b){

		return a;
	}
	else{

		return b;
	}
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 278 - Chess

Post by brianfry713 » Wed Jan 21, 2015 11:17 pm

Your code isn't working correctly for the sample input on my machine.
On way to get AC is to remove the lines: setbuf(stdin, NULL);
And change your input parsing to:
scanf("%d\n",&t);
and
scanf("%c %d %d\n", &p, &m, &n);
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 2 (200-299)”