#include<iostream>#include<cstdlib>#include<cmath>#include<time.h>usingnamespace std;booll(int a,int n){if(a<n){
cout<<"The number of guesses is greater than the number of outputs"<<endl;}if(a>n)
cout<<"The number of guesses is less than the number of outputs"<<endl;return0;}boolf(int a,int n){if(a==n)return1;elsereturn0;}intmain(){
cout<<"Please Select Difficulty Level"<<endl;//请选择难度
cout<<"1. Easy"<<endl;//1.简单
cout<<"2. Difficulty"<<endl;//2.困难
cout<<"3. Hell"<<endl;//3.地狱int a1,timer=30,n,a;srand(time(0));
cin>>a1;
cout<<"Okay, I get it"<<endl;if(a1==1){
timer=30;
n=rand()%50+1;
cout<<"Your difficulty is simple, the number of fault tolerance is 30, the number of guesses is 1 to 50"<<endl;}elseif(a1==2){
timer=15;
n=rand()%150+1;
cout<<"Your difficulty is difficulty, fault tolerance is 15, the number of guesses is 1 to 150"<<endl;}elseif(a1==3){
timer=10;
n=rand()%250+1;
cout<<"Your difficulty is hell, the number of fault tolerance is 10, the number of guesses is 1 to 250"<<endl;}
cout<<"So ";
cout<<"The guessing game starts now!"<<endl;for(int i=1;;i++){
cout<<"Please output a number"<<endl;
cin>>a;if(timer-i==0){
cout<<"The game failed"<<endl;
cout<<"Don't lose heart"<<endl;break;}if(f(a,n)==1){
cout<<"Guessed right!"<<endl;
cout<<"Used "<<i<<" times";break;}elsel(a,n);
cout<<"You have "<<timer-i<<" chances left"<<endl;}return0;}
解析
#include<iostream>#include<cstdlib>#include<cmath>#include<time.h>usingnamespace std;booll(int a,int n){if(a<n){
cout<<"The number of guesses is greater than the number of outputs"<<endl;}if(a>n)
cout<<"The number of guesses is less than the number of outputs"<<endl;return0;}boolf(int a,int n){if(a==n)return1;elsereturn0;}intmain(){
cout<<"Please Select Difficulty Level"<<endl;//请选择难度
cout<<"1. Easy"<<endl;//1.简单
cout<<"2. Difficulty"<<endl;//2.困难
cout<<"3. Hell"<<endl;//3.地狱int a1,timer=30,n,a;srand(time(0));
cin>>a1;
cout<<"Okay, I get it"<<endl;if(a1==1){//对难度进行判断得出可猜的次数与范围
timer=30;
n=rand()%50+1;
cout<<"Your difficulty is simple, the number of fault tolerance is 30, the number of guesses is 1 to 50"<<endl;}elseif(a1==2){
timer=15;
n=rand()%150+1;
cout<<"Your difficulty is difficulty, fault tolerance is 15, the number of guesses is 1 to 150"<<endl;}elseif(a1==3){
timer=10;
n=rand()%250+1;
cout<<"Your difficulty is hell, the number of fault tolerance is 10, the number of guesses is 1 to 250"<<endl;}
cout<<"So ";
cout<<"The guessing game starts now!"<<endl;for(int i=1;;i++){
cout<<"Please output a number"<<endl;
cin>>a;if(timer-i==0){//判断失败情况
cout<<"The game failed"<<endl;
cout<<"Don't lose heart"<<endl;break;}if(f(a,n)==1){
cout<<"Guessed right!"<<endl;
cout<<"Used "<<i<<" times";break;}elsel(a,n);
cout<<"You have "<<timer-i<<" chances left"<<endl;}return0;}