【Kata Daily 190908】How Much?
原题:
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
andn
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:
- how much money
f
he could possibly have ? - the cost
c
of a car? - 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 >= 0
. m
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
- so he can buy 7 boats each worth 5:
- 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
- he can buy 7 boats each worth 14:
-------------------------------------------------------------------------------------------------------------------
题目大意为给出一个数的范围,在此范围中找出数字,满足公式(这个整数-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?的更多相关文章
- 【Kata Daily 190929】Password Hashes(密码哈希)
题目: When you sign up for an account somewhere, some websites do not actually store your password in ...
- 【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 ...
- 【Kata Daily 191010】Grasshopper - Summation(加总)
题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...
- 【Kata Daily 190927】Counting sheep...(数绵羊)
题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...
- 【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 ...
- 【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 ...
- 【Kata Daily 190920】Square(n) Sum(平方加总)
题目: Complete the square sum function so that it squares each number passed into it and then sums the ...
- 【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 ...
- 【Kata Daily 190918】Spacify(插空)
题目: Modify the spacify function so that it returns the given string with spaces insertedbetween each ...
随机推荐
- 019 01 Android 零基础入门 01 Java基础语法 02 Java常量与变量 13 数据类型转换的代码示例
019 01 Android 零基础入门 01 Java基础语法 02 Java常量与变量 13 数据类型转换的代码示例 本文知识点:Java中的数据类型转换案例 学习视频有误,导致没法写文,文章内容 ...
- 利用rtklib处理GPS以及北斗数据详解
利用rtklib开源代码处理GPS以及北斗数据详解 在GNSS领域最基础的工作是这些GNSS系统的定位工作,对于绝大多数研究者,自己着手完成这些工作是一个"鸡肋":完全独立设计的话 ...
- 版本控制系统之git
一.简介 git是Linux内核项目发起者linus用C语言写的,主要用来做项目的版本控制追踪:git是无中心节点的分布式版本控制系统,也是目前很流行的版本控制系统:其安装简单,使用简单:相比传统的c ...
- 【MySQL Errors】Table 'xxx' is marked as crashed and should be repaired 的解决方案
现象描述 访问 Zabbix Web,出现如下错误提示: • Error in query [SELECT * FROM history_uint h WHERE h.itemid='25067' O ...
- 笔记本键盘按U键却变成了4
解答 笔记本键盘U盘变成了4,是因为你开启了小键盘功能.出现该问题,只要关闭小键盘功能即可,操作如下: 按住键盘下方的Fn,同时按住键盘顶部的F键中标有Numlk的键. 电脑屏幕出现解锁标志,小键盘功 ...
- docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled
故障描述: [root@docker01 ~]# docker run centos docker: Error response from daemon: Get https://registry- ...
- 用cmd下载tp5.0版本
1.首先进入phpstudy的www目录 composer create-project topthink/think=5.0.* pt5.0的名字 --prefer-dist $ composer ...
- SpringBoot+单机redis
spring boot-redis集成 看教程来的,看起来很简单,但是集成后发现启动失败? WARN 2556 --- [ restartedMain] ationConfigEmbeddedWebA ...
- turtle库元素语法分析
一.turtle原理理解: turtle库是Python中一个有趣的图形绘制函数库.原名(海龟),我们想象一只海龟,位于显示器上窗体的正中心,在画布上游走,它游走的轨迹就形成了绘制的图形. 对于小海龟 ...
- 多测师_高级讲师肖sir讲解html中 Button跳转连接方法归纳
第一种方法: 1.1<a href="http://www.baidu.com"> <input type="button" name=& ...