猜数字(模拟)

猜数字(模拟)

题目描述:

猜数字游戏啦!给你如下四种提示:

(1)这个数严格大于x吗?

(2)这个数严格小于x吗?

(3)这个数大于等于x吗?

(4)这个数小于等于x吗?

每个提示,都会给出相应的答案,yes或者no。

如果有多个数满足条件,输出最小的。如果不存在这样的数,输出“Impossible”。

输入格式:

第一行输入一个整数n。

接下来n行,每行一个字符串“sign x answer”,是四个提示的中的一个。

sign是“>”,"<","<=",">="; answer 是“Y”或者"N" ;

如(1)就会有类似这样的字符串“  > x Y” 或者“ > x N”;

输出格式:

如果最终的答案有下界的,则输出这个下界
如果存在答案,但是答案没有下界,输出-2000000000
否则输出“Impossible”

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 int main()
 5 {
 6     int a,xx=-21842,sx=999999; //下限和上限
 7     cin>>a;
 8     for(int i=0;i<a;i++)
 9     {
10         string s;
11         int b;
12         cin>>s;
13         if(s==">")
14         {
15             cin>>b;
16             cin>>s;
17             if(s=="Y")
18             {
19                 if(xx<b+1) xx=b+1; //改下限
20             }
21             else
22             {
23                 if(sx>b) sx=b; //改上限
24             }
25         }
26         else if(s=="<")
27         {
28             cin>>b;
29             cin>>s;
30             if(s=="Y")
31             {
32                 if(sx>b-1) sx=b-1; //改上限
33             }
34             else
35             {
36                 if(xx<b) xx=b; //改下限
37             }
38         }
39         else if(s==">=")
40         {
41             cin>>b;
42             cin>>s;
43             if(s=="Y")
44             {
45                 if(xx<b) xx=b; //改下限
46             }
47             else
48             {
49                 if(sx>b-1) sx=b-1; //改上限
50             }
51         }
52         else if(s=="<=")
53         {
54             cin>>b;
55             cin>>s;
56             if(s=="Y")
57             {
58                 if(sx>b) sx=b; //改上限
59             }
60             else
61             {
62                 if(xx<b+1) xx=b+1; //改下限
63             }
64         }
65     }
66     if(xx!=-21842&&xx<=sx) cout<<xx; //有解
67     else if(xx==-21842) cout<<"-2000000000"; //无下限
68     else cout<<"Impossible"; //无解
69     return 0;
70 }

原文地址:https://www.cnblogs.com/abs27/p/9188956.html

时间: 2024-08-05 00:54:21

猜数字(模拟)的相关文章

编程成长日记————猜数字游戏

模拟猜数字游戏,提示用户每次应输入的数字范围. #include <stdio.h> #include <stdlib.h> //猜数字游戏 int main() { int i=0; int num =0,ret=0,choose=0; int min=1,max=100; flag: srand((unsigned)time(NULL));  ret=rand()%100+1; while(1) { printf("请输入%d~%d之间的数字:\n",min

20170913自制猜数字游戏

/* 猜数字:系统随机生成一个四位数,请根据下列判断猜出来 A:数值正确,位置正确 B:数值正确,位置不正确 C:数值不正确 */ #include<stdio.h> #include<time.h> #include<stdlib.h> #pragma warning (disable:4996) #define pUCharHead unsigned char * //以数组形式返回n个无重复的随机数,范围可指定[min,max] pUCharHead GenNoR

JavaScript一个猜数字游戏

效果图: 代码: <body> <script type="text/javascript"> window.onload = newgame; //页面载入的时候就开始一个新的游戏 window.onpopstate = popState; //处理历史记录相关事件 var state,ui; //全局变量,在newgame()方法中会对其初始化 function newgame( playagin ){ //开始一个新的猜数字游戏 //初始化一个包含需要的文

函数调用_猜数字和简易计算器

package app1; import java.util.*; public class TestFunction{     public static void main(String[] args){         Scanner sc=new Scanner(System.in);         System.out.print("请选择一项应用:\n1.猜数字\n2.简易计算器");         int n=sc.nextInt();         switch(

【bzoj入门】3189 猜数字(数学,搜索)

Description 味味最近在玩猜数字的游戏,现在她也希望你来玩一下这个游戏.猜数字游戏的规则是这样的,告诉你一个正整数 n(2<=n<=11),然后味味心中会想一个 n 个数字组成的数字串 (数字串最前面若干位可能是 0).味味会随意排列 n 位数上的数字,这样可能产生 n!个 n 位数.(n!=1×2×3×4×5×......×n,n!念作"n 阶乘").比如味味想了一个三位数 abc,那么一共会产生六个三位数,分别为 abc,acb,bac,bca,cab,cba

猜数字游戏及rand()函数

#include<stdio.h>#include<stdlib.h>int main() { short number; short guess=0; number=rand()%100; number++; printf("猜数字游戏\n"); printf("该数字在1到100之间\n"); while(guess!=number) { printf("请你输入所猜数字:"); scanf("%hd&quo

简单的猜数字小游戏

/** 简单的猜数字小游戏 要求如下: 用户输入想猜测数字的范围,输入1000则是0~1000之内的数字,程序就会内置一个 1 到 1000 之间的数字作为猜测的结果,由用户猜测此数字,用户每猜测一次,由系统提示猜测结果:大了.小了或者猜对了:直到用户猜对结果,则提示游戏结束.用户可以提前退出游戏,即,游戏过程中,如果用户录入数字0则游戏终止.加入新功能: 记次猜测次数功能,提示游戏开始时间,计猜测总用时功能,提示游戏结束时间 思路:1.用户输入电脑生成的数值取值范围,接收并判断是否是合理数值?

猜数字-暴力枚举

A - 猜数字 Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status id=17473" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-block; position:rela

猜数字小游戏

#include <stdio.h> #include <stdlib.h> #include <time.h> void print_menu() { printf("**********************\n"); printf("******* 1.start ******\n"); printf("******* 0. exit ******\n"); printf("*********