def write_1d(a): length = len(a) stdio.writeln(length) for i in range(length): element = a[i] if isinstance(element, bool): if element == True: stdio.write(1) else: stdio.write(0) else: stdio.write(element) stdio.write(' ') stdio.writeln()
def write_2d(a): row_count = len(a) col_count = len(a[0]) stdio.writeln(str(row_count) + ' ' + str(col_count)) for row in range(row_count): for col in range(col_count): element = a[row][col] if isinstance(element, bool): if element == True: stdio.write(1) else: stdio.write(0) else: stdio.write(element) stdio.write(' ') stdio.writeln()
import random import sys from introcs.stdlib import stdio n = int(sys.argv[1]) for i in range(n): stdio.writeln(random.random())
import sys from introcs.stdlib import stdio m = int(sys.argv[1]) d = int(sys.argv[2]) y = int(sys.argv[3]) y0 = y - ((14 - m) // 12) x = y0 + (y0 // 4) - (y0 // 100) + (y0 // 400) m0 = m + (12 * ((14 - m) // 12)) - 2 d0 = (d + x + ((31 * m0) // 12)) % 7 stdio.writeln(f"day num = {d0}")
import math from introcs.stdlib import stdio number = [2**x for x in range(1, 11)] for n in number: stdio.write(f"{math.log2(n)}\t") stdio.write(f"{n}\t") stdio.write(f"{math.log(n, math.e)}\t") stdio.write(f"{math.pow(n, 2)}\t") stdio.write(f"{math.pow(n, 3)}\t") stdio.writeln(f"{math.pow(2, n)}")
import sys from introcs.stdlib import stdio n = int(sys.argv[1]) total = 0.0 for i in range(1, n + 1): total += 1.0 / i stdio.writeln(total)
import sys from introcs.stdlib import stdio x0 = float(sys.argv[1]) v0 = float(sys.argv[2]) t = float(sys.argv[3]) G = 9.80665 stdio.writeln(f"x0 + v0t - Gt^2 / 2 = {x0 + (v0 * t) - ((G * (t ** 2)) / 2)}")
import sys from introcs.stdlib import stdio a = float(sys.argv[1]) b = float(sys.argv[2]) total = a + b diff = a - b prod = a * b quot = a / b exp = a**b stdio.writeln(f"{a} + {b} = {total}") stdio.writeln(f"{a} - {b} = {diff}") stdio.writeln(f"{a} * {b} = {prod}") stdio.writeln(f"{a} / {b} = {quot}") stdio.writeln(f"{a} ** {b} = {exp}")
import sys from introcs.stdlib import stdio a = int(sys.argv[1]) b = int(sys.argv[2]) total = a + b diff = a - b prod = a * b quot = a // b rem = a % b exp = a**b stdio.writeln(f'{str(a)} + {str(b)} = {str(total)}') stdio.writeln(f'{str(a)} - {str(b)} = {str(diff)}') stdio.writeln(f'{str(a)} * {str(b)} = {str(prod)}') stdio.writeln(f'{str(a)} // {str(b)} = {str(quot)}') stdio.writeln(f'{str(a)} % {str(b)} = {str(rem)}') stdio.writeln(f'{str(a)} ** {str(b)} = {str(exp)}')
import random import sys from introcs.stdlib import stdarray, stdio n = int(sys.argv[1]) trials = int(sys.argv[2]) deadEnds = 0 for t in range(trials): a = stdarray.create_2d(n, n, False) x = n // 2 y = n // 2 while (x > 0) and (x < n - 1) and (y > 0) and (y < n - 1): a[x][y] = True if a[x - 1][y] and a[x + 1][y] and a[x][y - 1] and a[x][y + 1]: deadEnds += 1 break r = random.randrange(1, 5) if r == 1 and not a[x + 1][y]: x += 1 if r == 2 and not a[x - 1][y]: x -= 1 if r == 3 and not a[x][y + 1]: y += 1 if r == 4 and not a[x][y - 1]: y -= 1 stdio.writeln(f"{100*deadEnds//trials}% 궁지에 몰림")
import math import sys from introcs.stdlib import stdio t = float(sys.argv[1]) v = float(sys.argv[2]) w = 34.74 + (0.6215 * t) + (((0.4275 * t) - 35.75) * math.pow(v, 0.16)) stdio.writeln( f"34.74 + (0.6215 * {t}) + ((0.4275 * {t}) - 35.75) * {v}^0.16 = {w}")
import random from introcs.stdlib import stdio o = random.random() t = random.random() th = random.random() fo = random.random() fi = random.random() arg = (o + t + th + fo + fi) / 5 stdio.writeln( f"arg = {arg}, min = {min(o, t, th, fo, fi)}, max = {max(o, t, th, fo, fi)}" )
import math import sys from introcs.stdlib import stdio t = int(sys.argv[1]) P = float(sys.argv[2]) r = float(sys.argv[3]) stdio.writeln(f"{P * math.exp(r * t)}")
import random import sys from introcs.stdlib import stdarray, stdio n = int(sys.argv[1]) count = 0 collectedCount = 0 isCollected = stdarray.create_1d(n, False) while collectedCount < n: value = random.randrange(0, n) count += 1 if not isCollected[value]: collectedCount += 1 isCollected[value] = True stdio.writeln(count)
from introcs.stdlib import stdio a = True b = False stdio.writeln((not (a and b) and (a or b)) or ((a and b) or not (a or b)))
import random import sys from introcs.stdlib import stdio a = int(sys.argv[1]) b = int(sys.argv[2]) stdio.writeln(random.randint(a, b))
import math from introcs.stdlib import stdio x = 3 y = 4 distance = math.sqrt((x**2) + (y**2)) stdio.writeln(f"x: {x}, y: {y}, distance: {distance}")
from introcs.stdlib import stdio a = 2 b = 3 stdio.writeln(not (a < b) and not (a > b)) stdio.writeln(a == b)
import sys from introcs.stdlib import stdio end_num = int(sys.argv[1]) if end_num > 1000: stdio.writeln("Error: out of range") exit(1) for i in range(1, end_num + 1): if (i % 10) == 1: stdio.writeln(f"{i}st Hello") elif (i % 10) == 2: stdio.writeln(f"{i}nd Hello") elif (i % 10) == 3: stdio.writeln(f"{i}rd Hello") else: stdio.writeln(f"{i}th Hello")
import math import sys from introcs.stdlib import stdio x1 = float(sys.argv[1]) y1 = float(sys.argv[2]) x2 = float(sys.argv[3]) y2 = float(sys.argv[4]) x1 = math.radians(x1) y1 = math.radians(y1) x2 = math.radians(x2) y2 = math.radians(y2) angle1 = math.acos( math.sin(x1) * math.sin(x2) + math.cos(x1) * math.cos(x2) * math.cos(y1 - y2) ) angle1 = math.degrees(angle1) delta = 60 * angle1 stdio.writeln(f"delta: {delta}")
from introcs.stdlib import stdio ruler1 = '1' ruler2 = ruler1 + ' 2 ' + ruler1 ruler3 = ruler2 + ' 3 ' + ruler2 ruler4 = ruler3 + ' 4 ' + ruler3 stdio.writeln(ruler1) stdio.writeln(ruler2) stdio.writeln(ruler3) stdio.writeln(ruler4)
import math import sys from introcs.stdlib import stdio x = float(sys.argv[1]) y = float(sys.argv[2]) r = math.sqrt((x ** 2) + (y ** 2)) theta = math.atan2(y, x) stdio.writeln(f"r = {r}, theta = {theta}")
import random import sys from introcs.stdlib import stdio stake = int(sys.argv[1]) goal = int(sys.argv[2]) trials = int(sys.argv[3]) bets = 0 wins = 0 for t in range(trials): cash = stake while (cash > 0) and (cash < goal): bets += 1 if random.randrange(0, 2) == 0: cash += 1 else: cash -= 1 if cash == goal: wins += 1 stdio.writeln(f"{100 * wins // trials}% 이김") stdio.writeln(f"평균 베팅 수: {bets // trials}")
import math import sys from introcs.stdlib import stdio n = int(sys.argv[1]) if n < 0: exit(1) for number in range(1, n + 1): if math.log2(number) % 1 == 0: stdio.writeln(f"{number}")
import datetime import sys from introcs.stdlib import stdio month = int(sys.argv[1]) day = int(sys.argv[2]) cal_date = datetime.datetime(year=2020, month=month, day=day) start_date = datetime.datetime(year=2020, month=3, day=20) end_date = datetime.datetime(year=2020, month=6, day=20) stdio.writeln( f"2020.{month:02}.{day:02} is {(start_date < cal_date < end_date)}")
import sys from introcs.stdlib import stdio stdio.write('HI, ') stdio.write(sys.argv[1]) stdio.writeln('. How are you?')
import sys from introcs.stdlib import stdio n = int(sys.argv[1]) for i in range(1, n + 1): for j in range(1, n + 1): if (i % j == 0) or (j % i == 0): stdio.write('* ') else: stdio.write(' ') stdio.writeln()
import math import sys from introcs.stdlib import stdio b = float(sys.argv[1]) c = float(sys.argv[2]) discriminant = b * b - 4.0 * c d = math.sqrt(discriminant) stdio.writeln((-b + d) / 2.0) stdio.writeln((-b - d) / 2.0)
from introcs.stdlib import stdio dragon0 = 'F' nragon0 = 'F' dragon1 = dragon0 + 'L' + nragon0 nragon1 = dragon0 + 'R' + nragon0 stdio.writeln(dragon0) stdio.writeln(dragon1)
import sys from introcs.stdlib import stdio a = int(sys.argv[1]) b = int(sys.argv[2]) c = int(sys.argv[3]) if a == b == c: stdio.writeln('equal') else: stdio.writeln('not equal')