python 編碼問題
Question:
題:
A power function is that positive number that can be expressed as x^x i.e x raises to the power of x, where x is any positive number. You will be given an integer array A and you need to print if the elements of array A are Power Number or not.
一種功率函數是可以表示為x ^ X即,X加注到x的功率即正數,其中x是任意正數。 您將得到一個整數數組A,并且如果數組A的元素是否為Power Number,則需要打印。
Input:
輸入:
The first line contains N, a positive integer. The next line contains N spaces – separated integers.
第一行包含N ,一個正整數。 下一行包含N個空格-分隔的整數。
3
1 3 4
Output:
輸出:
For every integer print "Yes" if it’s a power number else print "No". these outputs must be separated by spaces.
對于每個整數,如果是冪數,則打印“ Yes” ,否則打印“ No” 。 這些輸出必須用空格分隔。
Yes No Yes
Constraints:
限制條件:
1 <= N <= 100
1 <= A[i] <= 10^16
Explanation/Solution:
說明/解決方案:
In this question, we have to check that the given numbers in an array are power numbers or not. We will find it with the help of a power function in python. So we can do this by comparing each element with the power number so first we take input from the user and create the array of size N. then make another array that stores the power number from 1 to 14. The key focus of this question is on constraints. In this question, the constraints are given till 10^16 numbers.
在這個問題中,我們必須檢查數組中給定的數字是否為冪數。 我們將在python中的冪函數的幫助下找到它。 因此,我們可以通過將每個元素與冪數進行比較來做到這一點,因此首先我們從用戶那里獲取輸入并創建大小為N的數組。 然后制作另一個存儲從1到14的冪數的數組。這個問題的重點是約束。 在這個問題中,給出的約束直到10 ^ 16個數字。
To solve this question, we are using Python3.
為了解決這個問題,我們使用Python3。
Code:
碼:
# input N
N = int(input())
# input N array elements
Arr = list(map(int, input().split()))
# create another blank/empty array
Brr = []
for i in range(15):
# Append the power numbers in blank array
Brr.append(pow(i, i))
for i in range(N):
# compare the give array with
# power number array one by one
if(Arr[i] in Brr):
# if it matches print Yes other wise NO
print('Yes', end=' ')
else:
print('No', end=' ')
Output
輸出量
RUN 1:
5
1 4 3 16 256
Yes Yes No No Yes
RUN 2:
5
2 4 8 16 32
No Yes No No No
翻譯自: https://www.includehelp.com/python/power-challenge-competitive-coding-questions.aspx
python 編碼問題