需要通过在终端输入题号,来选择完成题目!(友情提示:可以试试函数)
时间: 2024-10-14 17:12:16 浏览: 47
这是一个命令行交互式的程序设计思路,通常用于教育环境或者小型项目中,用户通过直接在终端输入数字(即题号)来选择要做的题目。这种设计利用了函数的功能,每个题目的解答对应一个函数,用户的选择触发相应的函数执行。
例如,在Python中,你可以创建一个字典,其中键是题号,值是对应的函数:
```python
def question1():
# 题目1的内容...
def question2():
# 题目2的内容...
questions = {
1: question1,
2: question2, # 添加更多的题目...
}
def select_question():
user_input = int(input("请输入题号(1-{}):".format(len(questions))))
if user_input in questions:
return questions[user_input]()
else:
print("无效的输入,已退出.")
select_question()
```
相关问题
编写函数输出小于等于n的水仙花数通过课程进入题号:118 6.4编写医设有一个3位数,它的百位数、十位数、个位数的立方和正好等于这个3位数,如153 = 1 + 125 + 27。编写函数,返回小于等于传入参数n且满足该条件的三位数(称为水仙花数)的个数。(指定函数原型:int find ( int n ) )输入输出格式要求:编写函数int find ( int n )返回值要求:如果传入参数n不是三位数或者在该范围内没有找到,则find返回0,否则返回找到的水仙花数的个数。注意:不要在find函数中打印(如调用printf或puts等函数》任何数据,否则视为错误。例如:n为:400find函数应返回3上传作业返回使用C语言
好的,下面是实现小于等于n的水仙花数的函数代码:
```c
int find(int n){
if(n<100 || n>999){ // 判断传入参数n是否为三位数
return 0;
}
int cnt = 0; // 记录水仙花数的个数
for(int i=100; i<=n; i++){ // 枚举三位数
int a = i / 100; // 百位数
int b = (i / 10) % 10; // 十位数
int c = i % 10; // 个位数
if(i == a*a*a + b*b*b + c*c*c){ // 判断是否为水仙花数
cnt++;
}
}
return cnt;
}
```
注意,上述代码中并没有使用任何打印函数,只是根据题目要求返回了小于等于n的水仙花数的个数。
题号 题目(4*7.5=30分) 请把调试好的代码复制到答案列,注意:1.代码比单元格长没有关系。2.每个变量要用一条输入语句。且必须按题目要求顺序输入。3.只能使用input()函数输入 答案 1 "已知收发两端的距离是x千米,信号在媒体上传播的速率是y米/s,如果数据长度是z 字节,网络带宽是s Mb/s,请计算出 (1)发送时延是多少秒; (2)传播时延是多少秒; 距离x: 904852507 媒体传播速度y: 4070147 数据长度z: 10
题目中提到要求按照顺序输入每个变量,下面是参考代码:
```
# 读取输入变量
x = float(input("请输入收发两端的距离(单位:千米):"))
y = float(input("请输入信号在媒体上传播的速率(单位:米/秒):"))
z = float(input("请输入数据长度(单位:字节):"))
s = float(input("请输入网络带宽(单位:Mb/秒):"))
# 计算发送时延
send_delay = z / (s * 125000) # 1 Mb/s = 125000 字节/秒
print("发送时延为:%.2f 秒" % send_delay)
# 计算传播时延
prop_delay = x * 1000 / y # 千米转换为米
print("传播时延为:%.2f 秒" % prop_delay)
```
运行上述代码,按照提示输入各个变量的值,即可计算出发送时延和传播时延。注意,代码中将输入的带宽单位转换为字节/秒,因为数据长度单位为字节。
阅读全文
相关推荐
















