def summ(m): s = 0 while m > 0: s += m % 10 m = m // 10 return s
from random import random
N = 10 a = [0] * N for i in range(N): a[i] = int(random() * 40) + 10 print('%4d' % a[i], end='') print()
for i in range(N - 1): # количество переборов 9 for j in range(N - i - 1): # при первом переборе i=0 if summ(a[j]) > summ(a[j + 1]): a[j], a[j + 1] = a[j + 1], a[j]
for i in range(N): print('%4d' % a[i], end='') print()
for i in range(N): print('%4d' % summ(a[i]), end='') print()
pascal
const N = 10; var arr: array[1..N] of integer; i, j: byte; a: integer; function sum (n: integer): byte; begin sum := 0; while n > 0 do begin sum := sum + n mod 10; n := n div 10; end; end; begin randomize; for i := 1 to N do begin arr[i] := random(20)+10; write (arr[i]:3); end; writeln;
for i := 1 to N-1 do // количество проходов по массиву for j := 1 to N-i do // количество сравнений в каждом проходе if sum(arr[j]) > sum(arr[j+1]) then begin a := arr[j]; arr[j] := arr[j+1]; arr[j+1] := a; end;
for i := 1 to N do write(arr[i]:3); writeln; for i := 1 to N do write(sum(arr[i]):3); writeln; end.
def summ(m):
s = 0
while m > 0:
s += m % 10
m = m // 10
return s
from random import random
N = 10
a = [0] * N
for i in range(N):
a[i] = int(random() * 40) + 10
print('%4d' % a[i], end='')
print()
for i in range(N - 1): # количество переборов 9
for j in range(N - i - 1): # при первом переборе i=0
if summ(a[j]) > summ(a[j + 1]):
a[j], a[j + 1] = a[j + 1], a[j]
for i in range(N):
print('%4d' % a[i], end='')
print()
for i in range(N):
print('%4d' % summ(a[i]), end='')
print()
pascal
const
N = 10;
var
arr: array[1..N] of integer;
i, j: byte;
a: integer;
function sum (n: integer): byte;
begin
sum := 0;
while n > 0 do begin
sum := sum + n mod 10;
n := n div 10;
end;
end;
begin
randomize;
for i := 1 to N do begin
arr[i] := random(20)+10;
write (arr[i]:3);
end;
writeln;
for i := 1 to N-1 do // количество проходов по массиву
for j := 1 to N-i do // количество сравнений в каждом проходе
if sum(arr[j]) > sum(arr[j+1]) then begin
a := arr[j];
arr[j] := arr[j+1];
arr[j+1] := a;
end;
for i := 1 to N do
write(arr[i]:3);
writeln;
for i := 1 to N do
write(sum(arr[i]):3);
writeln;
end.