def isEmpty(stack): return (len(stack) == 0) def solution(board, moves): answer = 0 b_stack = {} stack = [] height = len(board) width = len(board[0]) for w in range(width): tmp = [] for h in range(height-1, -1, -1): if (board[h][w] == 0): break else: tmp.append(board[h][w]) b_stack[w+1] = tmp for m in moves: if (not isEmpty(b_stack[m])): item = b_stack[m].pop() if (not isEmpty(stack) and stack[-..
1~45숫자 6개의 숫자 고르기 6개:1등/ 5개:2등/ 4개:3등/ ... / 2개: 5등/ 1개:6등/ 0개:6등 0은 맞출 수도 있고 못맞출 수도 있다. def solution(lottos, win_nums): rank = [6,6,5,4,3,2,1] cnt_0 = lottos.count(0) # 0 개수 세기 ans = 0 for x in win_nums: if x in lottos: ans += 1 return rank[cnt_0+ans], rank[ans] print(solution([1,2,3,4,0,0], [1,2,3,4,5,6])) 랭크를 저렇게 배열 인덱스로 고를 수 있도록 하는 것도 매력적인 방법인 것 같다. 보기도 쉽고 말이다. def solution(lottos, win_nums)..
파이썬에서 반올림은 round함수를 사용합니다. round - 파이썬의 내장함수(import 필요x) - 두 번째 매개변수로 반올림 자릿수 결정가능 ex) round(1.5555, 2) = 1.56 # 두 번째 자리까지 표시 - 대부분의 경우 일반적으로 생각하는 반올림기능 수행 - 1.5, 2.5, 3.5 경우 자신과 가장 가까운 짝수 반환 ◼︎ 일반적인 반올림 기능 ◼︎ .5 경우 자신과 가장 가까운 짝수 반환 일반적으로 0.5 반올림은 1이라고 생각하실텐데, 파이썬 round함수는 0을 반환합니다. 여기서도 마찬가지로 1.57을 원했는데 1.56이 나와버립니다. 1) 정수부분이 짝수일 경우 0.1을 더하여 round함수 사용 원래 round(0.5) = 0 인데 1이 나옵니다. 조금 더 일반화 하면 ..
Q. 3번째 자리에서 반올림 하고싶다! 1. round함수 public static long round(double a) round는 소수점 첫 째 자리에서 반올림하여 정수값을 반환해준다. 3번째 자리에서 반올림 하고싶다면 1) 원하는 값에 100을 곱한 뒤 2) 그 값을 round함수에 넣어주고 3) 거기에 100을 나눠주면 된다. 이 때 반환값이 long 정수라는 것에 주의하여 형변환 잘해주자. 2. String.format("%.2f", num) 하고 float으로 형변환 %.2f를 이용해주면 소수점 2번째자리까지 표현된 String값이 나온다. (3번째 자리에서 반올림됨) 이걸 float값으로 받고싶다면 Float.parseFloat() 함수에 집어넣어주면 된다. 3. 곱하고 0.5더해서 int..
import java.util.*; public class prac { static void putWord(HashMap words) { words.put("chair", "의자"); words.put("computer", "컴퓨터"); words.put("Integer", "정수"); } public static void main(String[] args) { HashMap words = new HashMap(); Scanner scanner = new Scanner(System.in); putWord(words); Set keys = words.keySet(); Iterator engWords = keys.iterator(); int i = 0; int right = 0; while (engWords..
보기싫은 책 목록이다. 앞에 10213 이런 숫자들이랑 뒤에 붙은 1이나 2는 쓸모없는 숫자처럼 보인다. 탭 키도 엉망이라 보기 좋지 않다. 하나하나 수정하기는 귀찮으니 프로그램으로 만들어줄 생각이다. 먼저 결과 화면부터 보자. 출판사랑 책이름이 붙어있기는 하지만 아까보다는 보기 좋은 문서같다. public static void main(String[] args) { Vector all = new Vector(); inputDocument(all); scan_and_print(all); } main함수는 간단하게 만들어줬다. all백터에 문서 전체를 받고 scan_and_print함수로 정리된 문서를 출력할거다. static boolean isNo(String str) { // 앞에 10215 이거인지 ..
print(5) print(8*(1+3)) 간단한 연산 가능 print(5>3) print(False) print(not True) print(not (1= 3) print("우리집 "+ animal + "의 이름은 "+ name + "에요") hobby = "공놀이" print(name, "는 ", age, "살이며, ", hobby, "을 아주 좋아해요") print(name + "는 어른일까요? " + str(is_adult)) 변수랑 문자열 합치기 ''' 작성자: 고양이 한 번에 주석처리 컨트롤 슬러쉬 ''' 큰 범위 주석 print(1+1) print(3-2) print(5*2) print(6/3) print(2**3) print(5%3) print(10%3) # 나머지 print(10//3) ..
import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; class BtnDigitListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton btn = (JButton)e.getSource(); String digits = NsystemWin.tf.getText(); if (digits.equals("0")) digits = btn.getText(); else digits = digits + btn.getText(); NsystemWin.tf.setText(digits); } } class Bt..
import javax.swing.*;import java.awt.*;class MyWin extends JFrame { public MyWin() { setTitle("Let's study Java"); setSize(400, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); Container contentPane = this.getContentPane(); contentPane.setBackground(Color.magenta); setVisible(true); }}public class Ex{ public static void main(String[] args) { new MyWin(); }}JFrame에 바로 setBackground 설정이 안..