package Day036;
import java.util.Scanner;
public class Ex00501배열테스트3문제 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int data[] = {5,5,6,8,4,6,};
// 값을 입력받고 삭제
// 조건) 만약에 같은값이 있으면 전부 삭제
// 예) 2 ==> 5,5,6,8,6
// 예) 5 ==> 6,8,2,6
int cnt = 0;
System.out.println("number");
int num = sc.nextInt();
for(int i=0; i<data.length; i++ ){
if( data[i]!=num){
cnt++;
}
}
int[] tmp = data;
data=new int[cnt];
int x = 0;
for(int i=0; i<tmp.length; i++){
if(tmp[i]!=num) {
data[x] = tmp[i];
x++;
}
}
for(int i=0; i<cnt; i++){
System.out.print(data[i]+" ");
}
System.out.println();
}
}
package Day036;
import java.awt.*;
import java.util.Scanner;
public class Ex00501배열테스트4문제 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int game[] = {2, 0, 0, 2, 8, 0, 0, 4, 0};
// 1)left , 2)right
//문제)번호를 입력받고 각방향으로 숫자를모으시요.
//조건) 만약에 같은숫자2개가 붙었을경우는 서로 더해진다.
// 아래 2단계출력
//예) left
// 1단계 : {2,2,8,4,0,0,0,0,0};
// 2단계 : {4,8,4,0,0,0,0,0,0}; 2가 2개붙었으니 4가된다.
//힌트)
//우선 0이 아닌 숫자를 밀어낸다. {2,2,8,4,0,0,0,0,0}
//같은 숫자끼리 더한다. {4,0,8,4,0,0,0,0,0}
//다시 0이 아닌 숫자를 밀어낸다. {4,8,4,0,0,0,0,0,0}
while(true) {
for (int i = 0; i < game.length; i++) {
System.out.print(game[i] + " ");
}
System.out.println();
System.out.println("1)left , 2)right");
int ch = sc.nextInt();
int x = 0;
int y = 0;
if (ch == 1) {
for(int j=0; j<game.length; j++) {
int cnt = 0;
for (int i = y; i < game.length; i++) {
if (game[i] == 0 || game[i] == game[y]) {
if (game[i] == game[y]) {
cnt++;
}
} else if (game[i] != game[y]) {
break;
}
}
if (cnt > 1) {
for (int i = y; i < y+cnt; i++) {
if (y != i) {
game[i] = 0;
}
}
}
game[y] = game[y] * cnt;
y++;
}
for (int i = 0; i < game.length; i++) {
if (game[i] != 0) {
int tmp = game[x];
game[x] = game[i];
game[i] = tmp;
x++;
}
}
} else if (ch == 2) {
y= game.length-1;
for(int j=game.length-1; j>=0; j--) {
int cnt = 0;
for (int i = y; i >=0; i--){
if (game[i] == 0 || game[i] == game[y]) {
if (game[i] == game[y]) {
cnt++;
}
} else if (game[i] != game[y]) {
break;
}
}
if (cnt > 1) {
for (int i = y; i > y-cnt; i--) {
if (y != i) {
game[i] = 0;
}
}
}
game[y] = game[y] * cnt;
y--;
}
x = game.length;
for (int i = game.length - 1; i >= 0; i--) {
if (game[i] != 0) {
int tmp = game[x - 1];
game[x - 1] = game[i];
game[i] = tmp;
x--;
}
}
} else {
System.out.println("wrong");
}
}
}
}