import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] words = new String[5];
for(int i = 0; i < 5; i++){
words[i] = sc.nextLine();
}
StringBuilder result = new StringBuilder();
int maxLength = 0;
for(String word : words){
if(maxLength < word.length()) maxLength = word.length();
}
for (int i = 0; i < maxLength; i++) {
for (int j = 0; j < 5; j++) {
if (i < words[j].length()) {
result.append(words[j].charAt(i));
}
}
}
System.out.println(result);
sc.close();
}
}
코드 설명
길이가 5인 1차원 배열 words를 선언한다
for문을 5번 돌면서 words[i]에 각 word를 입력해준다
words중 가장 길이가 긴 것을 기준으로 세로읽기를 해야하므로, maxLength를 구해준다
이중 for문으로 maxLength만큼, 5번 반복한다
i가 words[j].length()보다 작을 경우, result(StringBuilder)에 words[j].length()를 append해준다
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int A[] = new int[N];
for (int i = 0; i < N ; i++) {
A[i] = sc.nextInt();
}
long sum = 0;
long max = 0;
for (int i = 0; i < N ; i++) {
if(max < A[i]) max = A[i];
sum += A[i];
}
System.out.println(sum*100.0/max/N);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int cnt = 0;
for (int i = 0; i < n; i++) {
if(groupWord(sc.next())) cnt++;
}
System.out.println(cnt);
}
private static boolean groupWord(String word) {
boolean[] visited = new boolean[26];
for (int i = 0; i < word.length(); i++) {
if(!visited[word.charAt(i) - 'a']){ // 처음 나오는 알파벳
visited[word.charAt(i) - 'a'] = true;
}
else if(word.charAt(i-1)!=word.charAt(i)){ // 이전에 나온적 있음 + 전 글자와 연속되지 않음
return false;
}
}
return true;
}
}
코드 설명
입력 받을 단어 개수를 n수에 입력받는다.
groupWord함수에 새로 입력받는 단어를 인자로 받아, true라면 cnt값을 1씩 증가시킨다 (n번 반복)