原题:

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. 029 01 Android 零基础入门 01 Java基础语法 03 Java运算符 09 逻辑“非”运算符

    029 01 Android 零基础入门 01 Java基础语法 03 Java运算符 09 逻辑"非"运算符 本文知识点:Java中的逻辑"非"运算符 逻辑& ...

  2. Jmeter JDBC Request 使用详解

    本篇博文讲解以MySQL为例,搞懂JDBC Request中MySQL的使用方法,换成其它数据库, 如Oracle.PSQL也会很容易上手. 一.基本配置 1.首先我们先了解一下,不同数据库的驱动类和 ...

  3. Black-Lives-Matter-Resources

    下载 Black-Lives-Matter-ResourcesBlack-Lives-Matter-Resources 关于最近在美国发生的事件的资源列表 链接 描述 由于(可选) 插入链接 在这里插 ...

  4. IDEA推送docker镜像到私服/利用dockerfile-maven-plugin插件在springboot中上传镜像到远程的docker服务器、远程仓库

    利用dockerfile-maven-plugin插件在springboot中上传镜像到远程仓库      这篇文章讲解在开发工具中把打包好的jar编译成docker镜像,上传到远程的docker服务 ...

  5. docker-创建容器常见选项

    1. docker run创建容器常见选项 1.1 创建容器 选项 描述 -i,-interactive 交互式 -t,-tty 分配一个伪终端 -d,-detach 运行容器到后台 -e,-env ...

  6. WebFlux快速上手

    一.新建项目 示例使用IDEA快速创建基于SpringBoot的工程. springboot 2.3.1 java 8 WebFlux 必须选用Reactive的库 POM 依赖 <depend ...

  7. 利用Docker搭建开发环境

    一. 前言 随着平台的不断壮大,项目的研发对于开发人员而言,对于外部各类环境的依赖逐渐增加,特别是针对基础服务的依赖.这些现象导致开 发人员常常是为了简单从而直接使用公有的基础组件进行协同开发,在出现 ...

  8. java 保留小数点后指定位数四种方法

    1 package com.itheima_01; 2 3 import java.math.BigDecimal; 4 import java.text.DecimalFormat; 5 impor ...

  9. 类型“DbContext”在未引用的程序集中定义。必须添加对程序及“EntityFramework,Version=6.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”的引用。using语句中使用的类型必须可隐式转换为”System.IDisposable

    其他层引用Model层的ef模型时会发生这个错误 解决方法: 在你要使用EF模型的层下点击添加引用 然后点击浏览   找到Model层文件下的bin>debug文件   引用这两个dll文件 如 ...

  10. linux mkfifo命令基本用法

    首先了解linux命令执行顺序 通常情况下,终端只能执行一条命令,然后按下回车,那么执行多条命令呢 顺序执行多条命令,可以用分号; cmd1;cmd2;cmd3 条件执行多条命令,使用&&am ...