import java.util.Scanner;
class Main {
public static void main(String[] args){
Scanner stdin = new Scanner(System.in);
int n = stdin.nextInt();
int k = stdin.nextInt();
int cnt = 0;
int[] coin = new int[n];
for(int i = 0; i < n; i++){
coin[i] = stdin.nextInt();
}
for(int j = n - 1; j >= 0; j--){
while(true){
if(k >= coin[j]){
k -= coin[j];
cnt++;
}
else
break;
}
}
System.out.print(cnt);
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class boj_1914 {
static int cnt = 0;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
hanoi(N, 1, 3);
System.out.println(cnt);
if(N <= 20) {
hanoi_print(N, 1, 3);
}
}
static void hanoi(int n, int x, int y){
if(n > 1){
hanoi(n-1, x, 6 - x - y);
}
cnt += 1;
if(n > 1){
hanoi(n-1, 6 - x - y, y);
}
}
static void hanoi_print(int n, int x, int y){
if(n > 1){
hanoi_print(n-1, x, 6 - x - y);
}
System.out.printf("%d %d\n",x, y);
if(n > 1){
hanoi_print(n-1, 6 - x - y, y);
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
BigInteger cnt = new BigInteger("2");
cnt = cnt.pow(N);
cnt = cnt.subtract(BigInteger.ONE);
System.out.println(cnt);
if(N <= 20) {
hanoi_print(N, 1, 3);
}
}
static void hanoi_print(int n, int x, int y){
if(n > 1){
hanoi_print(n-1, x, 6 - x - y);
}
System.out.println(x + " " + y);
if(n > 1){
hanoi_print(n-1, 6 - x - y, y);
}
}
}
Arrays.sort()
뒤 구멍 - 앞 구멍 + 1
이 테이프의 길이보다 짧으면 cnt 1을 빼준다. (테이프 하나로 막을 수 있으므로)import java.util.Arrays;
import java.util.Scanner;
import static java.lang.Math.abs;
public class boj_1449{
public static Scanner stdin = new Scanner(System.in);
static int cnt;
public static void main(String[] args){
int n = stdin.nextInt();
int l = stdin.nextInt();
int[] hole = new int[n];
for(int i = 0; i < n; i++){
hole[i] = stdin.nextInt();
}
System.out.println(solution(hole, l));
}
static int solution(int[] hole, int l) {
Arrays.sort(hole);
int a = hole[0];
cnt = hole.length;
for(int i = 1; i < hole.length; i++){
if((abs(a - hole[i]) + 1 <= l)){
cnt--;
}
else {
a = hole[i];
}
}
return cnt;
}
}
index 0 ~ index N - 3
까지만 확인을 해주면 된다.index 0 ~ index N - 3
에서 행렬a와 행렬b의 값이 다르면 해당 인덱스로부터 3x3 만큼 reverse 한다.public class Main {
static int n;
static int m;
static int cnt = 0;
static int[][] a;
static int[][] b;
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
n = stdin.nextInt();
m = stdin.nextInt();
a = new int[n][m];
b = new int[n][m];
for (int i = 0; i < n; i++) {
String str = stdin.next();
for (int j = 0; j < m; j++) {
a[i][j] = str.charAt(j) - '0';
}
}
for (int i = 0; i < n; i++) {
String str = stdin.next();
for (int j = 0; j < m; j++) {
b[i][j] = str.charAt(j) - '0';
}
}
System.out.println(solution());
}
static void reverse(int x, int y) {
for (int i = x; i < x + 3; i++) {
for (int j = y; j < y + 3; j++) {
if (a[i][j] == 1) {
a[i][j] = 0;
}
else {
a[i][j] = 1;
}
}
}
}
static int solution(){
for (int i = 0; i < n - 2; i++) {
for (int j = 0; j < m - 2; j++) {
if (a[i][j] != b[i][j]) {
reverse(i, j);
cnt++;
}
}
}
for (int i = 0 ; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] != b[i][j]) {
return -1;
}
}
}
return cnt;
}
}
Arraylist, collection 공부하기
2^N-1번째
이므로 index 1024부터는 -1을 출력int
타입으로는 표현할 수 없기 때문에 long
타입 사용solution(5, 1, list)
가 실행된다면 list에 5, 50, 51, 510, 52, 520, 521, 5210, 53, 530, 531, 5310, 532, 5320, 5321, 53210 ...... 이 재귀적으로 계속 추가된다list
를 리턴한다list
에 존재하는 요소들은 정렬되지 않는 상태이므로 정렬시킨다.public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
ArrayList<Integer> list = new ArrayList<>();
for(int num = 0; num < 10; num++) {
solution(num, 1, list);
}
Collections.sort(list);
if(T > 1023){
System.out.println(-1);
}
else {
System.out.println(list.get(T - 1));
}
}
static ArrayList solution(long num, int digit, ArrayList list){
if(digit > 10){
return list;
}
list.add(num);
for(int i = 0; i < 10; i++){
if(num % 10 > i){
solution(num * 10 + i, digit + 1, list);
}
}
return list;
}
}
(820)
8이 존재하지 않을 수 있다.830
이 나올 수 있으므로 stoppublic class Main {
static int cnt = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
String x = st.nextToken();
String y = st.nextToken();
System.out.println(solution(x, y));
}
static int solution(String x, String y){
if(x.length() == y.length()) {
for (int i = 0; i < x.length(); i++) {
if(x.charAt(i) == y.charAt(i)){
if(x.charAt(i) == '8'){
cnt++;
}
}
else
break;
}
}
return cnt;
}
}
public class Main {
static ArrayList<Integer> box = new ArrayList<Integer>();
static ArrayList<Integer> ship = new ArrayList<Integer>();
static int cnt = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int shipcnt = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < shipcnt; i++) {
ship.add(Integer.parseInt(st.nextToken()));
}
int boxcnt = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < boxcnt; i++) {
box.add(Integer.parseInt(st.nextToken()));
}
System.out.println(solution(shipcnt));
}
static int solution(int s) {
Collections.sort(box);
Collections.sort(ship);
Collections.reverse(box);
Collections.reverse(ship);
if (box.get(0) > ship.get(0)) {
return -1;
}
while (!box.isEmpty()) {
int ship_idx = 0;
int box_idx = 0;
while (ship_idx < s) {
if (ship.get(ship_idx) >= box.get(box_idx)) {
box.remove(box_idx);
ship_idx++;
}
else if (ship.get(ship_idx) < box.get(box_idx)) {
box_idx++;
}
if(box_idx == box.size()){
break;
}
}
cnt++;
}
return cnt;
}
}
public class Main {
static ArrayList<point> arr;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
public static void main(String[] args) throws IOException {
int t = Integer.parseInt(br.readLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(br.readLine());
arr = new ArrayList<point>();
for (int j = 0; j < n; j++) {
st = new StringTokenizer(br.readLine(), " ");
arr.add(new point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
}
System.out.println(solution(n));
}
}
static int solution(int n){
Collections.sort(arr, new Comparator<point>() {
@Override
public int compare(point o1, point o2) {
return o1.x > o2.x ? 1 : -1;
}
});
int cnt = arr.size();
int min = arr.get(0).y;
for(int i = 1; i < n; i++){
if(min < arr.get(i).y){
cnt--;
}
else
min = arr.get(i).y;
}
return cnt;
}
}
class point{
int x;
int y;
public point(int x, int y){
this.x = x;
this.y = y;
}
}
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static ArrayList<Time> arr = new ArrayList<Time>();
static int time = 0;
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(br.readLine());
for(int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine(), " ");
arr.add(new Time(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
}
System.out.println(solution(n));
}
static int solution(int n){
Collections.sort(arr, new Comparator<Time>() {
@Override
public int compare(Time o1, Time o2) {
return o1.si > o2.si ? 1 : -1;
}
});
boolean flag = false;
for(int i = 0; i <= arr.get(0).si; i++){
int idx = 0;
int sum = i;
while(idx < n) {
sum += arr.get(idx).ti;
if (sum > arr.get(idx).si){
if(i == 0){
return -1;
}
flag = true;
break;
}
idx++;
}
if(flag == true){
break;
}
time = i;
}
return time;
}
}
class Time{
int ti;
int si;
public Time(int ti, int si){
this.ti = ti;
this.si = si;
}
}
ArrayList<Integer>[] tree = new ArrayList[개수];
- tree[i].add(parent);
- tree[parent].add(i);
public class boj_1068 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static ArrayList<Integer>[] tree;
static boolean[] isvisited;
static int leaf_cnt = 0;
static int root = 0;
static int delete_idx = 0;
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(br.readLine());
isvisited = new boolean[n];
tree = new ArrayList[n];
for(int i = 0; i < n; i++) {
tree[i] = new ArrayList<Integer>();
isvisited[i] = false;
}
st = new StringTokenizer(br.readLine(), " ");
for(int i = 0; i < n; i++){
int parent = Integer.parseInt(st.nextToken());
if(parent != -1) {
tree[i].add(parent);
tree[parent].add(i);
}
else root = i;
}
delete_idx = Integer.parseInt(br.readLine());
if(delete_idx == root){
System.out.println(0);
}
else{
DFS(root);
System.out.println(leaf_cnt);
}
}
static void DFS(int node){
isvisited[node] = true;
boolean haschild = false;
for(int i = 0; i < tree[node].size(); i++){
if(tree[node].get(i) != delete_idx && !isvisited[tree[node].get(i)]) {
haschild = true;
DFS(tree[node].get(i));
}
}
if(!haschild){
leaf_cnt++;
}
}
}