Skip to content
Snippets Groups Projects
Commit c444e5d2 authored by JangHyeongJun's avatar JangHyeongJun
Browse files

first

parent f3113cb4
Branches
No related tags found
No related merge requests found
#This is For Final Assignment for 창의소프트웨어입문(F031-1)
##magic_3x3.py
##magic_4x4.py
##magic_5x5.py
##magix_nxn.py
##multi_magix_4x4.py 로 구성되어있습니다.
###
magic_3x3.py에서는 사용자는 3x3 마방진을 만들 수 있습니다.
계산복잡도는 O(n^n)입니다.
실행하고나면 9개의 숫자를 입력하라는 출력문구가 나오며, 숫자를 입력하면 그 숫자들로 만들 수 있는 마방진이 one2nine.txt 파일에 저장되고, stderr에 총 계산시간과, 몇개의 답을 찾았는지 출력하고 종료합니다.
magic_4x4.py에서는 사용자는 4x4 마방진을 만들 수 있습니다.
계산복잡도는 O(n^n)입니다.
실행하고나면 16개의 숫자를 입력하라는 출력문구가 나오며, 숫자를 입력하면 그 숫자들로 만들 수 있는 마방진이 4x4.txt 파일에 저장되고, stderr에 총 계산시간과, 몇개의 답을 찾았는지 출력하고 종료합니다.
부가적으로 사분면의 계산을 위한 함수외에 실제 마방진(사분면에 대한 계산 없이 마방진을 만듦)을 만들긴 했다만, 사분면마방진에 포함되는 내용이라 따로 출력양식을 만들어놓지 않았습니다.
만약 1~16까지의 입력을 한다면 예상 출력답은 3456개이며, 계산시간은 제 컴퓨터 기준 66.91초 입니다.
magic_5x5.py
숫자가 늘어남에따라 기하급수적으로 증가하므로 기존의 방법으로는 너무 오랜시간이 들었습니다.
그리하여 만약 input이 연속된 숫자이고, 중복이 존재하지않는 경우를 가정하고 만들었던 5*5마방진입니다.
4x4마방진으로 바뀌기 전에 이미 작성하였는데 삭제하기 아까워서 함께 제출합니다.
만약 연속된 25개의 숫자로 만들 수 있는 마방진은 과연 몇개가 있을까 고민해보았습니다.
그 결과 하나를 만들어놓고 90도회전 4번, 원점대칭 1번으로 8개를 만드는것이 가장 쉬웠기에
일단은 8개를 만들어놓고 더 만들떄의 케이스를 생각중이었습니다.
magix_nxn.py
실패입니다.
multi_magix_4x4.py
파이썬은 단일 쓰레드를 사용합니다. 만약 멀티프로세싱을 이용하면 연산속도가 얼마나 빨라질 수 있을까 고민하며 만들어보려합니다. 아직 미완성입니다.
test_nxn.py
magic_3x3.py와 magic_4x4.py 의 결과 text파일을 이용합니다.
먼저 두개의 파이썬을 실행 완료한 후, 어떤것을 테스트할지 입력합니다.
1을 입력하면 magic_3x3.py의 결과를 테스트합니다.
2를 입력하면 magic_4x4.py의 결과를 테스트합니다.
\ No newline at end of file
......@@ -14,7 +14,7 @@ import sys , copy
startTime = time.time()
print("Typing Your Number split by space '중복숫자는 불가합니다': < ex : 1 2 3 4 5 6 7 8 9 10 11 ... 15>")
print("Typing Your Number split by space : < ex : 1 2 3 4 5 6 7 8 9 10 11 ... 15>")
str=input()
try:#스페이스로 구분해서 입력한경우
arr1=[int(a) for a in str.split()]
......
'''
201320527 교통시스템공학과 장형준
4x4 마방진 - magix_4x4.py (가로/세로/대각선과 1사분면, 2사분면, 3사분면, 4사분면의 합이 모두 같음, 입력된 숫자는 한번 만 사용,
만일 중복된 숫자가 있으면 중복된 횟수만큼 사용)
입력숫자를 input 명령으로 사용자가 자유롭게 입력할 수 있도록 한다.
예) 1 2 3 4 5 6 7 8 9 10 10 11 13 14 14 15
출력은 각각의 답을 한줄로 stdout으로 다음과 같이 모두 출력한다.
예) 1 14 14 4 11 7 6 9 8 10 10 5 13 2 3 15
stderr에 다음 예와 같이 출력한다.
총 XX 개의 답이 있습니다. 계산시간은 총 Y.YYYY 초 입니다.'''
import time , math
import sys , copy
import multiprocessing
startTime = time.time()
print("Typing Your Number split by space '중복숫자는 불가합니다': < ex : 1 2 3 4 5 6 7 8 9 10 11 ... 15>")
str=input()
try:#스페이스로 구분해서 입력한경우
arr1=[int(a) for a in str.split()]
except ValueError: # , 컴마로 구분해서 입력한경우
arr1=[int(a) for a in str.split(',')]
finally:
arr1.sort()
num=int(math.sqrt(len(arr1)))
table=[[0]*num for i in range(num)]#n차 테이블
def newlistremove(src,a): #list를 받아서, 요소 a 를 삭제한 새 리스트를 반환하는 함수
new_list=copy.copy(src)
new_list.remove(a)
return new_list
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
def mkQuadrantTable(arr1): # 사분면기준 마방진 만들기
match = 0
avg = sum(arr1) / 4
for a in arr1:
blist = newlistremove(arr1, a)
for b in blist:
clist = newlistremove(blist, b)
for c in clist:
dlist = newlistremove(clist, c)
for d in dlist:
if (a + b + c + d != avg): continue #1번째 줄 검사
elist = newlistremove(dlist, d)
for e in elist:
flist = newlistremove(elist, e)
for f in flist:
if(a+b+e+f != avg):continue #1사분면검사
glist = newlistremove(flist, f)
for g in glist:
hlist = newlistremove(glist, g)
for h in hlist:
if(c+d+g+h != avg or e+f+g+h != avg) : continue
ilist = newlistremove(hlist, h)
for i in ilist:
jlist = newlistremove(ilist, i)
for j in jlist:
klist = newlistremove(jlist, j)
for k in klist:
llist = newlistremove(klist, k)
for l in llist:
if (i + j + k + l != avg): continue
mlist = newlistremove(llist, l)
for m in mlist:
if (d+g+j+m !=avg or a + e + i + m != avg): continue
nlist = newlistremove(mlist, m)
for n in nlist:
if (b+f+j+n !=avg or i+j+m+n != avg): continue
olist = newlistremove(nlist, n)
for o in olist:
if (c + g + k + o != avg): continue
plist = newlistremove(olist, o)
for p in plist:
if (a+f+k+p!= avg or m+n+o+p!=avg or d+h+l+p!=avg or k+l+o+p!=avg): continue
print(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,
p)
match += 1
return match
def mkMagicTable(arr1):# 일반적인 마방진형식
match=0
avg = sum(arr1) / 4
for a in arr1:
blist=newlistremove(arr1,a)
for b in blist:
clist=newlistremove(blist,b)
for c in clist:
dlist=newlistremove(clist,c)
for d in dlist:
if(a+b+c+d != avg):continue
elist=newlistremove(dlist,d)
for e in elist:
flist=newlistremove(elist,e)
for f in flist:
glist=newlistremove(flist,f)
for g in glist:
if(a+e+f+g != avg) : continue
hlist=newlistremove(glist,g)
for h in hlist:
ilist=newlistremove(hlist,h)
for i in ilist:
if(d+g+i+h != avg): continue
jlist=newlistremove(ilist,i)
for j in jlist:
klist=newlistremove(jlist,j)
for k in klist:
llist=newlistremove(klist,k)
for l in llist:
if(a+j+k+l != avg):continue
mlist=newlistremove(llist,l)
for m in mlist:
if (e+j+i+m !=avg):continue
nlist=newlistremove(mlist,m)
for n in nlist:
if(f+h+k+n != avg) : continue
if(d+m+n+l != avg) : continue
olist=newlistremove(nlist,n)
for o in olist:
if (b+j+h+o!=avg):continue
plist=newlistremove(olist,o)
for p in plist:
if(c+i+k+p!=avg):continue
if(g+o+p+l !=avg) : continue
print(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p)
match+=1
return match
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
sys.stdout = open("4x4.txt",'w')
#pool=multiprocessing.Pool(processes=2) #멀티프로세싱 연습
#pool.map(mkQuadrantTable,arr1)
count=mkQuadrantTable(arr1)
t=round(time.time() - startTime , 2) #수행시간을 출력
sys.stderr.write("총 {} 개의 답이 있습니다. 계산시간은 총 {} 초 입니다.".format(count, t))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment