/*문제
1) 두 개의 배열이 있다. 한 배열의 값을 다른 배열로 복사하라!
2) 배열 데이터 예:
int[] list1 = {1, 2, 4, 7, 11, 9};
int[] list2 = new int[list1.length];
3) 출력 예:
list1: 1, 2, 4, 7, 11, 9
list2: 1, 2, 4, 7, 11, 9

 

 

class Test {   

 public static void main(String[] args) throws Exception {

   int[] list1 = {1, 2, 4, 7, 11, 9};
   int[] list2 = new int[list1.length];

   for (int i = 0; i < list1.length; i++) {
     list2[i] = list1[i];
   }

   System.out.print("list1: ");
   for (int i = 0; i < list1.length; i++) {
     System.out.printf("%s%d", (i == 0) ? "" : ", ", list1[i]);
   }

  }
}

Posted by 안낭우훗

/*문제: 13 [절대 값 Math.abs()]
  1) 배열에서 이웃 값과의 차이(절대값)를 모두 더하여 출력하라!
  >java Test
  2) 배열 데이터 예:
  {1, 2, 4, 7, 11, 9}
  3) 출력 예:
  합계: 34
  절대값 합계: 12
  Math.abs(값) => 절대값

 

class Test {

 public static void main(String[] args) throws Exception {

   int[] lists = {1, 2, 4, 7, 11, 9};
   int sum = 0;
   for (int list : lists) {
     sum += list;
   }
   System.out.printf("합계: %d \n", sum);

   sum = 0;
   for (int i = 0; i < lists.length - 1; i++) {
     sum += Math.abs(lists[i] - lists[i + 1]);
   }
   System.out.printf("절대값 합계: %d \n", sum);
  }
}

Posted by 안낭우훗

/*문제: 12 [이중배열 [][] ]
  다섯 명의 국영수 점수를 계산하여 총점과 평균을 구하라
  각 학생의 총점과 평균은 배열에 저장하라.
  double[][] scores = {
  {60.5, 78.9, 95, 0, 0},
  {90, 85, 99, 0, 0},
  {80, 74, 98, 0, 0},
  {70, 82, 97, 0, 0},
  {60, 88, 96, 0, 0}
  };
  예) java Test
  출력 예:
  1: 총점 234.400000점, 평균 78.133333점
  2: 총점 274.000000점, 평균 91.333333점
  3: 총점 252.000000점, 평균 84.000000점
  4: 총점 249.000000점, 평균 83.000000점
  5: 총점 244.000000점, 평균 81.333333점
  전체 평균: 83.560000점

 

 

class Test{

 public static void main(String[] args) throws Exception {

   double allAverSum = 0.0;
   double[][] scores = {
     {60.5, 78.9, 95, 0, 0},
     {90, 85, 99, 0, 0},
     {80, 74, 98, 0, 0},
     {70, 82, 97, 0, 0},
     {60, 88, 96, 0, 0}
   };

   for (int row = 0; row < scores.length; row++) {
     for (int col = 0; col < 3; col++) {
       scores[row][3] += scores[row][col];
     }
     scores[row][4] = scores[row][3] / 3;
     allAverSum += scores[row][4];
   }

   for (int row = 0; row < scores.length; row++) {
     System.out.printf("%d: 총점 %f점, 평균 %f점 \n",
      row + 1, scores[row][3], scores[row][4]);
   }
   System.out.printf("전체 평균: %f점\n", allAverSum / scores.length);
  }
}

Posted by 안낭우훗

블로그 이미지
좋은싸이트 공유, 재해석 , 공부 정리, 틀린거 알려 주세요~
안낭우훗

태그목록

공지사항

Yesterday
Today
Total

달력

 « |  » 2025.7
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

최근에 올라온 글

최근에 달린 댓글

글 보관함