백준
백준 1032 명령 프롬프트(Java)
Park DJ
2023. 1. 15. 02:21
https://www.acmicpc.net/problem/1032
1032번: 명령 프롬프트
첫째 줄에 파일 이름의 개수 N이 주어진다. 둘째 줄부터 N개의 줄에는 파일 이름이 주어진다. N은 50보다 작거나 같은 자연수이고 파일 이름의 길이는 모두 같고 길이는 최대 50이다. 파일이름은
www.acmicpc.net
해석 및 팁
먼저 단어의 개수를 입력받은후에 입력받은 문자열을 반복문을 통해 char배열에 저장합니다. 그 다음에 입력받은 단어랑 비교하면서 다른경우에 "?"를 출력해주면 됩니다.
Java 코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine()); //nextint를 사용하면 에러발생
String str = sc.nextLine();
char[] a = new char[str.length()];
for(int i = 0; i < str.length(); i++) {
a[i] = str.charAt(i); //입력받은 문자를 char 배열에 저장
}
for(int i = 0; i < n - 1; i++) {
String b = sc.nextLine();
for(int j = 0; j < a.length; j++) {
if(a[j] != (b.charAt(j))) a[j] = '?'; //하나씩 비교하면서 다른경우 '?' 변경
}
}
for(int i = 0; i < a.length; i++) System.out.print(a[i]);
}
}