По разделам

 

Решение задачи 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 Просмотров: 6816
Proc 2 Просмотров: 5265
Proc 3 Просмотров: 5360
Proc 4 Просмотров: 4521
Proc 5 Просмотров: 3545
Proc 6 Просмотров: 4823
Proc 7 Просмотров: 4065
Proc 8 Просмотров: 3177
Proc 9 Просмотров: 3295
Proc 10 Просмотров: 3038
Proc 11 Просмотров: 3137
Proc 12 Просмотров: 2324
Proc 13 Просмотров: 2573
Proc 14 Просмотров: 2822
Proc 15 Просмотров: 2028
Proc 16 Просмотров: 3081
Proc 17 Просмотров: 2680
Proc 18 Просмотров: 2556
Proc 19 Просмотров: 2575
Proc 20 Просмотров: 3373
Proc 21 Просмотров: 3489
Proc 22 Просмотров: 2611
Proc 23 Просмотров: 2592
Proc 24 Просмотров: 1836
Proc 25 Просмотров: 2210
Proc 26 Просмотров: 1979
Proc 27 Просмотров: 2171
Proc 28 Просмотров: 1890
Proc 29 Просмотров: 2082
Proc 30 Просмотров: 2605
Proc 31 Просмотров: 1819
Proc 32 Просмотров: 1375
Proc 33 Просмотров: 1399
Proc 34 Просмотров: 1834
Proc 35 Просмотров: 1306
Proc 36 Просмотров: 1790
Proc 37 Просмотров: 1642
Proc 38 Просмотров: 1335
Proc 39 Просмотров: 1229
Proc 40 Просмотров: 2003
Proc 41 Просмотров: 2023
Proc 42 Просмотров: 1266
Proc 43 Просмотров: 1236
Proc 44 Просмотров: 1147
Proc 45 Просмотров: 1278
Proc 46 Просмотров: 1159
Proc 47 Просмотров: 1214
Proc 48 Просмотров: 946
Proc 49 Просмотров: 1200
Proc 50 Просмотров: 2214
Proc 51 Просмотров: 1815
Proc 52 Просмотров: 1829
Proc 53 Просмотров: 1357
Proc 54 Просмотров: 1392
Proc 55 Просмотров: 1301
Proc 56 Просмотров: 1898
Proc 57 Просмотров: 1649
Proc 58 Просмотров: 1314
Proc 59 Просмотров: 1264
Proc 60 Просмотров: 1535

Комментарии

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



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