Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Sort them in the correct order?
1 回表示 (過去 30 日間)
古いコメントを表示
% if a>b we swap the roles of a and b.
if (a > b)
c = a; a = b; b = c;
end
function computerGuess (a, b)
% function computerGuess (a, b)
% Game : the computer must guess the integer
% in the interval (a,b) which the user is thinking of.
% the computer asks the user to think of a number
disp(['Think of a number in ' num2str(a) ...
' and ' num2str(b)]);
% We keep track of the previous guess. It is initialized as a-1:
old_guess = a-1;
% Takes the integer part of a and b
a = floor(a);
b = ceil (b);
% loops while the computer guesses
% this loop is exited with a break
while (true)
% as a guess, the computer gives the middle of [a,b]
guess = ceil( (a+b)/2 );
if ( guess == old_guess )
% if the old guess is the same, then the computer is nearly there, the answer is:
guess = guess - 1;
end
elseif ( answer == 2 )
% we must keep the lower interval
b = guess;
answer = menu(['The number is ' num2str(guess) ' ? '], ...
'correct!', too high!', too low!');
else
% we must keep the upper interval
a = guess;
end
if ( answer == 1)
disp('I win !');
break;
old_guess = guess;
end
end
3 件のコメント
Stephen23
2017 年 7 月 4 日
@Meenakshi S: do you want to ask us a question, or are you just showing off your code?
回答 (0 件)
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!