По разделам

 

Решение задачи 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 Просмотров: 6575
Proc 2 Просмотров: 5074
Proc 3 Просмотров: 5181
Proc 4 Просмотров: 4338
Proc 5 Просмотров: 3408
Proc 6 Просмотров: 4660
Proc 7 Просмотров: 3927
Proc 8 Просмотров: 3027
Proc 9 Просмотров: 3158
Proc 10 Просмотров: 2918
Proc 11 Просмотров: 3020
Proc 12 Просмотров: 2217
Proc 13 Просмотров: 2455
Proc 14 Просмотров: 2698
Proc 15 Просмотров: 1899
Proc 16 Просмотров: 2885
Proc 17 Просмотров: 2531
Proc 18 Просмотров: 2446
Proc 19 Просмотров: 2365
Proc 20 Просмотров: 3227
Proc 21 Просмотров: 3335
Proc 22 Просмотров: 2453
Proc 23 Просмотров: 2439
Proc 24 Просмотров: 1694
Proc 25 Просмотров: 2078
Proc 26 Просмотров: 1843
Proc 27 Просмотров: 2055
Proc 28 Просмотров: 1764
Proc 29 Просмотров: 1949
Proc 30 Просмотров: 2485
Proc 31 Просмотров: 1701
Proc 32 Просмотров: 1262
Proc 33 Просмотров: 1303
Proc 34 Просмотров: 1722
Proc 35 Просмотров: 1196
Proc 36 Просмотров: 1660
Proc 37 Просмотров: 1513
Proc 38 Просмотров: 1225
Proc 39 Просмотров: 1124
Proc 40 Просмотров: 1868
Proc 41 Просмотров: 1928
Proc 42 Просмотров: 1176
Proc 43 Просмотров: 1147
Proc 44 Просмотров: 1033
Proc 45 Просмотров: 1166
Proc 46 Просмотров: 1067
Proc 47 Просмотров: 1103
Proc 48 Просмотров: 854
Proc 49 Просмотров: 1072
Proc 50 Просмотров: 2114
Proc 51 Просмотров: 1710
Proc 52 Просмотров: 1734
Proc 53 Просмотров: 1207
Proc 54 Просмотров: 1289
Proc 55 Просмотров: 1187
Proc 56 Просмотров: 1770
Proc 57 Просмотров: 1536
Proc 58 Просмотров: 1216
Proc 59 Просмотров: 1132
Proc 60 Просмотров: 1432

Комментарии

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



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