[Java]CodeUp1061~1080

박진우·2022년 9월 21일
0

CodeUp기초 100제

목록 보기
4/6

1061

입력된 정수 두 개를 비트단위로 or 연산한 후 그 결과를 정수로 출력해보자.
비트단위(bitwise) 연산자 |(or, vertical bar, 버티컬바)를 사용하면 된다.
입력 예시
3 5
출력 예시
7

import java.util.Scanner;

public class C1061 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println(sc.nextInt()|sc.nextInt());
	}
}

1062

입력된 정수 두 개를 비트단위로 xor 연산한 후 그 결과를 정수로 출력해보자.
비트단위(bitwise) 연산자 ^(xor, circumflex/caret, 서컴플렉스/카릿)를 사용하면 된다.
입력 예시
3 5
출력 예시
6

import java.util.Scanner;

public class C1062 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println(sc.nextInt()^sc.nextInt());
	}
}

1063

입력된 두 정수 a, b 중 큰 값을 출력하는 프로그램을 작성해보자.
단, 조건문을 사용하지 않고 3항 연산자 ? 를 사용한다.
입력 예시
123 456
출력 예시
456

import java.util.Scanner;

public class C1063 {
	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?b:a);
	}
}

1064

입력된 세 정수 a, b, c 중 가장 작은 값을 출력하는 프로그램을 작성해보자.
단, 조건문을 사용하지 않고 3항 연산자 ? 를 사용한다.
입력 예시
3 -1 5
출력 예시
-1

import java.util.Scanner;

public class C1064 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int temp = a<b?a:b;
		temp = temp<c?temp:c;
		System.out.println(temp);
	}
}

1065

세 정수 a, b, c가 입력되었을 때, 짝수만 출력해보자.
입력 예시
1 2 4
출력 예시
2
4

import java.util.Scanner;
public class C1065 {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		int[] num = new int[3];
		for(int i = 0;i<num.length;i++) {
			num[i] = sc.nextInt();
			if(num[i]%2 == 0) {
				System.out.println(num[i]);
			}
		}
	}
}

1066

세 정수 a, b, c가 입력되었을 때, 짝(even)/홀(odd)을 출력해보자.
입력 예시
1 2 8
출력 예시
odd
even
even

import java.util.Scanner;
public class C1066 {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		int[] num = new int[3];
		for(int i = 0;i<num.length;i++) {
			num[i] = sc.nextInt();
			if(num[i]%2 == 0) 
				System.out.println("even");
				else 
				System.out.println("odd");
			
		}
	}
}

1067

정수 1개가 입력되었을 때, 음(minus)/양(plus)과 짝(even)/홀(odd)을 출력해보자.
입력 예시
-2147483648
출력 예시
minus
even

import java.util.Scanner;

public class C1063 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		if(num<0) {
			System.out.println("minus");
		}else {
			System.out.println("plus");
		}
		if(num%2==0) {
			System.out.println("even");
		}else {
			System.out.println("odd");
		}
	}
}

1068

점수(정수, 0 ~ 100)를 입력받아 평가를 출력해보자.
입력 예시
73
출력 예시
B

import java.util.Scanner;

public class C1068 {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int result = sc.nextInt();
		if(result<=100 && result >=90) {
			System.out.println("A");
		}else if(result<=89 && result >=70) {
			System.out.println("B");
		}else if(result<=69 && result >=40) {
			System.out.println("C");
		}
		else if(result<=39 && result >=0) {
			System.out.println("D");
		}
		
		
	}
}

1069

평가를 문자(A, B, C, D, ...)로 입력받아 내용을 다르게 출력해보자.
입력 예시
A
출력 예시
best!!!

import java.util.Scanner;

public class C1069 {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		char C = sc.next().charAt(0);
		switch (C) {
		case 'A':
			System.out.println("best!!!");
			break;

		case 'B':
			System.out.println("good!!");
			break;
		case 'C':
			System.out.println("run!");
			break;
		case 'D':
			System.out.println("slowly~");
			break;
		default : 
			System.out.println("what?");
			break;
		}
		
		
	}
}

1070

월이 입력될 때 계절 이름이 출력되도록 해보자.
입력 예시
12
출력 예시
winter

import java.util.Scanner;

public class C1070 {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int C = sc.nextInt();
		switch (C) {
		case 12:
			
		case 1:
			
		case 2:
			System.out.println("winter");
			break;
		case 3:
			
		case 4:
			
		case 5:
			System.out.println("spring");
			break;
		case 6:
			
		case 7:
			
		case 8:
			System.out.println("summer");
			break;
		case 9:
			
		case 10:
			
		case 11:
			System.out.println("fall");
			break;
		}
		
		
	}
}

1071

정수가 순서대로 입력된다.
-2147483648 ~ +2147483647, 단 개수는 알 수 없다.
0이 아니면 입력된 정수를 출력하고, 0이 입력되면 출력을 중단해보자.
while( ), for( ), do~while( ) 등의 반복문을 사용할 수 없다.
입력 예시
7 4 2 3 0 1 5 6 9 10 8
출력 예시
7
4
2
3

import java.util.Scanner;

public class C1071 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(true) {
			long num = sc.nextLong();
			if(num == 0) {
				break;
			}
			System.out.println(num);
		}
	}
}

1072

n개의 정수가 순서대로 입력된다.
-2147483648 ~ +2147483647, 단 n의 최대 개수는 알 수 없다.
n개의 입력된 정수를 순서대로 출력해보자.
입력 예시
5
1 2 3 4 5
출력 예시
1
2
3
4
5

import java.util.Scanner;

public class C1072 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		for(int i = 1;i<=N;i++) {
			System.out.println(sc.nextLong());
		}
	}
}

1073

정수가 순서대로 입력된다.
-2147483648 ~ +2147483647, 단 개수는 알 수 없다.
0이 아니면 입력된 정수를 출력하고, 0이 입력되면 출력을 중단해보자.\
입력 예시
7 4 2 3 0 1 5 6 9 10 8
출력 예시
7
4
2
3

public class C1073 {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		while(true) {
			int num = sc.nextInt();
			if(num == 0) {
				break;
			}else {
				System.out.println(num);
			}
			
		}
	}
}

1074

정수(1 ~ 100) 1개가 입력되었을 때 카운트다운을 출력해보자.
입력 예시
5
출력 예시
5
4
3
2
1

import java.util.Scanner;

public class C1074 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		for(int i = 0;i<N;i++) {
			System.out.println(N-i);
		}
	}
}

1075

정수(1 ~ 100) 1개가 입력되었을 때 카운트다운을 출력해보자.
입력 예시
5
출력 예시
4
3
2
1
0

import java.util.Scanner;

public class C1063 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		for(int i = 1;i<=N;i++) {
			System.out.println(N-i);
		}
	}
}

1076

영문자(a ~ z) 1개가 입력되었을 때 그 문자까지의 알파벳을 순서대로 출력해보자.
입력 예시
f
출력 예시
a b c d e f


import java.util.Scanner;

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

	    char value = sc.nextLine().charAt(0);

	    for(char i='a'; i<=value; i++) {
	      System.out.printf("%c ", i);
	    }
	}
}

1077

정수(0 ~ 100) 1개를 입력받아 0부터 그 수까지 순서대로 출력해보자.
입력 예시
4
출력 예시
0
1
2
3
4

import java.util.Scanner;

public class C1077 {
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int num = sc.nextInt();
	    for(int i = 0;i<=num;i++) {
	    	System.out.println(i);
	    }
	    
	}
}

1078

정수(1 ~ 100) 1개를 입력받아 1부터 그 수까지 짝수의 합을 구해보자.

import java.util.Scanner;

public class C1078 {
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int num = sc.nextInt();
	    int sum = 0;
	    for(int i = 0;i<=num;i++) {
	    	if(i%2 == 0) {
	    		sum = sum+i;
	    	}
	    }
	    System.out.println(sum);
	    
	}
}

1079

'q'가 입력될 때까지 입력한 문자를 계속 출력하는 프로그램을 작성해보자.
입력 예시
x b k d l q g a c
출력 예시
x
b
k
d
l
q

import java.util.Scanner;

public class C1079 {
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	  while(true) {
		  char C = sc.next().charAt(0);
		  if(C == 'q') {
			  System.out.println(C);
			  break;
		  }
		  System.out.println(C);
	  }
	}
}

1080

1, 2, 3 ... 을 계속 더해 나갈 때,
그 합이 입력한 정수(0 ~ 1000)보다 같거나 작을 때까지
계속 더하는 프로그램을 작성해보자.
입력 예시
55
출력 예시
10

import java.util.Scanner;

public class C1063 {
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int N = sc.nextInt();
	    int sum = 0;
	    int i = 0;
	    while(true) { 
	    	sum = sum+i;
	    	if(sum>=N)break;
	    	i++;
	    }
	    System.out.println(i);
	}
}
profile
개발자를 꿈꾸는 사람입니다

0개의 댓글