2022.02.05 네이버 블로그에 작성했던 것
#방법1 Array 사용 x (백준 오류)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int max = -99999, min = 99999;
int num = sc.nextInt();
int arr[] = new int[num];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= max) {
max = arr[i];
}
if (arr[i] <= min) {
min = arr[i];
}
}
System.out.println(min + " " + max);
}
}
#방법 2 Array 사용
import java.util.Arrays;
import java.util.Scanner;
public class 최소최대 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr= new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
sc.close();
Arrays.sort(arr);
System.out.println(arr[0]+" "+arr[n-1]);
}
}
package 백준배열;
import java.util.*;
public class 최댓값 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int[] arr= new int[9];
int max=0;
int max_index=0;
for(int i=0; i<9;i++) {
arr[i]=sc.nextInt();
}
for(int i=0; i<arr.length;i++) {
if(arr[i]>max) {
max=arr[i];
max_index=i;
}
}
System.out.println(max);
System.out.println(max_index+1);
}
}
#방법 1
import java.util.*;
public class 숫자의개수 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a, b, c= 0;
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
int x=a*b*c;
String str=Integer.toString(x);
for(int i=0;i<10;i++) {
int count=0;
for(int j=0;j<str.length();j++) {
if((str.charAt(j)-'0')==i) { //charAt()을 쓰는 경우 -'0' or -'48' 해야함 안그럼 아스키코드..
count++;
}
}
System.out.println(count);
}
}
}
#방법 2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int val = Integer.parseInt(br.readLine())*Integer.parseInt(br.readLine())*Integer.parseInt(br.readLine());
int[] arr = new int[10];
while(val!=0) {
arr[val%10]++;
val/=10;
}
for(int result : arr) {
System.out.println(result);
}
}
}
package 백준배열;
import java.util.Scanner;
public class 나머지 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc= new Scanner(System.in);
int[] arr= new int[10];
int count=0;
for(int i=0;i<arr.length;i++) {
arr[i]=sc.nextInt()%42;
}
for(int i=0;i<arr.length;i++) {
int tmp=0;
for(int j=i+1;j<arr.length;j++) {
if(arr[i]==arr[j])
{tmp++;}
}
if(tmp==0) {
count++;
}
}
System.out.println(count);
}
}
package 백준배열;
import java.util.*;
public class 평균 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr= new int[n];
int max=0;
int sum =0;
double avg;
for(int i=0;i<arr.length;i++) {
arr[i]=sc.nextInt();
if(arr[i]>max) {
max=arr[i];
}
sum+=arr[i];
}
avg=100.0*sum/max/n;
System.out.println(avg);
}
}
import java.util.Scanner;
public class OX퀴즈 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String[] arr = new String[sc.nextInt()];
for(int i=0;i<arr.length;i++){
arr[i]=sc.next();//string을 배열에 그냥 단순히 저장하면 된다..
}
for(int i=0;i<arr.length;i++) {
int cnt=0;
int sum=0;
for(int j=0; j<arr[i].length();j++) { //arr[i].length라 할 수 있었군,,
if(arr[i].charAt(j)=='O') {
cnt++; //cnt++후 반복되는 게 발견되면 누적돼서 계산된다
}
else {
cnt=0; //초기화해주는 작업이 중요
}
sum+=cnt;
}
System.out.println(sum);
}
}
}
import java.util.Scanner;
public class 평균은넘겠지 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int[] arr;
int testcase = sc.nextInt();
double rate=0;
for(int j=0;j<testcase;j++) {
int N=sc.nextInt(); //학생 수
arr=new int[N];
double sum=0; // 성적 누적 합 변수
//성적 입력부분
for(int i=0;i<N;i++){
int val = sc.nextInt();
arr[i]=val;
sum+=val;
}
double mean = (sum/N);
double count = 0; //평균 넘는 학생 수 변수
//평균 넘는 학생 비율 찾기
for(int k=0;k<N;k++) {
if(arr[k]>mean) {
count++;
}
}
System.out.printf("%.3f%%\n",(count/N)*100);
//%문구 입력 위해 %%씀
//소수점 셋째자리까지 출력하기 위해서는 printf()를 써서 %.3f으로 출력해야함
}
}
}