我被困在这个问题上几天了。可以找到整个实验室问题here.
对任何正整数n打印以下内容。使用输入语句允许用户输入n的值,然后打印正确大小的框。
a -> a -> a
不要担心处理多位数的间距。
E.g. n = 3
1 3 5 5 3 1
3 5 5 3
5 5
5 5
3 5 5 3
1 3 5 5 3 1
E.g. n = 5
1 3 5 7 9 9 7 5 3 1
3 5 7 9 9 7 5 3
5 7 9 9 7 5
7 9 9 7
9 9
9 9
7 9 9 7
5 7 9 9 7 5
3 5 7 9 9 7 5 3
1 3 5 7 9 9 7 5 3 1
我使用了未来函数的导入,因为我使用的是Python 2.7.3.1。另外,如果这似乎是一个" noob"我很抱歉。问题,但我是初学者,需要帮助。谢谢你的帮助!非常感谢。
答案 0 :(得分:0)
这是一个有趣的问题。这是我的解决方案,不是很优化,但应该直截了当地理解:
// 1. Find the minimum value in the list.
// 2. Swap the minimum value with the value in the first position.
// 3. move the insertion point for the minimum along one.
// 4. Repeat the steps above for the remainder of the list
public static void yoursort(String[]array) {
int currentIndex = 0;
while(currentIndex < array.length-1) {
currentIndex++;
int indexOfMinimum = currentIndex;
int candidateMinIndex = currentIndex + 1;
while(candidateMinIndex < array.length) {
candidateMinIndex++;
if(candidateMinIndex < indexOfMinimum) {
indexOfMinimum = candidateMinIndex;
}
}
swap(array, currentIndex, indexOfMinimum);
}
}
public static void mysort(String[]array) {
int currentIndex = 0;
while(currentIndex < array.length-1) { // dont need to do the last one
int indexOfMinimum = findIndexOfMinimum(array, currentIndex);
swap(array, currentIndex, indexOfMinimum);
currentIndex++;
}
}
protected static int findIndexOfMinimum(String[] array, int startIndex) {
int indexOfMinimum = startIndex; // start off asumming next is minimum
int candidateIndex = startIndex + 1; // can shift candidate along to second element.
while(candidateIndex < array.length) {
// compare candidate and minimum
if ( array[candidateIndex].compareTo(array[indexOfMinimum]) < 0 ) {
// ie array[candidateIndex] before/smaller array[indexOfMinimum]
indexOfMinimum = candidateIndex;
}
candidateIndex++;
}
return indexOfMinimum;
}
protected static void swap(String[] array, int toIndex, int fromIndex) {
if ( toIndex == fromIndex ) {
// fast return if swapping with self.
return;
}
String temp = array[toIndex];
array[toIndex] = array[fromIndex];
array[toIndex] = temp;
}
两位数字真的搞砸了格式,所以我用字母代替。
n = 5
import string
numberList = string.letters
# uncomment if you want numbers, i prefer letters
#numberList = [i*2-1 for i in range(1, n+1)]
# upper lower
# V V
for n_row in range(0, n) + list(reversed(range(0, n))):
# left number
for number in numberList[:(n-n_row)]:
print number,
# space
for number in range(n_row):
print ' ', ' ',
# right number
for number in reversed(numberList[:(n-n_row)]):
print number,
print
答案 1 :(得分:0)
请尝试以下代码:
inputValue = int(input('input interger:'))
base = ' '
for i in range(inputValue):
base = ' '+str(2*(inputValue-1-i)+1)+base+str(2*(inputValue-1-i)+1)+' '
length = len(base.strip())
graph = []
for j in range(inputValue):
line = ' '
for k in range(j):
line = ' '+line+' '
for i in range(inputValue-j):
line = ' '+str(2*(inputValue-1-i)+1)+line+str(2*(inputValue-1-i)+1)+' '
line = line.strip()
length_temp = int((length-len(line))/2)
for m in range(length_temp):
line = ' '+line+' '
graph.append(line)
for j in range(inputValue):
graph.append(graph[inputValue-1-j])
for line in graph:
print(line)
或者此代码,取决于您所需的输出:
inputValue = int(input('input interger:'))
base = ' '
for i in range(inputValue):
base = ' '+str(2*(inputValue-1-i)+1)+base+str(2*(inputValue-1-i)+1)+' '
length = len(base.strip())
graph = []
for j in range(inputValue):
line = ' '
for k in range(j):
line = ' '+line+' '
for i in range(inputValue-j):
line = ' '+str(2*(inputValue-1-i)+1)+line+str(2*(inputValue-1-i)+1)+' '
line = line.strip()
length_temp = int((length-len(line)))
for m in range(length_temp):
line = line[:int(len(line)/2)]+' '+line[int(len(line)/2)+1:]
graph.append(line)
for j in range(inputValue):
graph.append(graph[inputValue-1-j])
for line in graph:
print(line)