코드업 기초 100제

업데이트:

[1080] [기초-종합] 언제까지 더해야 할까?

[입력] 정수 1개가 입력된다.(1 ~ 12)
[출력] 1, 2, 3, 4, 5 … 를 순서대로 계속 더해 합을 만들어가다가,
입력된 정수와 같거나 커졌을 때, 마지막에 더한 정수를 출력한다.
[입력 예시] 55
[출력 예시] 10

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int num = sc.nextInt();
		
		int sum = 0;
		int last =0;
		
		for(int i=1; i<num; i++) {
			sum += i;
			last = i;
			
			if(sum >= num) {
			  System.out.print(last);
			  break;
			}
		}
	}
}




[1081] [기초-종합] 주사위를 2개 던지면?

[입력] 주사위 2개의 면의 개수 n, m이 공백을 두고 입력된다.
단, n, m은 10이하의 자연수
[출력] 나올 수 있는 주사위의 숫자를 한 세트씩 줄을 바꿔 모두 출력한다.
첫 번째 수는 n, 두 번째 수는 m으로 고정해 출력하도록 한다.
[입력 예시] 2 3
[출력 예시] 1 1
                  1 2
                  1 3
                  2 1
                  2 2
                  2 3

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		
		for(int i=1; i<=a; i++) {
			for(int j=1; j<=b; j++) {
				System.out.println(i + " " + j);
			}
		}
	}
}




[1082] [기초-종합] 16진수 구구단?

[입력] 16진수로 한 자리 수가 입력된다. 단, A ~ F 까지만 입력된다.
[출력] 입력된 16진수에 1~F까지 순서대로 곱한, 16진수 구구단을 줄을 바꿔 출력한다.
계산 결과도 16진수로 출력해야 한다.
image

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	
		String a = sc.next();
		
		int b = Integer.parseInt(a,16); //a 문자열을 16진수로 변환
		//Integer.parseInt(숫자형 문자열 ,진수값);
		for(int i=1;i<16;i++) {
			System.out.printf("%X*%X=%X\n",b, i, (b*i));
		}
		
	}
}




[1083] [기초-종합] 3 6 9 게임의 왕이 되자!

image

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int num = sc.nextInt();
		char ch ;
		
		for(int i=1; i<=num ;i++) {
			
			if(i%3==0) {
				ch = 'X';
				System.out.print(ch+" ");
			} else {
			
			System.out.print(i+ " ");
			}
		}
	}
}




[1084] [기초-종합] 빛 섞어 색 만들기

image

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int red = sc.nextInt();
        int green = sc.nextInt();
        int blue = sc.nextInt();

        BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(System.out));
        // System.out.println 함수를 이용하면 시간 초과 에러가 발생하기 때문에
        // 속도가 빠른 BufferedWriter 사용.
        try { // bf 출력에서 예외 발생함.
            int sum = 0;
            for (int r = 0; r < red; r++) {
                for (int g = 0; g < green; g++) {
                    for (int b = 0; b < blue; b++) {

                        String a = (r + " " + g + " " + b);

                        bf.write(a + "\n"); 
                    // 문자열 출력 시 개행을 해주지 않아서 개행문자를 넣어주거나
                    // 아래 줄에 bf.newLine(); 함수를 작성해 주어야 함.
                    //bf.flush(); // 버퍼에 남아있는 데이터를 없앤다..
                    // 시간초과 에러가 발생해서,,일단 주석 처리 해두었음.
                        sum++;
                    }
                }
            }
            bf.write(sum+""); //sum은 int형이기 때문에 ""를 붙여 String 자료형으로 바꾸어줌.
            bf.close(); //스트림을 닫아 준다.
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}




[1085] [기초-종합] 소리 파일 저장용량 계산하기

image


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long h = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long s = sc.nextLong();
		
		double total = h*b*c*s; //비트단위
		
		//풀이법1)
		double bt =0;
		double kb =0;
		double mb =0;
		
		bt= (total/8);
		kb= (bt/1024);
		mb= (kb/1024);
		System.out.printf("%.1f MB",mb);
		
		
		
		//Math.pow(밑, 지수) 특정 제곱값의 제곱을 구하는 함수.
		//Math.pow(5,2) == 5의 2승
		//Math.pow(2,4) == 2의 4승
	/*	
		//풀이법2)
		double result = ((total/8)/Math.pow(2, 10))/Math.pow(2,10);
		// total/8 = Byte로 바꿈 ->  Byte/ (2의10승 = 1024)=KB로 바꿈 -> KB/1024 ->MB로 바꿈
		System.out.printf("%.1f MB",result);
	*/
	}
}




[1086] [기초-종합] 그림 파일 저장용량 계산하기

image

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	
		long w = sc.nextLong();
		long h = sc.nextLong();
		long b = sc.nextLong();
		
		double total = w*h*b;
		double result = 0;
		
		result = ((total/8)/Math.pow(2, 10))/Math.pow(2, 10);
		
		System.out.printf("%.2f MB",result);
		
	}
}




[1087] [기초-종합] 여기까지! 이제 그만~

image

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	
		int num = sc.nextInt();
		
		int sum=0;
		for(int i=1; i<=num ; i++) {
			sum += i;
			if(sum>=num) {
				break;
			}
		}
		System.out.print(sum);
	
	}
}




[1088] [기초-종합] 3의 배수는 통과?

image

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	
		int num = sc.nextInt();
		
		for(int i=1; i<=num; i++) {
			if(i%3==0) {
				continue;
			}
			System.out.print(i + " ");
		}
	}
}




댓글남기기