QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#456479 | #7703. Base Hi-Lo Game | yongwhan | AC ✓ | 0ms | 1648kb | C++14 | 3.5kb | 2024-06-28 04:59:41 | 2024-06-28 04:59:43 |
Judging History
answer
/*
* bhilo_jb.cpp : Solution to interactve problem of guessing arbritray based value
* John Buck, Greater New York Chief Judge
*/
//#define _DEBUG
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define CORRECT_ANSWER "correct"
#define CHEATER_ANSWER "cheater"
#define MAX_NUM_DIGS 64
char testcase[MAX_NUM_DIGS + 1];
char lastguess[MAX_NUM_DIGS + 1];
int testdigits[MAX_NUM_DIGS];
int maxdigits[MAX_NUM_DIGS];
int mindigits[MAX_NUM_DIGS];
char response[MAX_NUM_DIGS + 10];
const char *szDigs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
void send_guess(int nLen)
{
int i;
char c;
for(i = 0; i < nLen; i++){
c = szDigs[testdigits[i]];
lastguess[i] = c;
putc(c, stdout);
}
lastguess[i] = '\0';
putc('\n', stdout);
fflush(stdout);
#ifdef _DEBUG
fprintf(stderr, "Sent guess \"%s\" to judge\n", &(lastguess[0]));
#endif
}
void cheater()
{
char szLine[32];
char fmtbuf[32];
fprintf(stdout, "%s\n", CHEATER_ANSWER);
fflush(stdout);
snprintf(&(fmtbuf[0]), sizeof(fmtbuf), "%%%lus", sizeof(szLine-1));
/* Need to wait for response */
scanf(&(fmtbuf[0]), &(szLine[0]));
#ifdef _DEBUG
fprintf(stderr, "Got back %s from judge after telling him he was cheating\n", &(szLine[0]));
#endif
}
void check_case(int nc, int base)
{
int nLen, i, nDig;
char fmtbuf[32];
if(scanf("%d", &(nLen)) != 1){
fprintf(stderr, "Error reading length for case %d\n", nc);
return;
}
#ifdef _DEBUG
fprintf(stderr, "check_case %d - len=%d\n", nc, nLen);
#endif
if(nLen > MAX_NUM_DIGS){
fprintf(stderr, "Too many digits requested %d, max is %d\n", nLen, MAX_NUM_DIGS);
return;
}
/* Form initial guess */
for(i = 0; i < nLen; i++){
testdigits[i] = base / 2;
maxdigits[i] = base - 1;
mindigits[i] = 0;
}
/* Enough for "correct" */
::snprintf(&(fmtbuf[0]), sizeof(fmtbuf), "%%%ds", nLen + 8);
for(;;){
send_guess(nLen);
/* Read response */
if(scanf(&(fmtbuf[0]), &(response[0])) != 1){
fprintf(stderr, "Bad read on response from judge\n");
return;
}
#ifdef _DEBUG
fprintf(stderr, "Got \"%s\" from judge for guess \"%s\"\n", &(response[0]), &(lastguess[0]));
#endif
if(strcmp(&(response[0]), CORRECT_ANSWER) == 0){
return;
}
if(::strlen(&(response[0])) != nLen){
fprintf(stderr, "Bad response length from judge response: \"%s\"\n", &(response[0]));
return;
}
for(i = 0; i < nLen; i++){
if(response[i] != '='){
break;
}
}
if(i >= nLen){
cheater();
return;
}
for(i = 0; i < nLen; i++){
if(response[i] == '-'){
nDig = testdigits[i] - 1;
if(nDig < mindigits[i]){
cheater();
return;
}
maxdigits[i] = nDig;
if(maxdigits[i] < 0){
maxdigits[i] = 0;
}
} else if(response[i] == '+'){
nDig = testdigits[i] + 1;
if(nDig > maxdigits[i]){
cheater();
return;
}
mindigits[i] = nDig;
if(mindigits[i] >= base){
mindigits[i] = base - 1;
}
} else {
/* Is there a cheat case here?? */
nDig = testdigits[i];
mindigits[i] = nDig;
maxdigits[i] = nDig;
continue;
}
testdigits[i] = ((maxdigits[i] - mindigits[i]) / 2) + mindigits[i];
}
}
}
int main(int argc, char **argv)
{
int ic, base, num_cases;
if(scanf("%d %d", &(base), &(num_cases)) == 2){
for(ic = 0; ic < num_cases; ic++){
check_case(ic, base);
}
} else {
fprintf(stderr, "Error reading base and number of cases\n");
}
return(0);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 1464kb
input:
10 2 5 ----= -=++= +==+= correct 6 --++-+ =+-+-+ =+===+ correct
output:
55555 22225 02335 12345 555555 227727 236808 246809
result:
ok correct (2 test cases)
Test #2:
score: 0
Accepted
time: 0ms
memory: 1584kb
input:
38 2 1 + + + + + correct 3 --- +-= --= --= correct
output:
J S X Z a b JJJ 999 E49 B19 A09
result:
ok correct (2 test cases)
Test #3:
score: 0
Accepted
time: 0ms
memory: 1544kb
input:
10 6 3 -++ --+ +-+ correct 3 -=+ -++ correct 5 ++++= ++=-= +===+ correct 4 ---- ==== correct 4 ++++ ++++ ==== correct 4 ==== correct
output:
555 277 068 cheater 555 257 cheater 55555 77775 88765 cheater 5555 2222 cheater 5555 7777 8888 cheater 5555 cheater
result:
ok correct (6 test cases)
Test #4:
score: 0
Accepted
time: 0ms
memory: 1544kb
input:
62 2 4 ==== correct 64 ================================================================ correct
output:
VVVV cheater VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV cheater
result:
ok correct (2 test cases)
Test #5:
score: 0
Accepted
time: 0ms
memory: 1536kb
input:
10 10 2 -= -- correct 2 -= -- correct 2 -= -- correct 2 -= -- correct 2 -= -- correct 2 -+ -- +- correct 2 -- -+ ++ =+ correct 2 -- -+ ++ =+ correct 2 -- -+ ++ =+ correct 2 -- -+ ++ =+ correct
output:
55 25 cheater 55 25 cheater 55 25 cheater 55 25 cheater 55 25 cheater 55 27 06 cheater 55 22 03 14 cheater 55 22 03 14 cheater 55 22 03 14 cheater 55 22 03 14 cheater
result:
ok correct (10 test cases)
Test #6:
score: 0
Accepted
time: 0ms
memory: 1648kb
input:
8 2 2 correct 2 correct
output:
44 44
result:
ok correct (2 test cases)