python笔记之1-简单读入+循环、判断+数组+函数调用+题目Resistors in Parallel(18焦作)

。。。。本来博主想一心一意搞算法和C++的,但今天的大数用C++写真的。。。心态爆炸,然后学了一波python。。。多路周折终于A了这题

python的语言在有了c语言的基础上其实还挺好学的。。。虽然博主学了半个下午才把线性素数筛和gcd写出来了QAQ,但博主觉得各位应该更快。。。。

python不需要头文件。。。直接写,变量也不用定义但它的读入有点。。。一行只读一个东西

n=int(input())

这个有点类似于scanf ("%d",&n),但,python的这个n好像没有大小范围限制。。。前面的int是将输入的东西强转成int型(这个int是理论无限大的)python的读入是一个字符串,所以需要这样写,以下是两种写法:

a=input()
b=input()
c=a+b
c=int(a)+int(b)

输入:

123

456

前面的c的值是123456,后面的值是579

print的话是输出,print(n)就是将n输出,只不过这个是换行的,如果想不换行的话:print(n,end=" ")就是将结束符换成空格

接下来就是循环了:

for i in range(a,b):
    ......

很好理解,就不多说了,有一点需要注意的是它不会到b,他是到b-1的。。

while a>=b or xx:
    ......

接下来就是判断了

if aa:
    ......
elif ax>3:
    ......
else :
    ...

。。。这个有点意思,else if 变成了elif。。

下面就是数组了,这个有点鬼畜了,博主现在只会一维数组QAQ。

a=[1,2,3]

这个数组什么都可以存。。。。一样从0开始,现在它只有3的大小,下标到2,我们可以先初始化它将它的空间扩大:

for i in range(3,100000):
    a.append(..)

a.append()就是将括号里的东西插入a里面,有点类似于C++里面的vector。

下面就是除法了。。。这个除法有点骚,int/int变成了浮点型,我今天就是因为这个一直WA3,会有浮点误差,如果确定是整除的话使用//就好了

下面写一个素数筛:

vis=[]
mac=int(1e5+10)
for i in range (0,mac):
    vis.append(0)
m=int(100010 ** 0.5)
for i in range(2,m):
    if (vis[i]==0):
        j=i*i
        while j<mac-5:
            vis[j]=1;j+=i;
prim=[]
for i in range(2,mac-5):
    if vis[i]==0:
        prim.append(i)  

接下来len(prim)的大小就是prim的大小,如果C语言懂了素数筛了的话应该很容易懂了

接下来就是一个函数的定义和运用,其实和C语言也是一样的:

#def hhh(a):
    ......

定义一个hhh的函数,里面传进来几个变量。。。然后就开始在这个函数里面xjb搞就好了

下面给出gcd函数:

def gcd(a,b):
    if b:
        return gcd(b,a%b)
    return a

下面就给出今天大数自闭的一题吧,也是博主的第一个python比较成熟一些的程序吧:

Resistors in Parallel

Time limit

2000 ms

Memory limit

1048576 kB

In this physics problem, what we are concerned about are only resistors. If you are poor at physics, do not worry, since solving this problem does not require you to have advanced abilities in physics.

Resistors are said to be connected together in parallel when both of their terminals are respectively connected to each terminal of the other resistors.

We have the following parallel resistor equation for k resistors with resistances R1, R2, ..., Rk in parallel and their combined resistance R:

Now you have n resistors, the i-th of which has a resistance of ri ohms with the equation

You also have n selections, the i-th of which is a set of resistors Si such that

Please find a selection in which the resistors form a parallel resistor with the minimum resistance and output the reduced fraction  of its resistance.

Input

The input contains several test cases, and the first line contains a positive integer T indicating the number of test cases which is up to 100.

For each test case, the only one line contains an integer n, where 1 ≤ n ≤ 10100.

Output

For each test case, output a line containing a reduced fraction of the form p/qindicating the minimum possible resistance, where p and q should be positive numbers that are coprime.

Example

Input

3
10
100
1000

Output

1/2
5/12
35/96

题目大意是给你n个集合,每个集合的电阻的大小是它的因子,当他的大小可以被一个平方数整除时该电阻为无穷大,比如4,8的电阻就是无穷大的。让你求这些集合中最小的并联电阻,比如2的电阻是1/(1/1+1/2)=2/3

这题就是打个表找规律就行了。最后会发现,分子就是素数之乘积,分母就是素数+1的乘积,然后用gcd约分一下就好了

以下是AC代码:

vis=[]
mac=int(1e5+10)
for i in range (0,mac):
    vis.append(0)
m=int(100010 ** 0.5)
for i in range(2,m):
    if (vis[i]==0):
        j=i*i
        while j<mac-5:
            vis[j]=1;j+=i;
prim=[]
for i in range(2,mac-5):
    if vis[i]==0:
        prim.append(i)  

def gcd(a,b):
    if b:
        return gcd(b,a%b)
    return a

t=int(input())
for o in range(1,t+1):
    n=int(input())
    j=0;fenmu=1;fenzi=1
    while fenzi*prim[j]<=n:
        fenzi*=prim[j]
        fenmu*=(prim[j]+1)
        j+=1
    gdx=gcd(fenzi,fenmu)
    fenzi=fenzi//gdx
    fenmu=fenmu//gdx
    print(fenzi,end="")
    print("/",end="")
    print(fenmu)

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值