原题:

I always thought that my old friend John was rather richer than he looked, but I never knew exactly how much money he actually had. One day (as I was plying him with questions) he said:

  • "Imagine I have between m and n Zloty..." (or did he say Quetzal? I can't remember!)
  • "If I were to buy 9 cars costing c each, I'd only have 1 Zloty (or was it Meticals?) left."
  • "And if I were to buy 7 boats at b each, I'd only have 2 Ringglets (or was it Zloty?) left."

Could you tell me in each possible case:

  1. how much money f he could possibly have ?
  2. the cost c of a car?
  3. the cost b of a boat?

So, I will have a better idea about his fortune. Note that if m-n is big enough, you might have a lot of possible answers.

Each answer should be given as ["M: f", "B: b", "C: c"] and all the answers as [ ["M: f", "B: b", "C: c"], ... ]. "M" stands for money, "B" for boats, "C" for cars.

Note: m, n, f, b, c are positive integers, where 0 <= m <= n or m >= n >= 0m and n are inclusive.

Examples:

howmuch(1, 100)      => [["M: 37", "B: 5", "C: 4"], ["M: 100", "B: 14", "C: 11"]]
howmuch(1000, 1100) => [["M: 1045", "B: 149", "C: 116"]]
howmuch(10000, 9950) => [["M: 9991", "B: 1427", "C: 1110"]]
howmuch(0, 200) => [["M: 37", "B: 5", "C: 4"], ["M: 100", "B: 14", "C: 11"], ["M: 163", "B: 23", "C: 18"]]

Explanation of the results for howmuch(1, 100):

In the first answer his possible fortune is 37:

    • so he can buy 7 boats each worth 5: 37 - 7 * 5 = 2
    • or he can buy 9 cars worth 4 each: 37 - 9 * 4 = 1
  • The second possible answer is 100:
    • he can buy 7 boats each worth 14: 100 - 7 * 14 = 2
    • or he can buy 9 cars worth 11: 100 - 9 * 11 = 1

-------------------------------------------------------------------------------------------------------------------

题目大意为给出一个数的范围,在此范围中找出数字,满足公式(这个整数-7*某个整数)=2以及满足(这个整数-9*某个整数)=1,然后返回这个数以及符合条件的“某个整数”的值

解题办法:

看一下我的解题办法,重复代码好多,,,,

def howmuch(m, n):
# your code
L = []
if n >= m:
for i in range(m, n+1):
L1 = []
b = (i-2) % 7
c = (i-1) % 9
if b == 0 and c == 0:
L1 = ["M: "+str(i), "B: "+str(int((i-2)/7)), "C: "+str(int((i-1)/9))]
L.append(L1)
else:
for i in range(n, m+1):
L1 = []
b = (i-2) % 7
c = (i-1) % 9
if b == 0 and c == 0:
L1 = ["M: "+str(i), "B: "+str(int((i-2)/7)), "C: "+str(int((i-1)/9))]
L.append(L1) return L

再看看网友优秀的思路:

def howmuch(m, n):
return [['M: %d'%i, 'B: %d'%(i/7), 'C: %d'%(i/9)] for i in range(min(m,n), max(m,n)+1) if i%7 == 2 and i%9 == 1]

简洁到令人发指,,,羡慕啊!!!

知识点:

1、取两个数的最大值和最小值,可以使用min()和max()函数来取出最大值和最小值;也可以使用n, m = m, n if m>n:

2、i = 1; (["M: %s"])%i -------->这是一种错误的办法

【Kata Daily 190908】How Much?的更多相关文章

  1. 【Kata Daily 190929】Password Hashes(密码哈希)

    题目: When you sign up for an account somewhere, some websites do not actually store your password in ...

  2. 【Kata Daily 191012】Find numbers which are divisible by given number

    题目: Complete the function which takes two arguments and returns all numbers which are divisible by t ...

  3. 【Kata Daily 191010】Grasshopper - Summation(加总)

    题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...

  4. 【Kata Daily 190927】Counting sheep...(数绵羊)

    题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...

  5. 【Kata Daily 190924】Difference of Volumes of Cuboids(长方体的体积差)

    题目: In this simple exercise, you will create a program that will take two lists of integers, a and b ...

  6. 【Kata Daily 190923】Odder Than the Rest(找出奇数)

    题目: Create a method that takes an array/list as an input, and outputs the index at which the sole od ...

  7. 【Kata Daily 190920】Square(n) Sum(平方加总)

    题目: Complete the square sum function so that it squares each number passed into it and then sums the ...

  8. 【Kata Daily 190919】Sort Out The Men From Boys(排序)

    题目: Scenario Now that the competition gets tough it will Sort out the men from the boys . Men are th ...

  9. 【Kata Daily 190918】Spacify(插空)

    题目: Modify the spacify function so that it returns the given string with spaces insertedbetween each ...

随机推荐

  1. Win10安装MongoDB

    1. 下载安装包:mongodb-win32-x86_64-2012plus-4.2.7-signed.msi 2. 安装,注意选择安装目录 3. 新建配置文件mongo.conf: ​``` #数据 ...

  2. CentOS 7安装docker和常用指令

    1.安装 yum -y install docker 2.启动 systemctl start docker // 启动 docker -v //查看版本号 systemctl stop docker ...

  3. webRTc实现视频直播

    <!DOCTYPE html> <html> <head> <script type='text/javascript' src='https://cdn.s ...

  4. 搭建zabbix+grafana监控

    编写一件安装脚本 #!/bin/sh echo "\033[32;1m脚本作者:fage\033[0m" #sleep 10 zabbix_version=4.0.2 zabbix ...

  5. 洛谷 P3413 【萌数】

    敲完这篇题解,我就,我就,我就,嗯,好,就这样吧... 思路分析: 首先我们要知道一个回文串的性质--假如说一个[l-1,r+1]的串是回文的,那么[l,r]一定也是回文的. 所以我们只要记录前一个数 ...

  6. Centos7 Docker配置TLS认证的远程端口的证书生成教程(shell脚本一键生成)

    通过 TLS来进行远程访问 百度百科 - TLS.我们需要在远程 docker 服务器(运行 docker 守护进程的服务器)生成 CA 证书,服务器证书,服务器密钥,然后自签名,再颁发给需要连接远程 ...

  7. VSCODE 配置eslint规则和自动修复

    全局安装eslint 打开终端,运行npm install eslint -g全局安装ESLint. vscode安装插件 vscode 扩展设置 依次点击 文件 > 首选项 > 设置 { ...

  8. mysql CHAR and VARCHAR 比较

    写在前面 面试的时候突然有一位面试官问,说说CHAR和VARCHAR的区别,懵逼了,想自己平常使用的时候直接把VARCHAR拿来就用,真没注意到其中的不同. 反思,为什么没有注意到他们的不同 对于my ...

  9. antd pro 下的文件下载

    概要 示例 后端 前端 直接显示图片 提供下载链接, 点击后下载 文件导出, 前端没有显示下载链接的位置 概要 前端上传文件的例子很多, 但是下载相关的例子不多, 主要是因为下载本身比较简单. 但是这 ...

  10. 串口wifi

    串口wifi 串口WiFi ZLAN7146是一款wifi转串口的wifi串口服务器.该串口服务器可以方便地使得串口设备连接到WIFI无线网络,实现串口设备的无线化网络升级.RS232接口支持全双工. ...