假设有一组非负整数,对数据元素重新排列,使其构成的整数最大,输出该整数。如输入11,20,3,7.输出732011
思路:
1.用冒泡排序法对数据进行排序,将最小的元素放在最后
2.最后对a数组各元素相加,输出字符串
3.程序可以正确运行
c
=
input
(
"请输入一组数字,用空格隔开:"
)
a
=
c
.
split
(
)
def
compare
(
a
,
b
)
:
s1
=
str
(
a
)
+
str
(
b
)
s2
=
str
(
b
)
+
str
(
a
)
if
s1
<
s2
:
return
True
else
:
return
False
for
j
in
range
(
len
(
a
)
-
1
)
:
for
i
in
range
(
len
(
a
)
-
1
-
j
)
:
if
compare
(
a
[
i
]
,
a
[
i
+
1
]
)
:
temp
=
a
[
i
]
a
[
i
]
=
a
[
i
+
1
]
a
[
i
+
1
]
=
temp
num
=
''
for
i
in
range
(
len
(
a
)
)
:
num
+=
a
[
i
]
print
(
num
)