Python 代码:
def initial(n):
a = []
for i in range(1,n+1):
a.append(i)
return a
def algorithm(a):
while(len(a)>3):
newarray = []
for i in range(0,len(a)):
if((i+1)%2!=0):
newarray.append(a[i])
a = newarray
if(len(a)>3):
newarray = []
for i in range(0,len(a)):
if((i+1)%3!=0):
newarray.append(a[i])
a = newarray
return a
if __name__ == '__main__':
n = input('Please enter the number of people: ')
array = initial(n)
newarray = algorithm(array)
print newarray
pascal语言程序:
label 999;
var
a,b:array[1..10000] of integer;
n:integer;
i,j,k:integer;
x,m:integer;
begin
readln(n);
for i:=1 to n do begin a[i]:=i; end;
x:=n;
repeat
if x>3 then begin
m:=x;
k:=0;
for i:=1 to m do begin
if i mod 2<>0 then begin inc(k); b[k]:=a[i]; end;
if i mod 2=0 then dec(x);
if (x=3)and(i
goto 999;
end;
end;
k:=x;
for i:=1 to k do a[i]:=b[i];
end;
//===============================
if x>3 then begin
m:=x;
k:=0;
for i:=1 to m do begin
if i mod 3<>0 then begin inc(k); b[k]:=a[i]; end;
if i mod 3=0 then dec(x);
if (x=3)and(i
goto 999;
end;
end;
k:=x;
for i:=1 to k do a[i]:=b[i];
end;
until x=3;
999:
writeln(b[1]:5,b[2]:5,b[3]:5);
end.
什么语言也不说你也是…