发现`A`被提升到'B`,那么你需要将A加到它自己的B次

时间:2016-10-21 05:01:45

标签: python python-2.7

'我在给出(9,-8)时没有得到正确的输出。如何在不使用pow函数和**运算符的情况下解决这个问题

def power(x,y):
    if x == 0:
        return x
    if y == -1:
        return 1
    else:
        return -1   
    if y == 0:
        return 1
    a = power(x,y//2)
    if y%2 == 0:
        return a*a
    return a*a*x
print power(9,-8)

2 个答案:

答案 0 :(得分:2)

def power(x,y):
   if y == 0:
    return 1
   a = power(x, y/2);
   if y%2 == 0:
    return a*a;
   else:
    return x*a*a;

x = 9
y = -8
if y < 0:
    print 1.0/(power(x,abs(y)))
else:
    print power(x,abs(y))

保持这样简单..

x^y为正时,我的pow()函数计算y。如果我们想要查找x^y为否定y,则只需打印1/pow(x,y)

注意:@vivin:现在答案是否正确?

希望它有帮助!!!!

答案 1 :(得分:0)

由于

,如果y!= -1(除非x == 0),您的代码将始终返回-1
doctrine:
    dbal:
        default_connection: customer
        connections:
            default:
                driver:   pdo_mysql
                host:     "%default_database_host%"
                port:     "%default_database_port%"
                dbname:   "%default_database_name%"
                user:     "%default_database_user%"
                password: "%default_database_password%"
                charset:  UTF8
            customer:
                driver:   pdo_mysql
                host:     "%customer_database_host%"
                port:     "%customer_database_port%"
                dbname:   "%customer_database_name%"
                user:     "%customer_database_user%"
                password: "%customer_database_password%"
                charset:  UTF8
    orm:
        default_entity_manager: customer
        entity_managers:
            default:
                connection: default
                mappings:
                    AppBundle: ~
            customer:
                connection: customer
                mappings:
                    CustomerBundle: ~

保持简单;)

    push    ebp
    mov     ebp, esp
    mov     ecx, [ebp+8]

L1: jcxz    done
    dec     ecx
    push    ecx
    .... Do printing here
    pop     ecx
    jmp     L1

done:
    leave
    ret     4