Codeforces Round #241 (Div. 2) A. Guess a number!

题目链接

题意 : 就是猜数游戏,根据给定的操作,让你输出一个符合条件的。

思路 : 这个题好玩儿,设置两个变量,一个找符合条件的数的上限,一个找下限,再判断一下。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 
 5 using namespace std ;
 6 
 7 char s[5] ;
 8 
 9 int judge()
10 {
11     if(strlen(s) == 2)
12     {
13         if(s[0] == '<')
14             return 4 ;
15         else return 3 ;
16     }
17     if(strlen(s) == 1)
18     {
19         if(s[0] == '<')
20             return 2 ;
21         else return 1 ;
22     }
23     return 0;
24 }
25 
26 int main()
27 {
28     int n,x ;
29     char c ;
30     scanf("%d",&n) ;
31     int ansx = -999999,ansn  = 9999999;
32     while(n--)
33     {
34         memset(s,0,sizeof(s)) ;
35         scanf("%s %d %c",s,&x,&c) ;
36         int a = judge() ;
37 
38         if(a == 1)
39         {
40             if(c == 'Y')
41                 ansx = max(x+1,ansx) ;
42             else ansn = min(x,ansn) ;
43         }
44         if(a == 2)
45         {
46             if(c == 'Y')
47                 ansn = min(x-1,ansn) ;
48             else ansx = max(x,ansx) ;
49         }
50         if(a == 3)
51         {
52             if(c == 'Y')
53                 ansx = max(ansx,x) ;
54             else ansn = min(x-1,ansn) ;
55         }
56         if(a == 4)
57         {
58             if(c == 'Y')
59                 ansn = min(ansn,x) ;
60             else ansx = max(ansx,x) ;
61         }
62     }
63     if(ansx > ansn)
64         printf("Impossible
") ;
65     else printf("%d
",ansx) ;
66     return 0 ;
67 }
View Code
原文地址:https://www.cnblogs.com/luyingfeng/p/3662762.html