zoukankan      html  css  js  c++  java
  • 第五章-数字 课后答案

    5-1

    普通整型和长整型能表示的数据的位数不同。用大写字母"L"加在后面表示长整型,目前整型和长整型正在逐渐缓慢的统一。

    5-2

    (a)

    def plus(a, b):
        return a*b;

    (b)

    >>>plus(4, 5)
    20

    5-3

     1 def scoreOut(x):
     2     scoreDic = {9:"A",8:"B",7:"C",6:"D"}
     3     score  = x / 10
     4     for item in sorted(scoreDic.keys(),reverse = True):
     5         if score >= item:
     6             out = scoreDic[item]
     7             break
     8         else:
     9             out = "F"
    10     return out
    11 
    12 score = input("Enter your number,Please!")
    13 print "Your level is: %s" % scoreOut(score)

     5-4

    1 def leap_year(n):
    2     if (n % 4==0 and n % 100!=0) or n % 400==0:
    3         return "is";
    4     else:
    5         return "is not";
    6 
    7 year = input("Please input the year: ");
    8 print "Year %d %s a leap year!" % (year, leap_year(year))

    5-5

     1 def cal_money(x):
     2     cent_25 = x/25
     3     cent_10 = (x - 25 * cent_25)/10
     4     cent_5 = (x - 25 * cent_25 - 10 * cent_10)/5
     5     cent_1 = x - 25 * cent_25 - 10 * cent_10 - 5 * cent_5
     6     return cent_25,cent_10,cent_5,cent_1
     7 
     8 cent = input("input your cent,please: ")
     9 a, b, c, d = cal_money(cent)
    10 print "%d convert to %d 25cent, %d 10cent, %d 5cent, %d 1cent" % (cent, a, b, c, d)

    5-6

     1 def calculate(str):
     2     sList = str.split(" ")
     3     if sList[1] == "+":
     4         return int(sList[0])+int(sList[2])
     5     elif sList[1] == "-":
     6         return int(sList[0])-int(sList[2])
     7     elif sList[1] == "*":
     8         return int(sList[0])*int(sList[2])
     9     elif sList[1] == "/":
    10         return int(sList[0])/int(sList[2])
    11     elif sList[1] == "%":
    12         return int(sList[0])%int(sList[2])
    13     elif sList[1] == "**":
    14         return int(sList[0])**int(sList[2])
    15     else:
    16         return "error operator"
    17 
    18 while True:
    19     strEval = raw_input("please input the eval(q to quit): ")
    20     if strEval.lower() == "q":
    21         break
    22     print "the result is %d" % (calculate(strEval))

     5-8

     1 def square(x):
     2     return x*x
     3 
     4 def cube(x):
     5     return x*x*x
     6 
     7 import math
     8 def circle(x):
     9     return math.pi * x * x
    10 
    11 def ball(x):
    12     return 4.0/3 * math.pi * r * r * r

    5-9

    (a)因为017等数字代表的是八进制数字

    (b)56和78后面的是字母l,不是数字1,表示的是长整型

    5-10

    1 def temperature(x):
    2     return (x - 32) * (5.0/9.0)
    3 
    4 if __name__ == "__main__":
    5     strTem = input("input the temperature,please: ")
    6     print "the result is: %f" % temperature(strTem)

     5-11

    (a)

    1 def odd():
    2     aList = []
    3     for i in range(21):
    4         if i%2 == 0:
    5             aList.append(i)
    6     return aList

    (b)

    1 def even():
    2     aList = []
    3     for i in range(21):
    4         if i%2 == 1:
    5             aList.append(i)
    6     return aList

    (c)

    取余

    (d)

    1 def calculate():
    2     number_a = input("Please input a integer: ")
    3     number_b = input("Please input another integer: ")
    4     if number_a % number_b == 0 or number_b % number_a == 0:
    5         return True
    6     else:
    7         return False

    5-12

    1 import sys;     #加载sys模块
    2 
    3 #整数的范围,超过则自动转换为长整数
    4 print (sys.maxint);
    5 print(-sys.maxint-1);
    6 
    7 #浮点数的范围
    8 print(sys.float_info);
    9 print(sys.long_info);

    5-13

    1 def translate():
    2     time_str = raw_input("Please input the time, like 14:15:
    ")
    3     sList = time_str.split(":")
    4     if 0<=int(sList[0])<=24 and 0<=int(sList[1])<=60:
    5         time = int(sList[0]) * 60 + int(sList[1])
    6         return time
    7     else:
    8         return "error time!"

    5-15

    1 def GCM(num1, num2):
    2     tempNum = abs(num1 - num2)
    3     if (num1 % tempNum) == (num2 % tempNum) == 0:
    4         return tempNum
    5     else:
    6         return GCM(num2, tempNum)
    7 def LCM(num1, num2):
    8     tempNum = GCM(num1, num2)
    9     return num1 * num2 / tempNum

    5-17

     1 import random
     2 while 1:  
     3     N1=random.randint(2,100)  
     4     N2=random.randint(1,100)  
     5     if N1>N2:  
     6         break  
     7 print 'N1 is %d, N2 is %d' %(N1,N2)   
     8 list1=[]  
     9 for i in range(0,N1):  
    10     n=random.randint(0,2**31-1)  
    11     list1.append(n)      
    12 print list1  
    13 list2=random.sample(list1,N2)  
    14 list2.sort()  
    15 print list2
  • 相关阅读:
    CookieContainer.Add只能加入20个Cookie实例的解决方法
    ELM网络训练模型
    ELM学习(一)
    贝叶斯规律
    异方差性
    分类,回归,聚类
    机器学习中的正则化
    过拟合
    高斯分布——正态分布或钟形分布
    IDC:全球数据库
  • 原文地址:https://www.cnblogs.com/hell0x/p/5086312.html
Copyright © 2011-2022 走看看