По разделам

 

Решение задачи Series 39


Дано целое число K, а также K наборов ненулевых целых чисел.
Каждый набор содержит не менее трех элементов, признаком его завершения является число 0. Найти количество пилообразных наборов (определение пилообразного набора дано в задании Series23).

Код (C/C++)

#include <stdio.h>
int main(void)
{
    int k;
    printf("K:");
    scanf("%i", &k);
 
    int ki,a=1,a1,a2,count=0;
    for (ki=1; ki<=k; ++ki){        printf(">");
       scanf("%i", &a1);
       printf(">");
       scanf("%i", &a);
       int check=1;
       while(a!=0){
            a2=a1;
            a1=a;
            printf(">");
            scanf("%i", &a);
            if (!(((a2<a1) && (a1>a)) || ((a2>a1) && (a1<a))) && (a!=0) ) check=0;
        }
        if (check)  count+=1;
    }
    printf("%i\n",count);
    return 0;
}

Код (Python)

import random

K = random.randrange(1,9)
print("K = ",K)

count_saw = 0
for i in range(0,K):
    x_prev = random.randrange(1,10)
    x_curr = random.randrange(x_prev+1,x_prev+11)
    print(x_prev,end="; ")
    print(x_curr,end="; ")
    k = -1
    saw = 0
    i = 2
    while True:
        if k == 1:
            y = list(range(x_curr-1,x_curr+11))
            y.append(0)
            #x_next = random.randrange(x_curr-1,x_curr+11)
            x_next = random.choice(y)
        else:
            #x_next = random.randrange(x_curr-10,x_curr+1)
            y = list(range(x_curr-10,x_curr+1))
            y.append(0)
            x_next = random.choice(y)
        k *= -1
        print(x_next,end="; ")
        if x_next == 0:
            break

        if saw == 0:
            if not ((x_prev < x_curr and x_curr > x_next) \
               or (x_prev > x_curr and x_curr < x_next)):
                saw = i
        x_prev = x_curr
        x_curr = x_next
        i += 1
        
    print()
    if saw == 0:
        print("saw: ", saw)
        count_saw += 1
    else:
        print("not saw: ", saw)
    print()
print("Number of saws: ", count_saw)
									

Код (Pascal)

program Series39;
var
  K,Ki,A,A1,A2,Count:Integer;
  Check:Boolean;
begin
 Write('K: ');
 Readln(K);
 for Ki:=1 to K do
  begin
   Write('A: ');
   Readln(A1);
   Write('A: ');
   Readln(A);
   Check:=True;
   repeat
    A2:=A1;
    A1:=A;
    Write('A: ');
    Readln(A);
    if  not (((A2<A1) and (A1>A)) or ((A2>A1) and (A1<A))) and (A<>0) then Check:=False;
   until A=0;
    Writeln('---');
   If Check then Count:=Count+1;
  end;
  Writeln(Count);
end.
									




Series. Абрамян
Series 1 Просмотров: 4448
Series 2 Просмотров: 2276
Series 3 Просмотров: 2039
Series 4 Просмотров: 2223
Series 5 Просмотров: 2096
Series 6 Просмотров: 2679
Series 7 Просмотров: 2938
Series 8 Просмотров: 3229
Series 9 Просмотров: 1915
Series 10 Просмотров: 2368
Series 11 Просмотров: 1783
Series 12 Просмотров: 2138
Series 13 Просмотров: 1560
Series 14 Просмотров: 1453
Series 15 Просмотров: 1743
Series 16 Просмотров: 2148
Series 17 Просмотров: 1962
Series 18 Просмотров: 2787
Series 19 Просмотров: 2494
Series 20 Просмотров: 1545
Series 21 Просмотров: 1713
Series 22 Просмотров: 1657
Series 23 Просмотров: 2058
Series 24 Просмотров: 1892
Series 25 Просмотров: 1459
Series 26 Просмотров: 1298
Series 27 Просмотров: 1227
Series 28 Просмотров: 1171
Series 29 Просмотров: 1195
Series 30 Просмотров: 1233
Series 31 Просмотров: 1189
Series 32 Просмотров: 1707
Series 33 Просмотров: 981
Series 34 Просмотров: 1030
Series 35 Просмотров: 1408
Series 36 Просмотров: 1520
Series 37 Просмотров: 951
Series 38 Просмотров: 1272
Series 39 Просмотров: 901
Series 40 Просмотров: 1249

Комментарии

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



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