[ 문제 - 배열 ]
문1)알파벳을 아래와 같이 한줄에 5개씩 출력하시오
ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
Z
문2)아래와 같이 출력하시오
####
###
##
#
문3)다음식의 결과값을 구하시오
1 2 3 4 5 99
─ - ─ + ─ - ─ + ─ ... ── = 0.688172
2 3 4 5 6 100
문4)아래와 같이 계산해서 출력하시오
1+....+10=55
11+....+20=155
21+....+30=255
81+....+90=
91+....+100=955
문5)어느 달팽이는 낮에는 3cm올라가고, 밤에는 2.5cm 내려온다고 할때
달팽이가 13cm의 나무 꼭대기에 올라가려면 며칠이 걸리는지 구하시오 (결과:21일/무한반복)
[ 풀이 ]
문1)
int count=0;
for(char ch='A'; ch<='Z'; ch++) {
count++;
System.out.print(ch);
//System.out.print(count);
if(count%5==0) {
System.out.println();
}//if end
}//for end
문2)
for(int a=1; a<=4; a++) {
for(int b=1; b<=4; b++) {
if(a<=b) {
System.out.print("#");
}else {
System.out.print(" ");
}//if end
}//for end
System.out.println();
}//for end
문3)
double hap=0.0;
boolean flag=false;
for(int a=1; a<=99; a++) {
if(flag) {
hap = hap - (a/(double)(a+1));
flag=false;
}else {
hap = hap + (a/(double)(a+1));
flag=true;
}//if end
}//for end
System.out.println(hap);//0.688172
문4)
int sum=0;
for(int a=10; a<=100; a+=10) {
for(int b=a-9; b<=a; b++) {
sum+=b;;
}
System.out.printf("%d + ... + %d = %d\n", (a-9), a, +sum);
sum=0;
}//for end
문5)
int day=0; //결과값
double snail=0.0; //달팽이
while(true) {
day++;
snail=snail+3.0;
if(snail>=13.0) {
break;
}else {
snail=snail-2.5;
}
}//while end
System.out.println(day+"일");
[ 문제2 - 배열 & 풀이 ]
char[] ch= {'I','t','W','i','l','l'};
int size=ch.length; //6
//문1) 대, 소문자의 갯수를 각각 구하시오
//->대문자 : 2개
//->소문자 : 4개
//대문자
int upper=0;//대문자의 갯수
int lower=0;//소문자의 갯수
for(int i=0; i<size; i++) {
if(ch[i] >= 'A' && ch[i] <= 'Z') {upper++;}
if(ch[i] >= 'a' && ch[i] <= 'z') {lower++;}
}
System.out.println("대문자 갯수 : %d\n" + upper);
System.out.println("소문자 갯수: %d\n" + lower);
//문2)대소문자를 서로 바꿔서 출력하시오
//->iTwILL
for(int i=0; i<size; i++) {
if(ch[i] >= 'A' && ch[i] <= 'Z') {
System.out.printf("%c", ch[i]+32);
}//if end
if(ch[i] >= 'a' && ch[i] <= 'z') {
System.out.printf("%c", ch[i]-32);
}//if end
}//for end
System.out.println();
//문3)모음의 갯수를 구하시오 AEIOUaeiou
//->모음의 갯수 : 2개
int mo=0; //모음의 갯수
for(int i=1; i<size; i++) {
char c=ch[i];
if(c<='A' && c<='Z') {//대문자인지?
c=(char)(c+32);//소문자로 변경
}//if end
switch(c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': mo++;
}
}//for end
System.out.printf("\n모음의 갯수 : %d\n", mo);
/////////////////////////////////
//문4)각 행의 모음의 갯수를 구하시오
//->str[0]행 : 2개
//->str[1]행 : 1개
//->str[2]행 : 2개
char [][] str= {
{'Y','e','a','r'}
,{'M','o','n','t','h'}
,{'D','a','t','e'}
};
int row=str.length; //3
int count=0;
for(int r=0; r<row; r++) {
int col=str[r].length;
for(int c=0; c<col; c++) {
char w=str[r][c];
if(w<='A' && w<='Z') {//대문자인지?
w=(char)(w+32);//소문자로 변경
}//if end
switch(w) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': count++;
}//switch end
}//for end
System.out.printf("\nstr[%d]행 모음의 갯수 : %d개", r, count);
count=0; //각 행마다 모음의 갯수를 구하기 때문에 초기화해야 함
}//for end
///////////////////////////////////
//문5)대각선 방향의 각 요소의 합을 구하시오
//대각선 ↘ 방향의 합 (4+9+7) num[0][0]+num[1][1]+num[2][2]
//대각선 ↙ 방향의 합 (2+9+6) num[0][2]+num[1][1]+num[2][0]
int[][] num= {
{4, 3, 2}
,{5, 9, 1}
,{6, 8, 7}
};
int hap1=0; //대각선↘
int hap2=0; //대각선↙
for(int i=0; i<=2; i++) {
hap1=hap1+num[i][i];
hap2=hap2+num[i][2-i];
}//for end
System.out.printf("\n대각선 ↘ 방향의 합 : %d", hap1);
System.out.printf("\n대각선 ↙ 방향의 합 : %d", hap2);
'Backend > JAVA' 카테고리의 다른 글
[JAVA] Sort 정렬 (0) | 2023.03.14 |
---|---|
[JAVA] Array (0) | 2023.03.14 |
[JAVA] for, while, do~while, break, continue (0) | 2023.03.14 |
[JAVA] quiz : if (0) | 2023.03.13 |
[JAVA] Math (0) | 2023.03.13 |