본문 바로가기
문제풀이/코드업

코드업 기초 100제 C++/Java (38~64, 각종 연산, 단 비트 제외)

by 혀니쌤1 2023. 12. 17.

목차

    이번 글은 코드업 100제 각종 연산에 대해 다룬다. 

    하지만, 47번 48번 등 비트 연산은 구멍이 나있는데, (풀지 않았다 ㅠㅠ) 비트연산은 속도가 빠른 계산 법이긴 하지만, 사칙연산과 달리 일상 생활에서 잘 쓰지 않기 때문에 별도 분리하려고 한다.

     

    38번~46번 : 기초 산술 연산

    #Long의 등장!

    #1038

    #include <iostream>
    
    int main() {
    	long long a, b;
    	std::cin >> a >> b;
    	std::cout << a + b;
    	return 0;
    }

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args){
    		Scanner sc = new Scanner(System.in);
    		long a = sc.nextLong();
    		long b = sc.nextLong();
    		System.out.println(a + b);
    	}
    }

     

     

    파이썬에선 쓰일 일이 없었던 롱이 산술 연산 시작부터 바로 등장해부렸다.

    long이 아닌 그냥 int를 쓰면 본문 제시 예제는 맞지만 (123으로 숫자가 작으니), 제출하면 틀렸다고 할 것이다

    본문 : 단, 입력되는 정수는 -1,073,741,824 ~ 1,073,741,824 이다.

    정수의 종류에 따른 데이터 크기는 아래와 같다.

    • short -32,768 ~ 32,767
    • int -2,147,483,648 ~ 2,147,483,647
    • long long -2,147,483,648 ~ 2,147,483,647

    정수 하나만 입력 받을 때는 데이터 누락이 없겠지만, 만약에 1073741824가 2번 들어와 합해 질 경우 2147483648가 되어 int 최대 범위인 2147483647을 벗어날 것이다. 실제로 그냥 int로 제출을 했다가 아래와 같은 오답 결과를 받았다.

     

     

    더 자세한 내용은 아래 글을 참고하자.

     

    Cpp 기초 : 자료형 - 숫자형 (정수/실수 등)

    Integer Type / 정수형 프로그래밍에서 정수(integer)는 소수점이 없는 숫자를 의미한다. 정수는 양수, 음수 또는 0일 수 있다. 3.0은 실수이지 정수가 아니다. 정수는 3으로 깔끔하게 표현되어야 하기

    mylittlenotepad.tistory.com

     

     

    그나저나, long이 되는지 안되는지 궁금해서 사용해보니 정답처리가 되었다.

    long에 대해 검색해보면 long long과 같다는 이야기도 나오지만, int처럼 2147483647 까지만 괜찮단 이야기도 있고...

    헷갈린다 ㅠㅠㅠ

     

    그나저나 자바에선 long만 존재한다.

     

     

     

    #1039

    #include <iostream>
    
    int main() {
    	long long a, b;
    	std::cin >> a >> b;
    	std::cout << a + b;
    	return 0;
    }

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args){
    		Scanner sc = new Scanner(System.in);
    		long a = sc.nextLong();
    		long b = sc.nextLong();
    		System.out.println(a + b);
    	}
    }

     

     

    #1040

    #include <iostream>
    
    int main() {
    	int a;
    	std::cin >> a;
    	std::cout << a * -1;
    	return 0;
    }

    import java.util.Scanner;
    public class Main {
    	public static void main(String[] args){
    		Scanner sc = new Scanner(System.in);
    		long a = Long.parseLong(sc.next());
    		System.out.println(a*-1);
    	}
    }

     

    #1041

    #include <iostream>
    
    int main() {
    	char c;
    	std::cin >> c;
    	std::cout << (char) ((int) c + 1);
    	return 0;
    }

     


    import java.util.Scanner;
    public class Main {
    	public static void main(String[] args){
    		Scanner sc = new Scanner(System.in);
    		char c = sc.next().charAt(0);
    		int a = (int) c + 1;
    		System.out.println((char) a);
    	}
    }

     

     

     

    #1042

    #include <iostream>
    int main() {
    	int a, b;
    	std::cin >> a >> b;
    	std::cout << a/b;
    	return 0;
    }

    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();
    		System.out.println(a/b);
    	}
    }

     

    #1043

    #include <iostream>
    int main() {
    	int a, b;
    	std::cin >> a >> b;
    	std::cout << a%b;
    	return 0;
    }

    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();
    		System.out.println(a%b);
    	}
    }

     

    #1044

    #include <iostream>
    
    int main() {
    	long a;
    	std::cin >> a;
    	std::cout << ++a;
    	return 0;
    }

     


    import java.util.Scanner;
    public class Main {
    	public static void main(String[] args){
    		Scanner sc = new Scanner(System.in);
    		long a = sc.nextLong();
    		System.out.println(a+1);
    	}
    }

     

     

    #1045

    #include <iostream>
    
    int main() {
    	long a, b;
    	std::cin >> a >> b;
    	std::cout << a+b << std::endl;
    	std::cout << a-b << std::endl;
    	std::cout << a*b << std::endl;
    	std::cout << a/b << std::endl;
    	std::cout << a%b << std::endl;
    	std::cout << std::fixed;
    	std::cout.precision(2);
    	std::cout << ((float)a)/b << std::endl;
    	return 0;
    }

     


    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            long a = sc.nextLong();
            long b = sc.nextLong();
            System.out.println(a + b);
            System.out.println(a - b);
            System.out.println(a * b);
            System.out.println(a / b);
            System.out.println(a % b);
            System.out.format("%.2f\n", (float) a / b);
        }
    
    }

     

     

    #1046

    #include <iostream>
    
    int main() {
    	long long a, b, c, d;
    	std::cin >> a >> b >> c;
    	d = a+b+c;
    	std::cout << d << std::endl;
    	std::cout << std::fixed;
    	std::cout.precision(1);
    	std::cout << d/3.0 << std::endl;
    	return 0;
    }

     


    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();
    		int c = sc.nextInt();
    		long d = a+b+c;
    		System.out.println(d);
    		System.out.format("%.1f",(float) d/3); 
    		
    	}
    }

     

     

    47번~48번 : 비트연산 제외

    49번~52번 : 기초 비교 연산

    if else block 사용

    #1049

    #include <iostream>
    int main() {
    	int a, b;
    	scanf("%d %d", &a, &b);
    	if (a > b) {
    		std::cout << 1;
    	}
    	else {
    		std::cout << 0;
    	}
    }

     

    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();
    		if (a>b) {
    			System.out.println(1);
    		}
    		else {
    			System.out.println(0);
    		}
    	}
    }

     

    #1050

    #include <iostream>
    int main() {
    	int a, b;
    	scanf("%d %d", &a, &b);
    	if (a == b) {
    		std::cout << 1;
    	}
    	else {
    		std::cout << 0;
    	}
    }
    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();
    		if (a==b) {
    			System.out.println(1);
    		}
    		else {
    			System.out.println(0);
    		}
    	}
    }

     

    if else block 미사용


    C/C++의 경우, boolean 결과를 바로 1 or 0 로 표현 가능하다. 하지만, JAVA는 안된다. 그러고보니, while true라는 statement도 C/C++에서는 간단히 while (1) 이라고 쓸 수 있지만, 자바에서는 그게 안되더라.

     

    #1051

    #include <iostream>
    int main() {
    	int a, b, c;
    	scanf("%d %d", &a, &b);
    	c = (a<=b);
    	std::cout << c;
    }

    ▽  그래서 (자바는 0을 false, 이외 정수를 true로 바로 못 알아들음) 그냥 ? 연산자를 사용해보았다.

    그런데, 사용하지 않았을 때 보다 5ms 가량 시간이 더 걸렸다.  자바의 경우, 그냥 if else block을 사용해야겠다.

    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();
            System.out.printf("%d", b >= a ? 1 : 0);
        }
    }

     

    #1052

    #include <iostream>
    int main() {
    	int a, b, c;
    	scanf("%d %d", &a, &b);
    	c = (a!=b);
    	std::cout << c;
    }
    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();
            System.out.printf("%d", a!=b ? 1 : 0);
        }
    }

     

     

    53번~58번 : 기초 논리 연산

    #1053

    #include <iostream>
    int main() {
    	int a;
    	std::cin >> a;
    	std::cout << !a;
    	return 0;
    }
    import java.util.Scanner;
    public class Main {
    	public static void main(String[] args){
    		Scanner sc = new Scanner(System.in);
    		int a = sc.nextInt();
    		if (a==1) { // if(a)는 자바에서 안됨
    			System.out.println(0);
    		}
    		else {
    			System.out.println(1);
    		}
    	}
    }

     

    #1054

    #include <iostream>
    int main() {
    	int a, b;
    	std::cin >> a >> b;
    	std::cout << (a && b);
    	return 0;
    }

     

    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();
    		if ((a==1)&&(b==1)){
    			System.out.println(1);
    		}
    		else {
    			System.out.println(0);
    		}
    	}
    }

     

    #1055

    #include <iostream>
    int main() {
    	int a, b;
    	std::cin >> a >> b;
    	std::cout << (a || b);
    	return 0;
    }

     

    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();
    		if ((a==1)||(b==1)){
    			System.out.println(1);
    		}
    		else {
    			System.out.println(0);
    		}
    	}
    }

     

    #1056

    #include <iostream>
    int main() {
    	int a, b;
    	std::cin >> a >> b;
    	std::cout << (a != b);
    	return 0;
    }
    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();
    		if (a!=b){
    			System.out.println(1);
    		}
    		else {
    			System.out.println(0);
    		}
    	}
    }

     

    #1057

    #include <iostream>
    int main() {
    	int a, b;
    	std::cin >> a >> b;
    	std::cout << (a != b);
    	return 0;
    }

     

    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();
    		if (a==b){
    			System.out.println(1);
    		}
    		else {
    			System.out.println(0);
    		}
    	}
    }

     

    #1058

    #include <iostream>
    int main() {
    	int a, b;
    ## 	std::cin >> a >> b;
    	std::cout << (!a && !b);
    	return 0;
    }

     

    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();
    		if ((a==0) && (b==0)){
    			System.out.println(1);
    		}
    		else {
    			System.out.println(0);
    		}
    	}
    }

     

    59번~62번 : 비트연산 제외

    63번~64번 : 기초 삼항 연산

     

    #1063

    #include <iostream>
    int main() {
    	int a, b;
    	std::cin >> a >> b;
    	std::cout << (a > b ? a : b);
    	return 0;
    }
    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();
    		int ans = (a > b) ? a : b;
    		System.out.println(ans);
    	}
    }

     

    #1064

    #include <iostream>
    int main() {
    	int a, b, c;
    	std::cin >> a >> b >> c;
    	std::cout << ((a < b ? a : b) < c ? (a < b ? a : b) : c);
    	return 0;
    }
    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();
    		int c = sc.nextInt();
    		System.out.println(((a < b ? a : b) < c ? (a < b ? a : b) : c));
    	}
    }

     

    ※ (a>b)? ((b>c)? c:b) : ((a>c)? c:a) 도 가능하다. (조합해서 나오는 것이라 이외에도 중복 답이 많을 것이다)