По разделам

 

Решение задачи Proc 27


Описать функцию IsPowerN(K, N) логического типа, возвращающую
TRUE, если целый параметр K (> 0) является степенью числа N (> 1), и
FALSE в противном случае. Дано число N (> 1) и набор из 10 целых положительных чисел. С помощью функции IsPowerN найти количество
степеней числа N в данном наборе.

Код (C/C++)

#include <stdio.h>
int ispowern5(int k, int n){
    float temp=k;
    for (;temp>=n; temp/=n);
    return temp==1;
}
 
int main(void)
{
    int i, n, res=0;
    printf("N:");
    scanf("%i", &n);
    for (i=1; i<=10; ++i){
        int k;
        printf("K:");
        scanf("%i", &k);
        res+=ispowern5(k,n);
    }
    printf("Res: %i\n",res);
    return 0;
}

Код (Python)

import random
import math

def IsPowerN(K,N):
    x = int(round(math.log(K,N)))
    if K == N**x:
        return True
    return False

def IsPowerNa(K,N):
    while K > 1:
        K /= N
    if K == 1.0:
        return True
    return False

s = 0
s2 = 0
N = random.randrange(2,10)
print("N = ",N)
L = [N**random.randrange(1,10)+random.randrange(0,2) for i in range(0,10)]
for i in range(0,len(L)):
    x = L[i]
    #print(x,end="; ")
    s += int(IsPowerN(x,N))
    s2 += int(IsPowerNa(x,N))
    print(x,":",IsPowerN(x,N),":",IsPowerNa(x,N))

print("\nAmount of IsPowerN:",s)
print("\nAmount of IsPowerN:",s2)
									

Код (Pascal)

program Proc27;
 
Function IsPowerN(K,N:Integer):Boolean;
begin
  While (K>=N) do
   begin
    if ((k mod N)<>0) and (k<>1) then
    begin
      K:=0;
      IsPowerN:=False;
    end;
    K:=K div N;
   end;
  if K=1 then IsPowerN:=True
  else IsPowerN:=False;
end;
 
var
  i,N,K,Res:Integer;
 
begin
  Write('N :');
  Readln(N);
 
 for i:=1 to 10 do
  begin
   Write('K :');
   Readln(K);
   if IsPowerN(K,N) then Inc(Res);
  end;
   Writeln(Res);
end.
									




Proc. Абрамян
Proc 1 Просмотров: 6512
Proc 2 Просмотров: 5032
Proc 3 Просмотров: 5116
Proc 4 Просмотров: 4303
Proc 5 Просмотров: 3362
Proc 6 Просмотров: 4607
Proc 7 Просмотров: 3892
Proc 8 Просмотров: 2985
Proc 9 Просмотров: 3123
Proc 10 Просмотров: 2882
Proc 11 Просмотров: 2984
Proc 12 Просмотров: 2181
Proc 13 Просмотров: 2422
Proc 14 Просмотров: 2655
Proc 15 Просмотров: 1865
Proc 16 Просмотров: 2840
Proc 17 Просмотров: 2485
Proc 18 Просмотров: 2405
Proc 19 Просмотров: 2323
Proc 20 Просмотров: 3169
Proc 21 Просмотров: 3298
Proc 22 Просмотров: 2401
Proc 23 Просмотров: 2397
Proc 24 Просмотров: 1659
Proc 25 Просмотров: 2032
Proc 26 Просмотров: 1806
Proc 27 Просмотров: 2006
Proc 28 Просмотров: 1737
Proc 29 Просмотров: 1915
Proc 30 Просмотров: 2445
Proc 31 Просмотров: 1669
Proc 32 Просмотров: 1221
Proc 33 Просмотров: 1250
Proc 34 Просмотров: 1670
Proc 35 Просмотров: 1154
Proc 36 Просмотров: 1613
Proc 37 Просмотров: 1474
Proc 38 Просмотров: 1193
Proc 39 Просмотров: 1098
Proc 40 Просмотров: 1831
Proc 41 Просмотров: 1897
Proc 42 Просмотров: 1149
Proc 43 Просмотров: 1115
Proc 44 Просмотров: 998
Proc 45 Просмотров: 1129
Proc 46 Просмотров: 999
Proc 47 Просмотров: 1061
Proc 48 Просмотров: 811
Proc 49 Просмотров: 1046
Proc 50 Просмотров: 2078
Proc 51 Просмотров: 1682
Proc 52 Просмотров: 1687
Proc 53 Просмотров: 1173
Proc 54 Просмотров: 1260
Proc 55 Просмотров: 1144
Proc 56 Просмотров: 1738
Proc 57 Просмотров: 1488
Proc 58 Просмотров: 1172
Proc 59 Просмотров: 1083
Proc 60 Просмотров: 1364

Комментарии

Чтобы написать комментарии вам нужно войти в систему или зарегистрироваться



Заявка на расчет