Guessing Game

题目描述

Stan and Ollie are playing a guessing game. Stan thinks of a number between 1 and 10 and Ollie guesses what the number might be. After each guess, Stan indicates whether Ollie's guess is too high, too low, or right on.

After playing several rounds, Ollie has become suspicious that Stan cheats; that is, that he changes the number between Ollie's guesses. To prepare his case against Stan, Ollie has recorded a transcript of several games. You are to determine whether or not each transcript proves that Stan is cheating.

Standard input consists of several transcripts. Each transcript consists of a number of paired guesses and responses. A guess is a line containing single integer between 1 and 10, and a response is a line containing "too high", "too low", or "right on". Each game ends with "right on". A line containing 0 follows the last transcript.

For each game, output a line "Stan is dishonest" if Stan's responses are inconsistent with the final guess and response. Otherwise, print "Stan may be honest".

样例输入

10
too high
3
too low
4
too high
2
right on
5
too low
7
too high
6
right on
0

样例输出

Stan is dishonest
Stan may be honest

题意:

斯坦和奥利在玩猜谜游戏。斯坦想到了1到10之间的数字,奥利猜测这个数字可能是什么。在每次猜测之后,斯坦都指出奥利的猜测是太高、太低还是正确。

打了几回合后,奥利怀疑斯坦作弊,也就是说,他在奥利的猜测之间改变了数字。为了准备他对斯坦的案子,奥利录下了一些比赛的录音。你要确定每份成绩单是否都能证明斯坦在作弊。

标准输入由几份成绩单组成。每份成绩单都有一些配对的猜测和反应。a猜是包含1与10之间的单整数的行,而一个响应则是包含“过高”、“过低”或“正确”的行。每个游戏都以“正确”结尾。在最后的成绩单后面有一行包含0。

对于每个游戏,如果斯坦的反应与最后的猜测和反应不一致,那么输出一行“斯坦是不诚实的”。否则,打印“斯坦可能是诚实的”。

#include<iostream>
#include<cstring>
using namespace std;
 
int main()
{
	int a;
	char ans[10];
	while(1)
	{
		int Max  = 100;
		int Min  = 0;
		cin>>a;
		if(a == 0)
			break;
		getchar();   
		gets(ans);
		
		while(1)
		{
			
			if(strcmp(ans,"right on") == 0)
				break;
			if(strcmp(ans,"too high") == 0&&a<Max)
				Max = a;
 
			if(strcmp(ans,"too low") == 0&&a>Min)
				Min = a;
			cin>>a;
			getchar();   
			gets(ans);
		}
		
		if( a > Min && a < Max )
			
			printf("Stan may be honest\n");
		else
			printf("Stan is dishonest\n");
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
function varargout = caishuzi(varargin) %CAISHUZI M-file for caishuzi.fig % CAISHUZI, by itself, creates a new CAISHUZI or raises the existing % singleton*. % % H = CAISHUZI returns the handle to a new CAISHUZI or the handle to % the existing singleton*. % % CAISHUZI('Property','Value',...) creates a new CAISHUZI using the % given property value pairs. Unrecognized properties are passed via % varargin to caishuzi_OpeningFcn. This calling syntax produces a % warning when there is an existing singleton*. % % CAISHUZI('CALLBACK') and CAISHUZI('CALLBACK',hObject,...) call the % local function named CALLBACK in CAISHUZI.M with the given input % arguments. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help caishuzi % Last Modified by GUIDE v2.5 08-Mar-2008 22:02:40 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @caishuzi_OpeningFcn, ... 'gui_OutputFcn', @caishuzi_OutputFcn, ... 'gui_LayoutFcn', [], ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before caishuzi is made visible. function caishuzi_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin unrecognized PropertyName/PropertyValue pairs from the % command line (see VARARGIN) % Choose default command line output for caishuzi handles.output = hObject; handles.source = rand4; %生成一个随机的四位数 handles.pc = 0; %用来保存按‘ok’键的次数,但不成功 % Update handles structure guidata(hObject, handles); % UIWAIT makes caishuzi wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = caishuzi_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; function edit_input_Callback(hObject, eventdata, handles) % hObject handle to edit_input (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit_input as text % str2double(get(hObject,'String')) returns contents of edit_input as a double handles.instr = str2double(get(hObject,'String')); %从编辑框取输入值 guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function edit_input_CreateFcn(hObject, eventdata, handles) % hObject handle to edit_input (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in pushbutton_clo. function pushbutton_clo_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_clo (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) delete(handles.figure1) %关闭界面 % --- Executes on selection change in popupmenu. function popupmenu_Callback(hObject, eventdata, handles) % hObject handle to popupmenu (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: contents = get(hObject,'String') returns popupmenu contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu handles.bushu = get(hObject,'value'); %获得指定步数限制,还未完成该功能 % --- Executes during object creation, after setting all properties. function popupmenu_CreateFcn(hObject, eventdata, handles) % hObject handle to popupmenu (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in pushbutton_ok. function pushbutton_ok_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_ok (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) A = handles.source; %A为生成的随机4位数 instr = handles.instr; %输入的4位数 handles.pc = handles.pc + 1; %按键次数 b = 0; %将输入的4位数拆开,组成矩阵B for i=1:4 B(5-i) = mod(instr,10); for j=1:4 if isequal(B(5-i),A(j)) %判断B与A是否有相同元素,并将其个数存入b b=b+1; end end instr = fix(instr/10); end %判断输入是否合法 f=0; for i=1:4 C(1:4)=B(i); if sum(C==B)>=2 set(handles.text_rez, 'string','请重新输入4位不重复数!'); f=1; break end end if f==0 %输入合法 a=sum(A==B); %A与B的元素及其位置均相同的元素个数 b = b - a; %A与B的元素相同但位置不相同的元素个数 set(handles.text_rez, 'string',['第',num2str(handles.pc),'步: ',num2str(B),' ',... num2str(a),'A',num2str(b),'B']); %显示步骤及结果 end % --- Executes on button press in pushbutton_reset. function pushbutton_reset_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_reset (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.source = rand4; %重新生成一个随机4位数 guidata(hObject, handles); % --- Executes on button press in pushbutton_ans. function pushbutton_ans_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_ans (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.text_rez, 'string',['正确答案: ',num2str(handles.source)]); %显示正确答案 %定义生成随机4位数的函数 function Y=rand4() M=randperm(10); M(find(M==10))=0; Y=M(1:4);
Sure, I can help you with that! Here's an example implementation of the GuessingGame in Java: ```java // GuessingGame.java import java.util.Scanner; public class GuessingGame { private String secretWord; private int guessesLeft; public GuessingGame(String secretWord, int maxGuesses) { this.secretWord = secretWord; this.guessesLeft = maxGuesses; } public void play() { String guessedWord = ""; for (int i = 0; i < secretWord.length(); i++) { guessedWord += "*"; } Scanner scanner = new Scanner(System.in); while (guessesLeft > 0) { System.out.println("Guess a letter (" + guessesLeft + " guesses left):"); String guess = scanner.nextLine(); // Check if guess is correct boolean correctGuess = false; for (int i = 0; i < secretWord.length(); i++) { if (secretWord.charAt(i) == guess.charAt(0)) { guessedWord = guessedWord.substring(0, i) + guess + guessedWord.substring(i + 1); correctGuess = true; } } if (!correctGuess) { guessesLeft--; } System.out.println("Guessed word: " + guessedWord); if (guessedWord.equals(secretWord)) { System.out.println("Congratulations, you guessed the word!"); return; } } System.out.println("Sorry, you ran out of guesses. The word was " + secretWord); } } ``` And here's an example usage in a main class: ```java // Main.java public class Main { public static void main(String[] args) { GuessingGame game = new GuessingGame("house", 10); game.play(); } } ``` In this implementation, the `GuessingGame` class takes two parameters: the secret word to guess, and the maximum number of guesses allowed. The `play` method runs the game loop, prompting the user for guesses and updating the guessed word accordingly. If the guessed word matches the secret word, the game is won; if the user runs out of guesses, the game is lost.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值