Problem:
Koch Curve Write a program which reads an integer n and draws a Koch curve based on recursive calles of depth n . The Koch curve is well known as a kind of fractals . You can draw a Koch curve in the following algorithm:  Divide a given segment (p1, p2) into three equal segments.  Replace the middle segment by the two sides of an equilateral triangle (s, u, t) of the same length as the segment.  Repeat this procedure recursively for new segments (p1, s), (s, u), (u, t), (t, p2).    You should start (0, 0), (100, 0) as the first segment. Input An integer n is given. Output Print each point (x, y) of the Koch curve. Print a point in a line. You should start the point(0, 0), which is the endpoint of the first segment and end with the point (100, 0), the other endpoint so that you can draw the Koch curve as an unbroken line. Each solution should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10-4. Constraints  0 ≤ n ≤ 6  Sample Input 1 1 Sample Output 1 0.00000000 0.00000000
33.33333333 0.00000000
50.00000000 28.86751346
66.66666667 0.00000000
100.00000000 0.00000000 Sample Input 2 2 Sample Output 2 0.00000000 0.00000000
11.11111111 0.00000000
16.66666667 9.62250449
22.22222222 0.00000000
33.33333333 0.00000000
38.88888889 9.62250449
33.33333333 19.24500897
44.44444444 19.24500897
50.00000000 28.86751346
55.55555556 19.24500897
66.66666667 19.24500897
61.11111111 9.62250449
66.66666667 0.00000000
77.77777778 0.00000000
83.33333333 9.62250449
88.88888889 0.00000000
100.00000000 0.00000000 Notes

Solution (Python):
from math import sqrt
q3 = sqrt(3)
fmt = "%.8f %.8f"
def koch(x0, y0, x1, y1, c):
    if c == n:
        return (fmt % (x0, y0),)
    xp = (x0*2 + x1) / 3
    yp = (y0*2 + y1) / 3

    xq = (x0 + x1*2) / 3
    yq = (y0 + y1*2) / 3

    dx = (x1 - x0) / 6
    dy = (y1 - y0) / 6

    xr = xp + (dx - dy * q3)
    yr = yp + (dx * q3 + dy)

    return koch(x0, y0, xp, yp, c+1) + koch(xp, yp, xr, yr, c+1) + koch(xr, yr, xq, yq, c+1) + koch(xq, yq, x1, y1, c+1)
n = int(input())
print(*koch(0, 0, 100, 0, 0)+(fmt % (100, 0),), sep='\n')

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(ty-sy)*(1/2)+(tx-sx)*(math.sqrt(3)/2)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)

n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n,x1,y1,x2,y2):
    if n == 0:
        plot(x1,y1)
        return
    sx = (2*x1+x2)/3
    sy = (2*y1+y2)/3
    tx = (x1+2*x2)/3
    ty = (y1+2*y2)/3
    ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1,x1,y1,sx,sy)
    Koch(n-1,sx,sy,ux,uy)
    Koch(n-1,ux,uy,tx,ty)
    Koch(n-1,tx,ty,x2,y2)
    
n = int(input())
Koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
d = int(input().rstrip())

import math
sin60 = math.sin(math.radians(60))
cos60 = math.cos(math.radians(60))


def make_koch(d, p1, p2):
    s = ((p1[0]*2 + p2[0]) / 3, (p1[1]*2 + p2[1]) / 3)
    t = ((p1[0] + p2[0]*2) / 3, (p1[1] + p2[1]*2) / 3)
    u = ((t[0] - s[0]) * cos60 - (t[1] - s[1])*sin60 + s[0], (t[0] - s[0]) * sin60 + (t[1] - s[1]) * cos60 + s[1])
    if d == 0:
        return [p1]
    elif d == 1:
        return [p1, s, u, t]
    else:
        return make_koch(d-1, p1, s) + make_koch(d-1, s, u) + make_koch(d-1, u, t) + make_koch(d-1, t, p2)
result_list = make_koch(d, (0, 0), (100, 0))

for result in result_list:
    print(result[0], result[1])
print("100 0")

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
def Koch(n, x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)
    
n=int(input())
Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
  print(f'{x:.8f} {y:.8f}')

def Koch(n,x1,y1,x2,y2):
  if n ==0:
    plot(x1,y1)
    return
  sx = (2*x1+x2)/3
  sy = (2*y1+y2)/3
  tx = (x1+2*x2)/3
  ty = (y1+2*y2)/3
  ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
  uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
  Koch(n-1, x1,y1, sx,sy)
  Koch(n-1, sx,sy, ux,uy)
  Koch(n-1, ux,uy, tx,ty)
  Koch(n-1, tx,ty, x2,y2)

n=int(input())
Koch(n, 0,0, 100,0)
plot(100, 0)

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1,x1,y1,sx,sy)
    Koch(n-1,sx,sy,ux,uy)
    Koch(n-1,ux,uy,tx,ty)
    Koch(n-1,tx,ty,x2,y2)
n=int(input())
Koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def Koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1,x1,y1,sx,sy)
    Koch(n-1,sx,sy,ux,uy)
    Koch(n-1,ux,uy,tx,ty)
    Koch(n-1,tx,ty,x2,y2)
    
n=int(input())
Koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1,x1,y1,sx,sy)
    Koch(n-1,sx,sy,ux,uy)
    Koch(n-1,ux,uy,tx,ty)
    Koch(n-1,tx,ty,x2,y2)

n=int(input())
Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
import math

n=int(input())


def koch(d,p1,p2):
  if d==0:
    return
  
  s=[(2*p1[0]+1*p2[0])/3,(2*p1[1]+1*p2[1])/3]
  t=[(1*p1[0]+2*p2[0])/3,(1*p1[1]+2*p2[1])/3]
  
  u=[
    (t[0]-s[0])*math.cos(math.radians(60))-(t[1]-s[1])*math.sin(math.radians(60))+s[0],
    (t[0]-s[0])*math.sin(math.radians(60))+(t[1]-s[1])*math.cos(math.radians(60))+s[1]
    ]
  koch(d-1,p1,s)
  print(*s)
  koch(d-1,s,u)
  print(*u)
  koch(d-1,u,t)
  print(*t)
  koch(d-1,t,p2)

print('0 0')
koch(n,[0,0],[100,0])
print('100 0')

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def Koch(n, x1,y1,x2,y2):
    if n == 0:
        plot(x1,y1)
        return
    sx = (2*x1+x2)/3
    sy = (2*y1+y2)/3
    tx = (x1+x2*2)/3
    ty = (y1+y2*2)/3
    ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)

n = int(input())
Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
def Koch(n, x1,y1,x2,y2):
    if n ==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1,x1,y1, sx,sy)
    Koch(n-1,sx,sy, ux,uy)
    Koch(n-1,ux,uy, tx,ty)
    Koch(n-1,tx,ty, x2,y2)
   
n=int(input())
Koch(n,0,0, 100,0)
plot(100,0)

Solution (Python):
import math

def plot (x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n,x1,y1,x2,y2):
    if n ==0:
        plot(x1,y1)
        return
    sx = (2 * x1 + x2)/ 3
    sy = (2 * y1 + y2)/ 3
    tx = (x1 + 2 * x2)/ 3
    ty = (y1 + 2 * y2)/ 3
    ux = (tx-sx)/2 -(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx-sx)*(math.sqrt(3)/2) +(ty-sy)/2+sy
    
    Koch(n-1,x1,y1,sx,sy)
    Koch(n-1,sx,sy,ux,uy)
    Koch(n-1,ux,uy,tx,ty)
    Koch(n-1,tx,ty,x2,y2)
    
n= int(input())
Koch(n,0,0,100,0)

plot(100,0)

Solution (Python):
from math import cos, sin, radians
def koch(n, a, b):
        if n == 0:
                return
        s = [0, 0]
        t = [0, 0]
        u = [0, 0]
        s[0] = (2 * a[0] + 1 * b[0]) / 3
        s[1] = (2 * a[1] + 1 * b[1]) / 3
        t[0] = (1 * a[0] + 2 * b[0]) / 3
        t[1] = (1 * a[1] + 2 * b[1]) / 3
        u[0] = (t[0] - s[0]) * cos(radians(60)) - (t[1] - s[1]) * sin(radians(60)) + s[0]
        u[1] = (t[0] - s[0]) * sin(radians(60)) + (t[1] - s[1]) * cos(radians(60)) + s[1]
        koch(n - 1, a, s)
        print('{:.08f} {:.08f}'.format(s[0], s[1]))
        koch(n - 1, s, u)
        print('{:.08f} {:.08f}'.format(u[0], u[1]))
        koch(n - 1, u, t)
        print('{:.08f} {:.08f}'.format(t[0], t[1]))
        koch(n - 1, t, b)

a = [0, 0]
b = [100, 0]
n = int(input())
print('{:.08f} {:.08f}'.format(a[0], a[1]))
koch(n, a, b)
print('{:.08f} {:.08f}'.format(b[0], b[1]))

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
def Koch(n, x1,y1,x2,y2):
    if n == 0:
        plot(x1,y1)
        return
    sx = (2*x1+x2)/3
    sy = (2*y1+y2)/3
    tx = (x1+2*x2)/3
    ty = (y1+2*y2)/3
    ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)
    
n = int(input())
Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):


import math
def plot(x,y):
    print(f"{x:.8f} {y:.8f}")
    
def koch(n, x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    
    koch(n-1, x1,y1, sx,sy)
    koch(n-1, sx,sy, ux,uy)
    koch(n-1, ux,uy, tx,ty)
    koch(n-1, tx,ty, x2,y2)
    
n=int(input())
koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
def Koch(n, x1,y1,x2,y2):
    if n == 0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)
    
n=int(input())
Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):


import math

def plot(x,y):
     print(f'{x:.8f} {y:.8f}')
     
def Koch(n, x1,y1, x2,y2):
     if n==0:
          plot(x1,y1)
          return
     sx=(2*x1+x2)/3
     sy=(2*y1+y2)/3
     tx=(x1+2*x2)/3
     ty=(y1+2*y2)/3
     ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
     uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
     Koch(n-1, x1,y1, sx,sy)
     Koch(n-1, sx,sy, ux,uy)
     Koch(n-1, ux,uy, tx,ty)
     Koch(n-1, tx,ty, x2,y2)
n=int(input())
Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
import math

n = int(input())
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def kock(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx = (2 * x1 + x2) / 3
    sy = (2 * y1 + y2) / 3
    tx = (x1 + 2 * x2) / 3
    ty = (y1 + 2 * y2) / 3
    ux = (tx - sx)*(1/2)-(ty - sy)*(math.sqrt(3)/2)+sx
    uy = (tx - sx)*(math.sqrt(3)/2)+(ty - sy)*(1/2)+sy
    
    kock(n-1, x1, y1, sx, sy)
    kock(n-1, sx, sy, ux, uy)
    kock(n-1, ux, uy, tx, ty)
    kock(n-1, tx, ty, x2, y2)
    
kock(n,0,0,100,0) 
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def koch(n,ax,ay,bx,by):
    if n==0:
        plot(ax,ay)
        return
    
    sx=(2*ax+bx)/3
    sy=(2*ay+by)/3
    tx=(ax+2*bx)/3
    ty=(ay+2*by)/3
    ux=((tx-sx)*math.cos(math.radians(60)))-((ty-sy)*math.sin(math.radians(60)))+sx
    uy=((tx-sx)*math.sin(math.radians(60)))+((ty-sy)*math.cos(math.radians(60)))+sy

    koch(n-1, ax,ay, sx,sy)
    koch(n-1, sx,sy, ux,uy)
    koch(n-1, ux,uy, tx,ty)
    koch(n-1, tx,ty, bx,by)
    
n=int(input())
koch(n, 0,0, 100,0)    
plot(100,0)

Solution (Python):
import sys
import math

n = int(sys.stdin.readline())


def koch(st, en):

    st_x, st_y = st[0], st[1]
    en_x, en_y = en[0], en[1]

    p1_x, p1_y = (2 * st_x + en_x) / 3, (2 * st_y + en_y) / 3
    p2_x, p2_y = (st_x + 2 * en_x) / 3, (st_y + 2 * en_y) / 3

    vec_x, vec_y = (p2_x - p1_x, p2_y - p1_y)  

    
    vec_r_x = vec_x * math.cos(math.pi / 3) - vec_y * math.sin(math.pi / 3)
    vec_r_y = vec_x * math.sin(math.pi / 3) + vec_y * math.cos(math.pi / 3)

    p3_x = vec_r_x + p1_x
    p3_y = vec_r_y + p1_y

    return (p1_x, p1_y), (p3_x, p3_y), (p2_x, p2_y)


def koch_r(n, st, en):
    if n == 0:
        return
    
    s, u, t = koch(st, en)
    
    koch_r(n - 1, st, s)
    print(f"{s[0]:f} {s[1]:f}")
    koch_r(n - 1, s, u)
    print(f"{u[0]:f} {u[1]:f}")
    koch_r(n - 1, u, t)
    print(f"{t[0]:f} {t[1]:f}")
    koch_r(n - 1, t, en)


st, en = (0, 0), (100, 0)

print(f"{st[0]:f} {st[1]:f}")
koch_r(n, st, en)
print(f"{en[0]:f} {en[1]:f}")

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    

def Koch(n, x1,y1, x2,y2):
    if n == 0:
        plot(x1,y1)
        return
    sx = (2 * x1 + x2) / 3
    sy = (2 * y1 + y2) / 3
    tx = (x1 + x2 * 2) / 3
    ty = (y1 + y2 * 2) / 3
    ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)

n = int(input())
Koch(n, 0,0, 100,0)    
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1,x1,y1,sx,sy)
    Koch(n-1,sx,sy,ux,uy)
    Koch(n-1,ux,uy,tx,ty)
    Koch(n-1,tx,ty,x2,y2)
    
n=int(input())
Koch(n,0,0,100,0)
print('100.00000000 0.00000000')

Solution (Python):

from sys import stdin
import math

fmt = "%.8f %.8f"
cos60 = math.cos(math.radians(60))
sin60 = math.sin(math.radians(60))

def koch(d,p1,p2):
    
    
    

    if d == 0:
        return
    sx = (2 * p1[0] + 1 * p2[0]) / 3
    sy = (2 * p1[1] + 1 * p2[1]) / 3
    tx = (1 * p1[0] + 2 * p2[0]) / 3
    ty = (1 * p1[1] + 2 * p2[1]) / 3

    dx = tx - sx
    dy = ty - sy

    ux = dx * cos60 - dy * sin60 + sx
    uy = dx * sin60 + dy * cos60 + sy

    koch(d - 1, p1, (sx, sy))
    print(fmt % (sx, sy))
    koch(d - 1, (sx, sy), (ux, uy))
    print(fmt % (ux, uy))
    koch(d - 1, (ux, uy), (tx, ty))
    print(fmt % (tx, ty))
    koch(d - 1, (tx, ty), p2)


n = int(stdin.readline().rstrip())

a_p1_x = a_p1_y = 0
a_p2_x = 100
a_p2_y = 0

print(fmt % (a_p1_x, a_p1_y))
koch(n, (a_p1_x, a_p1_y), (a_p2_x, a_p2_y))
print(fmt % (a_p2_x, a_p2_y))

Solution (Python):

import math

def plot(x,y):
    print(f'{x:.8f}',"",f'{y:.8f}')

def Koch(n,x1,y1,x2,y2):
    if n == 0:
        plot(x1,y1)
        return
    sx = (2 * x1 + x2) / 3
    sy = (2 * y1 + y2) / 3
    tx = (x1 + 2 * x2) / 3
    ty = (y1 + 2 * y2) / 3
    ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/(2))+sx
    uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1,x1,y1, sx,sy)
    Koch(n-1,sx,sy, ux,uy)
    Koch(n-1,ux,uy, tx,ty)
    Koch(n-1,tx,ty, x2,y2)
    
n = int(input())    
Koch(n,0,0, 100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:8f} {y:8f}')
    
def Koch(n, x1,y1,x2,y2):
    if n == 0:
        plot(x1,y1)
        return
    sx = (2*x1+x2) / 3
    sy = (2*y1+y2) / 3
    tx = (x1+2*x2) / 3
    ty = (y1+2*y2) / 3
    ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1, x1,y1,sx,sy)
    Koch(n-1, sx,sy,ux,uy)
    Koch(n-1, ux,uy,tx,ty)
    Koch(n-1, tx,ty,x2,y2)

n=int(input())    
Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):



import math

n = int(input())

p1 = [0.0, 0.0]
p2 = [100.0, 0.0]

def rad(r) :
    return math.radians(r)

def sin(s) :
    return math.sin(s)
    
def cos(s) :
    return math.cos(s)

def A(n, p1, p2) :
    if n == 0 :
        return 0
    else :
        s = [0, 0]
        t = [0, 0]
        u = [0, 0]
        
        s[0] = (p2[0] - p1[0]) / 3 + p1[0]
        s[1] = (p2[1] - p1[1]) / 3 + p1[1]
        t[0] = (p2[0] - p1[0]) / 3 * 2 + p1[0]
        t[1] = (p2[1] - p1[1]) / 3 * 2 + p1[1]
        
        u[0] = (t[0] - s[0]) * cos(rad(60)) - (t[1] - s[1]) * sin(rad(60)) + s[0]
        u[1] = (t[0] - s[0]) * sin(rad(60)) + (t[1] - s[1]) * cos(rad(60)) + s[1]
        
        A(n - 1, p1, s)
        p(s[0], s[1])
        
        A(n - 1, s, u)
        p(u[0], u[1])
        
        A(n - 1, u, t)
        p(t[0], t[1])
        
        A(n - 1, t, p2)
        
def p(x, y) :
    print(f"{x:.8f} {y:.8f}")
    
print(p1[0], p1[1])
A(n, p1, p2)
print(p2[0], p2[1])

Solution (Python):
import math

n = int(input())
koch = [(0.0, 0.0), (100.0, 0.0)]
SIN60 = math.sin(math.radians(60))

for i in range(n):
    nextKoch = []
    for j in range(len(koch) - 1):
        p1 = koch[j]
        p2 = koch[j+1]
        
        s = ((p1[0] * 2 + p2[0]) / 3, (p1[1] * 2 + p2[1]) / 3)
        t = ((p1[0] + p2[0] * 2) / 3, (p1[1] + p2[1] * 2) / 3)
        
        ux = (t[0]-s[0])/2 - (t[1]-s[1])*SIN60 + s[0]
        uy = (t[0]-s[0])*SIN60 + (t[1]-s[1])/2 + s[1]
        u = (ux, uy)
        nextKoch[len(nextKoch):len(nextKoch)] = [p1, s, u, t]
    nextKoch.append(koch[-1])
    koch = nextKoch

for i in range(len(koch)):
    print('{0:.8f} {1:.8f}'.format(koch[i][0], koch[i][1]))

Solution (Python):
import math

def plot(x,y):
    print(f"{x:.8f} {y:.8f}")

def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*math.cos(math.pi/3)-(ty-sy)*math.sin(math.pi/3)+sx
    uy=(tx-sx)*math.sin(math.pi/3)+(ty-sy)*math.cos(math.pi/3)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)
    
n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def koch(n, x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx = ( 2*x1 + x2 ) /3
    sy = ( 2*y1 + y2 ) /3
    tx = ( x1 + 2*x2 ) /3
    ty = ( y1 + 2*y2 ) /3
    ux = (tx-sx)*(1/2) - (ty-sy)*(math.sqrt(3)/2) + sx
    uy = (tx-sx)*(math.sqrt(3)/2) + (ty-sy)*(1/2) + sy
    koch(n-1 ,x1,y1,sx,sy)
    koch(n-1 ,sx,sy,ux,uy)
    koch(n-1 ,ux,uy,tx,ty)
    koch(n-1 ,tx,ty,x2,y2)
    
n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
def print8f(xy):
    from decimal import Decimal, ROUND_HALF_UP
    acc = Decimal("0.00000001")
    x, y = xy
    x = Decimal(str(x)).quantize(acc, rounding=ROUND_HALF_UP)
    y = Decimal(str(y)).quantize(acc, rounding=ROUND_HALF_UP)
    print("{:.8f} {:.8f}".format(x, y))


def kochCurve(p1p2, n, ang):
    from math import sin, cos, radians, sqrt
    if n == 0:
        print8f(p1p2[1])
        return
    p1, p2 = p1p2
    xd3 = (p2[0] - p1[0]) / 3
    yd3 = (p2[1] - p1[1]) / 3
    s = [p1[0] + xd3, p1[1] + yd3]
    t = [p2[0] - xd3, p2[1] - yd3]
    st = sqrt((s[0] - t[0])**2 + (s[1] - t[1])**2)
    u = [s[0] + cos(radians(ang + 60)) * st,
         s[1] + sin(radians(ang + 60)) * st]

    kochCurve([p1, s], n - 1, ang)
    kochCurve([s, u], n - 1, ang + 60)
    kochCurve([u, t], n - 1, ang - 60)
    kochCurve([t, p2], n - 1, ang)


def resolve():
    n = int(input())
    p1p2 = [[0, 0], [100, 0]]
    print8f(p1p2[0])
    kochCurve(p1p2, n, 0)


resolve()

Solution (Python):
import math

def plot(x,y):
    print(f"{x:.8f} {y:.8f}")

def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)
    
n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1,x1,y1,sx,sy)
    Koch(n-1,sx,sy,ux,uy)
    Koch(n-1,ux,uy,tx,ty)
    Koch(n-1,tx,ty,x2,y2)

n=int(input())
Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
from math import cos, sin, pi
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def koch (n, x1, y1, x2, y2):
    if n == 0:
        plot(x1,y1)
        return
    sx = (2*x1+x2)/3
    sy = (2*y1+y2)/3
    tx = (x1+2*x2)/3
    ty = (y1+2*y2)/3
    ux = (tx-sx)*cos(pi/3)-(ty-sy)*sin(pi/3)+sx
    uy = (tx-sx)*sin(pi/3)+(ty-sy)*cos(pi/3)+sy
    koch(n-1, x1, y1, sx, sy)
    koch(n-1, sx, sy, ux, uy)
    koch(n-1, ux, uy, tx, ty)
    koch(n-1, tx, ty, x2, y2)
    
n = int(input())    
koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def Koch(n,x1, y1, x2, y2):
    if n == 0:
        plot(x1,y1)
        return
    sx = (2 * x1 + x2) / 3
    sy = (2 * y1 + y2) / 3
    tx = (x1 + 2 * x2) / 3
    ty = (y1 + 2 * y2) / 3
    ux = (tx - sx) * (1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx - sx) * (math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1, x1, y1, sx, sy)
    Koch(n-1, sx, sy, ux, uy)
    Koch(n-1, ux, uy, tx, ty)
    Koch(n-1, tx, ty, x2, y2)

n = int(input())    
Koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return

    sx = (2*x1+x2)/3
    sy = (2*y1+y2)/3
    tx = (x1+2*x2)/3
    ty = (y1+2*y2)/3
    ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)    
    koch(n-1,tx,ty,x2,y2)    
    

n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def Koch(n,x1,y1,x2,y2):
    if n == 0:
        plot(x1,y1)
        return
    sx = (2*x1+x2)/3
    sy = (2*y1+y2)/3
    tx = (x1+2*x2)/3
    ty = (y1+2*y2)/3
    ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1,x1,y1, sx,sy)
    Koch(n-1,sx,sy, ux,uy)
    Koch(n-1,ux,uy, tx,ty)
    Koch(n-1,tx,ty, x2,y2)
    
n=int(input()) 
Koch(n,0,0, 100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f"{x:.8f} {y:.8f}")

def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)
    
n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:8f} {y:8f}')

def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)
 
n=int(input())   
koch(n,0,0 ,100,0)
plot(100,0)

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+1*x2)/3
    sy=(2*y1+1*y2)/3
    tx=(1*x1+2*x2)/3
    ty=(1*y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)

n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def Koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1,x1,y1,sx,sy)
    Koch(n-1,sx,sy,ux,uy)
    Koch(n-1,ux,uy,tx,ty)
    Koch(n-1,tx,ty,x2,y2)

   

n=int(input())    
Koch(n,0,0, 100,0)
plot(100,0)

Solution (Python):
import math

def RotationMatrix(theta):
    '''
    theta in degree
    '''
    c = math.cos(theta*math.pi/180)
    s = math.sin(theta*math.pi/180)
    mat = [[c,-s],[s,c]]
    return mat

def Rotate60(vec):
    mat = RotationMatrix(60)
    result_vec = []
    result_vec.append(mat[0][0]*vec[0] + mat[0][1]*vec[1])
    result_vec.append(mat[1][0]*vec[0] + mat[1][1]*vec[1])
    return result_vec

def get_points(a_vec,b_vec):
    s_vec = [(2*a+b)/3 for (a,b) in zip(a_vec, b_vec)]
    t_vec = [(a+2*b)/3 for (a,b) in zip(a_vec, b_vec)]
    u_vec = [(b-a)/3 for (a,b) in zip(a_vec,b_vec)]
    u_vec = Rotate60(u_vec)
    u_vec = [s+u for (s,u) in zip(s_vec,u_vec)]
    return s_vec, t_vec, u_vec

def get_koch(A, n,cnt, a_vec,b_vec):
    if cnt >= n:
        return
    else:
        cnt += 1
        s_vec, t_vec, u_vec = get_points(a_vec,b_vec)
        get_koch(A, n,cnt, a_vec, s_vec)
        A.append(s_vec)
        get_koch(A, n,cnt, s_vec, u_vec)
        A.append(u_vec)
        get_koch(A, n,cnt, u_vec, t_vec)
        A.append(t_vec)
        get_koch(A, n,cnt, t_vec, b_vec)
        
        
n = int(input())
cnt = 0
a_vec = [0,0]
b_vec = [100,0]
A = []
A.append(a_vec)
get_koch(A, n,cnt, a_vec,b_vec)
A.append(b_vec)
for a in A:
    print(*a)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n, x1,y1,x2,y2):
    if n == 0: 
        plot(x1,y1)        
        return
    sx = (2 * x1 + x2) / 3
    sy = (2 * y1 + y2) / 3
    tx = (x1 + 2 * x2) / 3
    ty = (y1 + 2 * y2) / 3
    ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1, x1, y1, sx, sy)
    Koch(n-1, sx, sy, ux, uy)
    Koch(n-1, ux, uy, tx, ty)
    Koch(n-1, tx, ty, x2, y2)

n = int(input())
Koch(n, 0, 0, 100, 0)
plot(100,0)

Solution (Python):
import math

n = int(input())

p1, p2 = (0, 0), (100, 0)

def koch(d, p1, p2):
    
    if d == n:
        return

    th = math.radians(60)
    
    s = (2/3 * p1[0] + 1/3 * p2[0], 2/3 * p1[1] + 1/3 * p2[1])
    t = (1/3 * p1[0] + 2/3 * p2[0], 1/3 * p1[1] + 2/3 * p2[1])
    u = ((t[0]-s[0])* math.cos(th) - (t[1]-s[1])*math.sin(th) + s[0], \
    (t[0]-s[0])*math.sin(th) + (t[1]-s[1])*math.cos(th)+s[1])

    koch(d+1, p1, s)
    print(*s)
    koch(d+1, s, u)
    print(*u)
    koch(d+1, u, t)
    print(*t)
    koch(d+1, t, p2)

print(*p1)
koch(0, p1, p2)
print(*p2)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    n = I()

    def rec(depth, p0_x, p0_y, p1_x, p1_y):
        if depth!=n:
            p2_x = p0_x*2/3 + p1_x*1/3
            p2_y = p0_y*2/3 + p1_y*1/3
            p4_x = p0_x*1/3 + p1_x*2/3
            p4_y = p0_y*1/3 + p1_y*2/3
            p3_x = (1/2)*(p4_x-p2_x)-(3**0.5/2)*(p4_y-p2_y)+p2_x
            p3_y = (3**0.5/2)*(p4_x-p2_x)+(1/2)*(p4_y-p2_y)+p2_y

            rec(depth+1, p0_x, p0_y, p2_x, p2_y)
            print(p2_x, p2_y)
            rec(depth+1, p2_x, p2_y, p3_x, p3_y)
            print(p3_x, p3_y)
            rec(depth+1, p3_x, p3_y, p4_x, p4_y)
            print(p4_x, p4_y)
            rec(depth+1, p4_x, p4_y, p1_x, p1_y)
    
    print(0, 0)
    rec(0, 0, 0, 100, 0)
    print(100, 0)

if __name__ == '__main__':
    resolve()

Solution (Python):
N = int(input())

def kccv(a, b, c, d):
    return [a*2/3 + c/3, b*2/3 + d/3, a/2 + b*(3**0.5)/6 + c/2 - d*(3**0.5)/6, -a*(3**0.5)/6 + b/2 + c*(3**0.5)/6 + d/2, a/3 + c*2/3, b/3 + d*2/3]

ans = [[[0,0],[100,0]]]

for i in range(1,N+1):
    m = len(ans[i-1])
    newset = []
    for j in range(m-1):
        newset.append(ans[i-1][j])
        newdot = kccv(ans[i-1][j][0], ans[i-1][j][1], ans[i-1][j+1][0], ans[i-1][j+1][1])
        x1 = newdot[0]
        y1 = newdot[1]
        x2 = newdot[2]
        y2 = newdot[3]
        x3 = newdot[4]
        y3 = newdot[5]
        newset.append([x1,y1])
        newset.append([x2,y2])
        newset.append([x3,y3])
    newset.append(ans[i-1][m-1])
    ans.append(newset)
           
    
for i in range(len(ans[N])):
    print(ans[N][i][0],ans[N][i][1])

Solution (Python):
import math

def koch(n, p1, p2):
    if n == 0:
        return
    
    sx = 2*p1[0]/3 + p2[0]/3
    sy = 2*p1[1]/3 + p2[1]/3
    tx = p1[0]/3 + 2*p2[0]/3
    ty = p1[1]/3 + 2*p2[1]/3
    ux = (tx - sx)*math.cos(math.radians(60))  - (ty - sy)*math.sin(math.radians(60)) + sx
    uy = (tx - sx)*math.sin(math.radians(60)) + (ty - sy)*math.cos(math.radians(60)) + sy
    koch(n-1, p1, [sx, sy])
    print(sx, sy)
    koch(n-1, [sx, sy], [ux, uy])
    print(ux, uy)
    koch(n-1, [ux, uy], [tx, ty])
    print(tx, ty)
    koch(n-1, [tx, ty], p2)
    
    
    
    
    
    
n = int(input())

p1 = [0, 0]
p2 = [100, 0]
print(p1[0], p1[1])
koch(n, p1, p2)
print(p2[0], p2[1])

Solution (Python):
import math
import sys
class Point:
    def __init__(self):
        self.x = 0
        self.y = 0

def koch(n,a,b):
    th = math.pi*(60.0/180.0)
    if n == 0:
        return
    s = Point()
    t = Point()
    u = Point()
    s.x = (2.0*a.x + 1.0*b.x)/3
    s.y = (2.0*a.y + 1.0*b.y)/3
    t.x = (1.0*a.x + 2.0*b.x)/3
    t.y = (1.0*a.y + 2.0*b.y)/3
    u.x = (t.x-s.x)*math.cos(th) - (t.y - s.y)*math.sin(th) + s.x
    u.y = (t.x-s.x)*math.sin(th) + (t.y - s.y)*math.cos(th) + s.y
    koch(n-1,a,s)
    print(s.x,s.y)
    koch(n-1,s,u)
    print(u.x,u.y)
    koch(n-1,u,t)
    print(t.x,t.y)
    koch(n-1,t,b)

if __name__ == "__main__":
    n = int(input())
    a = Point()
    b = Point()
    a.x = 0
    a.y = 0
    b.x = 100
    b.y = 0

    print(a.x,a.y)
    koch(n,a,b)
    print(b.x,b.y)