Skip to content

Instantly share code, notes, and snippets.

@autekroy
Last active August 29, 2015 13:59
Show Gist options
  • Save autekroy/10975499 to your computer and use it in GitHub Desktop.
Save autekroy/10975499 to your computer and use it in GitHub Desktop.
Codeforces 416A Guess a number!
//This program is for Codeforces 416A Guess a number!
//題目來源 Problem link: http://codeforces.com/problemset/problem/416/A
/** my mistakes from code on paper
compiler errers:
1. reduceRange(char[] oper) --> reduceRange (char* oper)
2. char[] reverseOper(char op) --> char* reverseOper(char* op)
3. declare function reverseOper after the function reduceRange
problem-solving logic: right
*/
#include<stdio.h>
#include<string.h>
char* reverseOper(char* op)
{
if(strcmp(op, "<") == 0)
return ">=";
if(strcmp(op, "<=") == 0)
return ">";
if(strcmp(op, ">") == 0)
return "<=";
if(strcmp(op, ">=") == 0)
return "<";
}
void reduceRange(char* oper, int num, char ans, int& Max, int& Min)
{
if(ans == 'N')
oper = reverseOper(oper);
if(strcmp(oper, "<") == 0){
if(Max > (num - 1))
Max = (num - 1);
}
else if(strcmp(oper, "<=") == 0){
if(Max > num)
Max = num;
}
else if(strcmp(oper, ">") == 0){
if(Min < (num + 1))
Min = (num + 1);
}
else if(strcmp(oper, ">=") == 0){
if(Min < num)
Min = num;
}
}
int main()
{
int n, Max, Min, num;
char oper[3], ans;
while(scanf("%d", &n) != EOF)
{
Max = 2e9;
Min = -2e9;
while(n--)
{
scanf("%s %d %c", &oper, &num, &ans);
reduceRange(oper, num, ans, Max, Min);
}
//printf("%d\t%d\n", Min, Max);
if(Min > Max)
printf("Impossible\n");
else
printf("%d\n", Min);
}
return 0;
}
//This program is for Codeforces 416A Guess a number! (code on paper)
//題目來源 Problem link: http://codeforces.com/problemset/problem/416/A
//it's wrong and can't compile
#include<stdio.h>
#include<string.h>
void reduceRange(char[] oper, int num, char ans, int& Max, int& Min)
{
if(ans == 'N')
oper = reverseOper(oper);
if(strcmp(oper, "<") == 0){
if(Max > (num - 1))
Max = (num - 1);
}
else if(strcmp(oper, "<=") == 0){
if(Max > num)
Max = num;
}
else if(strcmp(oper, ">") == 0){
if(Min < (num + 1))
Min = (num + 1);
}
else if(strcmp(oper, ">=") == 0){
if(Min < num)
Min = num;
}
}
char[] reverseOper(char op)
{
if(strcmp(op, "<") == 0)
return ">=";
if(strcmp(op, "<=") == 0)
return ">";
if(strcmp(op, ">") == 0)
return "<=";
if(strcmp(op, ">=") == 0)
return "<";
}
int main()
{
int n, Max, Min, num;
char oper[3], ans;
while(scanf("%d", &n) != EOD)
{
Max = 2e9;
Min = -2e9;
while(n--)
{
scanf("%s %d %c", &oper, &num, &ans);
reduceRange(oper, num, ans, Max, Min);
}
if(Min > Max)
printf("Impossible\n");
else
printf("%d\n", Min);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment