7일

권준석·2022년 9월 2일
0

1
int[] point= {20,32,12,34,24,23}
String[] name={"kim","lee","part","chol","choo","ryu"}
가장 높은 점수를 획득한 사람의 점수와 이름을 출력하시오

	int[] point = { 20, 32, 12, 34, 24, 23 };
	String[] name = { "kim", "lee", "part", "chol", "choo", "ryu" };
	int max = 0;
	int idx = 0;
	for (int i = 0; i < point.length; i++) {
		if (max < point[i]) {
			max = point[i];
			if (max == point[i]) {
				idx = i;
			}
		}
	}
	System.out.println("최고 점수 : " + max + "/ 이름 : " + name[idx]);

2
위 문제에서
점수가 높으면 1등이다
1등부터 3등까지의 점수와 이름을 출력하시오.

	int[] point = { 20, 32, 12, 34, 24, 23 };
	String[] name = { "kim", "lee", "part", "chol", "choo", "ryu" };
	int max = 0;
	int idx = 0;
	for (int j = 0; j < 3; j++) {
		for (int i = 0; i < point.length; i++) {
			if (max < point[i]) {
				max = point[i];
				if (max == point[i]) {
					idx = i;
					point[i] = -1;
				}
			}
		}
		System.out.println("최고 점수 : " + max + "/ 이름 : " + name[idx]);
		max = 0;

	}

3
int[] a = {10,0,0,30,45,0,0,60,0,0,0,0,56,0,0,0,45};
배열에서 0은 빈 땅이다. 이곳에 건물을 지으려고 한다.
0이 연속된 만큼을 땅의 크기라고 가정하자.
예를 들어 0이 3개 연속이라면 땅 크기는 3이다
땅의 크기가 3이상인 곳의 배열의 시작 인덱스를 출력하시오.

	int[] a = { 10, 0, 0, 30, 45, 0, 0, 60, 0, 0, 0, 0, 56, 0, 0, 0, 45 };
	int cnt = 0;
	int size = 3;
	int idx = 0;
	for (int i = 0; i < a.length; i++) {
		if (a[i] == 0) {
			cnt++;
			for (int j = i + 1; j < a.length; j++) {
				if (a[j] == 0) {
					cnt++;
				} else {
					j = 1000;
				}
			}
			if (cnt >= size) {
				idx = i + 1;
			}
			i += cnt;
			cnt = 0;
			if (idx != 0) {
				System.out.println("크기가 3이상인 시작점 :" + idx);
			}
		}
	}

4
int[] a = {10,0,0,30,45,0,0,60,0,0,0,0,56,0,0,0,45};
0은 빈땅이다. 가장큰 빈땅의 크기와 시작인덱스를 출력하시오.

	int[] a = { 10, 0, 0, 30, 45, 0, 0, 60, 0, 0, 0, 0, 56, 0, 0, 0, 45 };
	int cnt = 0;
	int size = 1;
	int idx = 0;
	int point = 0;
	for (int i = 0; i < a.length; i++) {
		if (a[i] == 0) {
			cnt++;
			for (int j = i + 1; j < a.length; j++) {
				if (a[j] == 0) {
					cnt++;
				} else {
					j = 1000;
				}
			}
			if (cnt >= size) {
				idx = i + 1;
			}
			i += cnt;
			if (point != 0) {
				if (cnt > point) {
					System.out.println("가장 큰 땅 : " + idx + " / 크기" + cnt);
				}
			}
			point = cnt;
			cnt = 0;
		}
	}

5
int[] a = {10,10,0,30,45,0,0,60,60,0,0,0,56,56,56,0,45};
숫자는 건물의 번호이고, 0은 빈땅이다.
건물의 번호가 같은 것은 건물의 크기가 1보다 크다는 것이다.
만약 10이 연속 2개 있다면 건물의 크기가 2라는 의미이다.
사이즈가 2이상인 건물의 번호와 크기를 모두 출력하시오

	int[] a = { 10, 10, 0, 30, 45, 0, 0, 60, 60, 0, 0, 0, 56, 56, 56, 0, 45 };
	int cnt = 0;
	int size = 2;
	int idx = 0;
	for (int i = 0; i < a.length; i++) {
		if (a[i] != 0) {
			cnt++;
			for (int j = i + 1; j < a.length; j++) {
				if (a[j] == a[i]) {
					cnt++;
				} else {
					j = 1000;
				}
			}
			if (cnt >= size) {
				idx = i ;
			}
			i += cnt;
			if(cnt>=size) {
				System.out.println("건물 번호 :" + a[idx]+" /크기 : "+cnt);
			}
			cnt = 0;
		}
	}
profile
ㅇㅇ

0개의 댓글