题目链接

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

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

 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\n") ;
65     else printf("%d\n",ansx) ;
66     return 0 ;
67 }
View Code

相关文章:

  • 2022-02-10
  • 2021-07-03
  • 2021-12-22
  • 2021-11-19
  • 2022-01-09
  • 2021-08-04
  • 2021-11-25
  • 2021-11-26
猜你喜欢
  • 2021-10-01
  • 2021-06-03
  • 2021-11-12
  • 2021-12-19
  • 2021-09-01
  • 2021-09-24
  • 2021-10-16
相关资源
相似解决方案