1.刪數
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32M,其他語言64M
有一個數組a[N]順序存放0~N-1,要求每隔兩個數刪掉一個數,到末尾時循環至開頭繼續進行,求最後一個被刪掉的數的原始下標位置。以8個數(N=7)爲例:{0,1,2,3,4,5,6,7},0->1->2(刪除)->3->4->5(刪除)->6->7->0(刪除),如此循環直到最後一個數被刪除。
輸入描述:
每組數據爲一行一個整數n(小於等於1000),爲數組成員數,如果大於1000,則對a[999]進行計算。
輸出描述:
一行輸出最後一個被刪掉的數的原始下標位置。
輸入例子1:
8
輸出例子1:
6
代碼實現:
import sys
for line in sys.stdin:
n = int(line)
if n > 1000:
n = 1000
a = [i for i in range(n)]
i, deletes, k = -1, 0, 0
while deletes < n:
k = 0
while k <= 2:
i += 1
i = i % n
if a[i] == -1:
continue
k+=1
a[i] = -1
deletes += 1
print(i)
[編程題]字符集合
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32M,其他語言64M
輸入一個字符串,求出該字符串包含的字符集合
輸入描述:
每組數據輸入一個字符串,字符串最大長度爲100,且只包含字母,不可能爲空串,區分大小寫。
輸出描述:
每組數據一行,按字符串原有的字符順序,輸出字符集合,即重複出現並靠後的字母不輸出。
輸入例子1:
abcqweracb
輸出例子1:
abcqwer
代碼實現:
import sys
for line in sys.stdin:
d = {}
s = ''
for c in line:
if c not in d and c != '\n':
s += c
d[c] = 1
print(s)
[編程題]數獨
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32M,其他語言64M
數獨是一個我們都非常熟悉的經典遊戲,運用計算機我們可以很快地解開數獨難題,現在有一些簡單的數獨題目,請編寫一個程序求解。
輸入描述:
輸入9行,每行爲空格隔開的9個數字,爲0的地方就是需要填充的。
輸出描述:
輸出九行,每行九個空格隔開的數字,爲解出的答案。
代碼實現:
import sys
import copy
def check_results(results, i, j):
row = results[i]
for m in range(0, j):
if row[m] == row[j]:
return False
for n in range(0, i):
if results[n][j] == results[i][j]:
return False
k, l = i//3, j//3
for m in range(k*3, i+1):
for n in range(l*3, l*3+3):
if m == i and n == j:
return True
if results[m][n] == results[i][j]:
return False
return True
def prt_results(results):
for row in results:
s = ''
for col in row:
s += ' ' + str(col)
print(s)
def get_sd(a, i=0, j=0, results=None):
if results is None:
results = copy.deepcopy(a)
if a[i][j] != 0:
if j < 8:
get_sd(a, i, j+1, results)
elif i < 8:
get_sd(a, i+1, 0, results)
else:
prt_results(results)
sys.exit(0)
else:
for s in range(1, 10):
results[i][j] = s
if not check_results(results, i, j):
continue
else:
if i == 8 and j == 8:
prt_results(results)
sys.exit(0)
elif j < 8:
get_sd(a, i, j+1, results)
elif i < 8:
get_sd(a, i+1, 0, results)
results[i][j] = 0
return results
if __name__ == "__main__":
a=[[0 for _ in range(9)] for _ in range(9)]
get_sd(a, 0, 0