问题:首先出示一件价格在999元以内商品,参与者要猜出这件商品的价格,在猜价格的过程中,主持人会根据参与者猜的价格进行相应的提示,“高了”或“底了”

代码实现:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cstring> 
#include<conio.h>
using namespace std;
int main(){
    int old_price,price = 0;
    int i = 0;
    cout<<"请输入商品的初始价格:";
    cin>>old_price;
    system("cls");
    cout<<"请输入试猜的价格:"<<endl;
    while(old_price != price){
        i++;
        cout<<"参与者:";
        cin>>price;
        cout<<"主持人:";
        if(old_price>price){
            cout<<"底了!"<<endl; 
        }
        else if(old_price<price){
            cout<<"高了!"<<endl;
        }
        else{
            cout<<"恭喜你猜对了,商品是你的了!\n\n你一共猜了:"<<i<<""<<endl;
        }    
    }
    getch();
    return 0;     
} 

算法入门(二分法—猜价格游戏)

相关文章:

  • 2021-12-12
  • 2021-08-26
  • 2021-06-18
  • 2021-06-09
  • 2021-11-29
  • 2021-11-30
  • 2021-10-31
  • 2021-09-27
猜你喜欢
  • 2022-12-23
  • 2021-04-26
  • 2021-12-05
  • 2021-12-06
  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
相关资源
相似解决方案