본문 바로가기

알고리즘 문제

오늘의 알고리즘 풀기(08.30)

2024.08.30(금)

 

문제 1:

팰린드롬

 

코드:

package palindrome;

public class palindrome {
	public static void main(String[] args) {
		//입력값
		String n = "12321";
		
		
		int leng = n.length();
		
//		String num1 = "";
//		String num2 = "";
//		for(int i = 0, j = leng -1; i < leng/2; i++, j--) {
//			num1 += n.charAt(i);
//			num2 += n.charAt(j);
//		}
//		
//		if(num1.equals(num2)) System.out.println("팰린드롬 번호입니다");
//		else System.out.println("팰린드롬 번호가 아닙니다.");
		
		boolean flag = true;
		for(int i = 0; i < leng/2; i++) {
			if(n.charAt(i) != n.charAt(leng - i - 1)) flag = false;
		}
		
		System.out.println(flag);
	}

}

 

출력:

true

 

문제 2:
문자열 뒤집기

 

코드:

package string_reverse;

public class reverse {
	public static void main(String[] args) {
		String n = "ADEFH";
		
		// =============[while 사용]
		
//		int i = n.length() - 1;
//		
//		String reverseStringN = "";
//		while(i >= 0) {
//			reverseStringN += n.charAt(i);
//			i--;
//		}
//		
//		System.out.println(reverseStringN);
		
		
		//=================[for 사용]
		
		
		String reverseN = "";
		for(int i = n.length() -1; i >= 0; i--) {
			reverseN += n.charAt(i);
		}
		
		System.out.println(reverseN);
		

	}

}

 

출력 :

HFEDA

 

 

문제 3:
평균보다 높은 학생의 비율

 

코드:

package avg;

public class avg_score {
	public static void main(String[] args) {
		int student = 7;
		int[] studentScore = new int[student];
		studentScore[0] = 100;
		studentScore[1] = 95;
		studentScore[2] = 90;
		studentScore[3] = 80;
		studentScore[4] = 70;
		studentScore[5] = 60;
		studentScore[6] = 50;
		
	
		double sum = 0;
		for(int i = 0; i < studentScore.length; i++) {
			sum += studentScore[i];
		}
		 
		
		double avg = sum / student;
		
		double cnt = 0;
		for(int i = 0; i < studentScore.length; i++) {
			if(studentScore[i] > avg) cnt++;
		}
		
		double stu = (cnt / student) * 100;
		
		System.out.println(stu);
	}

}

 

출력:

57.14285714285714