每日codingame小游戏练习[2021.3.28](python3入门学习之分数fraction运算)

题目描述

无文字描述,看实例写代码

Test 1

InputOutput
4
6
2/3

Test 2

InputOutput
5
5
1/1

Test 3

InputOutput
9
12
234

Test 4

InputOutput
3
5
3/5

Test 5

InputOutput
14750533
22725185
2107219/3246455

题目分析

所见即所得,直接上代码。

解题代码

我写的代码:

import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

x = int(input())
y = int(input())

def hcf(x,y):
    if x>y:
            smaller=y
    else:
            smaller=x
    for i in range(1,smaller+1):
        if ((x%i==0) and (y%i==0)):
            h=i
    return h
# Write an answer using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)
h1=hcf(x,y)
x1=x//h1
y1=y//h1
print(("%d/%d")%(x1,y1))

大佬的代码(一个来自保加利亚的老哥):

from fractions import Fraction
numerator = int(input())
denominator = int(input())

# Write an answer using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)
f=Fraction(numerator, denominator)
print(("%d/%d")%(f.numerator,f.denominator))

总结

1.Python range() 函数用法
2.Python3 print() 函数用法总结
3.python中进行分数fraction()运算

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值