CS入门学习笔记1-MIT 6.00.1x

本文介绍了MIT6.00.1x课程第三讲的内容,涵盖迭代循环、穷举搜索、浮点精度、二分查找及牛顿-拉弗森算法等主题。通过一个猜数字的游戏程序,展示了如何使用二分查找算法来高效猜测用户心中的秘密数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MIT 6.00.1x 第三讲

主要内容包括:
· Iteration loops
· “Guess & Check” Algorithms
· Exhaustive enumeration
· Floating point accuracy
· Bisection Search
· Newton-Raphson Algorithm

ProblemSet:In this problem, you’ll create a program that guesses a secret number!

x = int( raw_input('Enter an integer between 0 and 100:'))
numGuesses = 0
low = 0
high = 100
ans = (high + low) / 2
while abs( ans - x ) >= 0:
    print ('Is your secret number '+str(ans)+'?')
    y = str( raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly."))
    if y == 'h':
        high = ans
    elif y == 'l':
        low = ans
    elif y == 'c':
        print('Game over. Your secret number was: '+ str(ans))
        break
    elif y =='quit':
        break
    else:
        print('Sorry, I did not understand your input.')
    
    ans = (high + low) / 2 

我的代码在本机上跑起来没问题,在线提交之后出现了错误,应该是由于他的输入设置和我的未能匹配的缘故,暂时还不知道原因,有待寻找。

另外在写代码的过程中遇到的问题是:
当用户输入不符合判断的字符时,要如何跳转回循环中最初的语句,粗略的查了一下之后似乎python中不支持goto等功能,有待研究。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值