SlideShare une entreprise Scribd logo
1  sur  7
Télécharger pour lire hors ligne
ANSWER 10-11                                                        Computer Programming using Java           1   2        Computer Programming using Java                                                        ANSWER 10-11


     CHAPTER                                        อาเรย์สองมิติ                                                     โจทย์ข้อที 4 [ระดับปานกลาง]
 ANS-10
 ANS-                                          (Two Dimensional Arrays)                                               public class RainStatistic {
                                                                                                                             public static double[][] getTable() {
                                                                                                                               double rain[][] = new double[12][];
โจทย์ข้อที 1 [ระดับง่ าย]                                                                                                      for (int i = 0; i < 12; i++) {
1) int a[][] = {{0,0,0},{0,0,0}};                                                                                                if (i == 1) {
                                                                                                                                   rain[i] = new double[28];
                                                                                                                                   // rain[i] = new double[29];
2)    int b[][] = {{1,1},{1,1},{1,1},{1,1},{1,1}};                                                                               } else if (i == 3 || i == 5 || i == 8 || i == 10) {
                                                                                                                                   rain[i] = new double[30];
3)    String s[][] = {{"Java"},{"Java"},{"Java"}};
                                                                                                                                 } else {
                                                                                                                                   rain[i] = new double[31];
                                                                                                                                 }
4)    String t[][] = {{"Java","Java","Java"}};                                                                                 }
                                                                                                                               return rain;
5)    double em[][] = {{}};
                                                                                                                             }

                                                                                                                           public static void main(String[] args) {

โจทย์ข้อที 2 [ระดับง่ าย]                                                                                                      double x[][] = getTable();

      ข้ อที 1 อาเรย์   m[][]             ข้ อที 2 อาเรย์   n[][]            ข้ อที 3 อาเรย์   p[][]                    } //End of main
                                                                                                                      } //End of class
       1     2          3   4             1                                   1     2          3
       2     3          4   5             2      3                            2     3
                                                                                                                      โจทย์ ข้อที 5 [ระดับง่ าย]
       3     4          5   6             3      4          5                 3     4          5   6
                                                                                                                      1) int x = num[51][49];
       4     5          6   7             4      5          6   7             4
                                                                              5     6                                 2)      char c = code[9][60];

                                                                                                                      3)      int var1 = bank[0][1];
โจทย์ข้อที 3 [ระดับง่ าย]
1) boolean matrix[][]                                                                                                 4)      double var2 = bank[1][bank[1].length - 1];
                                    = new boolean[5][8];

2)                                                                                                                    5)      code[6][4] = x;
      String chess[][] = new String[8][8];

3)                                                                                                                    6)      sName[0][9] = s1;
      int tranMatrix[][] = new int[4][9];

4)                                                                                                                    7)      sName[1][2] = s2;
      double data[][] = new double[300][3];

5)    n[7] = new int[4];
                                                                                                                      8)     a[a.length - 1][a[a.length - 1].length - 1] = y;

6)    n[4] = new int[x * x + 1];




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 10-11                                                      Computer Programming using Java             3   4     Computer Programming using Java                                                           ANSWER 10-11


โจทย์ข้อที 6 [ระดับง่ าย]                                                                                             โจทย์ข้อที 8 [ระดับปานกลาง - ระดับยาก]
public class The2DArray {                                                                                             import java.util.Scanner;
                                                                                                                      public class DataExperiment {
     public static void setArray(int n[][]) {
       for (int i = 0; i < n.length; i++) {                                                                             public static double max(double t[][]) {
         for (int j = 0; j < n[i].length; j++) {                                                                             double maxData = t[0][0];
           n[i][j] = (i + 1) * (j + 1);                                                                                      for (int i = 0; i < t.length; i++) {
         }                                                                                                                     for (int j = 0; j < t[i].length; j++) {
       }                                                                                                                         if (t[i][j] > maxData) maxData = t[i][j];
     }                                                                                                                         }
                                                                                                                             }
   public static void main(String[] args) {                                                                                  return maxData;

       int x[][] = new int[5][6];                                                                                       } //End of max(…)
       setArray(x);
                                                                                                                         public static double min(double t[][]) {
  } //End of main                                                                                                            double minData = t[0][0];
} //End of class                                                                                                             for (int i = 0; i < t.length; i++) {
                                                                                                                               for (int j = 0; j < t[i].length; j++) {
                                                                                                                                 if (t[i][j] < minData) minData = t[i][j];
โจทย์ข้อที 7 [ระดับปานกลาง]                                                                                                    }
public class EqualityOf2DArray {                                                                                             }
                                                                                                                             return minData;
     public static boolean isArrayEquals(int x[][], int y[][]) {
       if (x.length == y.length) {
         for (int i = 0; i < x.length; i++) {                                                                           } //End of min(…)
           if (x[i].length != y[i].length) return false;
         }
                                                                                                                        public static double middleRange(double t[][]) {
         for (int i = 0; i < x.length; i++) {
           for (int j = 0; j < x[i].length; j++) {                                                                           return (max(t) + min(t)) / 2.0;
             if (x[i][j] != y[i][j]) return false;
           }                                                                                                            } //End of avgRange(…)
         }
         return true;                                                                                                   public static double meanOfMonth(double t[][], int m) {
       } else {                                                                                                              double sum = 0.0;
         return false;                                                                                                       int i = (m + 3) % 12;
       }                                                                                                                     for (int j = 0; j < t[i].length; j++) {
     }                                                                                                                         sum += t[i][j];
                                                                                                                             }
   public static void main(String[] args) {                                                                                  return sum / t[i].length;
       int m[][] = {{1},{2,3},{3,4,5,6},{4,5,6,7}};
                                                                                                                        } //End of meanOfMonth(…)
       int n[][] = {{1},{2,3},{3,4,5},{4,5,6,7}};
       boolean ch = isArrayEquals(m, n);                                                                                public static double dataOfDay(double t[][], int d, int m) {
                                                                                                                             return t[(m + 3) % 12][d - 1];
  } //End of main
} //End of class
                                                                                                                        } //End of dataOfDay(…)




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 10-11                                                      Computer Programming using Java             5   6     Computer Programming using Java                                                           ANSWER 10-11


  public static void main(String[] args) {
                                                                                                                         //[ระดับยาก] เขียนเมท็อด transposeMatrix(…)
       Scanner kb = new Scanner(System.in);
       double test[][] = new double[12][];                                                                                 public static double[][]transposeMatrix(double x[][]) {
       for (int i = 0; i < test.length; i++) {                                                                               double at[][] = new double[x[0].length][x.length];
         if (i == 5) {                                                                                                       for (int i = 0; i < x.length; i++) {
                                                                                                                               for (int j = 0; j < x[i].length; j++) {
           test[i] = new double[28];
         } else if (i == 0 || i == 2 || i == 7 || i == 9) {                                                                      at[j][i] = x[i][j];
                                                                                                                               }
           test[i] = new double[30];
                                                                                                                             }
         } else {
           test[i] = new double[31];                                                                                         return at;
                                                                                                                           }
         for (int j = 0; j < test[i].length; j++) {
           System.out.print((j + 1) + "/" + ((i + 9) % 12) + " : ");
           test[i][j] = kb.nextDouble();
         }//End of for j                                                                                                 //[ระดับยาก] เขียนเมท็อด mulMatrix(…)
       }//End of for i                                                                                                     public static double[][] mulMatrix(double x[][], double y[][]) {
                                                                                                                             double mul[][] = new double[x.length][y[0].length];
       //========= Method Call =========                                                                                     for (int i = 0; i < mul.length; i++) {
       System.out.println(max(test));                                                                                          for (int j = 0; j < mul[i].length; j++) {
       System.out.println(min(test));                                                                                            for (int k = 0; k < y.length; k++) {
       System.out.println(middleRange(test));                                                                                      mul[i][j] += x[i][k] * y[k][j];
       System.out.println(meanOfMonth(test, 10));                                                                                }
       System.out.println(dataOfDay(test, 13, 6));                                                                             }
                                                                                                                             }
  } //End of main(…)                                                                                                         return mul;
} //End of Class                                                                                                           }


โจทย์ข้อที 9 [ระดับง่ าย – ระดับยาก]                                                                                     //[ระดับง่ าย] เขียนเมท็อด printMatrix(…)
import java.util.Scanner;
public class MatrixOperation {                                                                                             public static void printMatrix(double x[][]) {
                                                                                                                             for (int i = 0; i < x.length; i++) {
   //[ระดับปานกลาง] เขียนเมท็อด addMatrix(…)                                                                                   for (int j = 0; j < x[i].length; j++) {
     public static double[][] addMatrix(double x[][], double y[][]) {                                                            System.out.print(x[i][j] + "t");
       double add[][] = new double[x.length][x[0].length];                                                                     }
       for (int i = 0; i < x.length; i++) {                                                                                    System.out.println();
         for (int j = 0; j < x[i].length; j++) {                                                                             }
           add[i][j] = x[i][j] + y[i][j];                                                                                  }
         }
       }
       return add;                                                                                                       //[ระดับง่ าย] เขียนเมท็อด main(…)
     }                                                                                                                   public static void main(String[] args) {
                                                                                                                               //Define and       creat matrix
   //[ระดับปานกลาง] เขียนเมท็อด mulScalarMatrix(…)                                                                             double a[][]       = {{1,3,5,9},{2,6,4,3}};
                                                                                                                               double b[][]       = {{3,6},{4,8},{1,0}};
     public static double[][] mulScalarMatrix(double x[][], double n) {                                                        double c[][]       = {{0,2,4},{5,3,1},{7,11,2},{6,6,9}};
       double mulS[][] = new double[x.length][x[0].length];
       for (int i = 0; i < x.length; i++) {                                                                                    //Calculate matrix m
         for (int j = 0; j < x[i].length; j++) {                                                                               double t1[][] = transposeMatrix(mulScalarMatrix(a, 2.0));
           mulS[i][j] = n * x[i][j];                                                                                           double t2[][] = transposeMatrix(mulScalarMatrix(b, 0.25));
         }                                                                                                                     double t3[][] = mulMatrix(t1, t2);
       }                                                                                                                       double t4[][] = mulScalarMatrix(c, -1.0);
       return mulS;                                                                                                            double m[][] = addMatrix(t3, t4);
     }




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 10-11                                                      Computer Programming using Java             7   8     Computer Programming using Java                                                                   ANSWER 10-11


         //Print matrix
         System.out.println("-----             Matrix a -----");
                                                                                                                        CHAPTER                                               การเวียนเกิด
         printMatrix(a);                                                                                               ANS-11
                                                                                                                       ANS-                                                    (Recursion)
         System.out.println("-----             Matrix b -----");
         printMatrix(b);
         System.out.println("-----             Matrix c -----");                                                      โจทย์ข้อที 1 [ระดับง่ าย]
         printMatrix(c);                                                                                                  เรี ยก           เรี ยก         เรี ยก          เรี ยก           เรี ยก               เรี ยก
         System.out.println("-----             Matrix m -----");
         printMatrix(m);
                                                                                                                         f(5)             f(4)           f(3)            f(2)             f(1)                 f(0)

   } //End of main
} //End of class                                                                                                         ผลลัพธ์         ผลลัพธ์        ผลลัพธ์         ผลลัพธ์           ผลลัพธ์              ผลลัพธ์
                                                                                                                                                                                                                              เวียนเข้ า
                                                                                                                                                                                                                 0
โจทย์ข้อที 10 [ระดับยาก]                                                                                                                                                                   1     +              1 +
 public static int[] splitRowOfArray(int a[][], int row) {                                                                                                                2   +            2     +              2 +
   int sp[];                                                                                                                                              3 +             3   +            3     +              3 +
   if (row > 0 && row <= a.length) {                                                                                                      4 +             4 +             4   +            4     +              4 +
     sp = new int[a[row - 1].length];                                                                                     5 +             5 +             5 +             5   +            5     +              5 +
     for (int i = 0; i < sp.length; i++) {
       sp[i] = a[row - 1][i];                                                                                            รอบที 1         รอบที 2         รอบที 3        รอบที 4           รอบที 5              รอบที 6
     }
   } else {                                                                                                               คืนค่ า         คืนค่ า        คืนค่ า         คืนค่ า           คืนค่ า              คืนค่ า
     sp = new int[0];
   }                                                                                                                       15              10              6               3                 1                    0
   return sp;
 }
                                                                                                                         ผลลัพธ์         ผลลัพธ์        ผลลัพธ์         ผลลัพธ์           ผลลัพธ์              ผลลัพธ์

โจทย์ข้อที 11 [ระดับยาก]                                                                                                                                                                                         0
 public static int[] splitColumnOfArray(int a[][], int col) {                                                                                                                              1     +              1 +
   int sp[];                                                                                                                                                              2   +            2     +              2 +           เวียนออก
   if (col > 0 && col <= a[0].length) {                                                                                                                   3 +             3   +            3     +              3 +
     sp = new int[a.length];                                                                                                              4 +             4 +             4   +            4     +              4 +
     for (int i = 0; i < sp.length; i++) {                                                                                5 +             5 +             5 +             5   +            5     +              5 +
       sp[i] = a[i][col - 1];
     }                                                                                                                   รอบที 1         รอบที 2         รอบที 3        รอบที 4           รอบที 5              รอบที 6
   } else {
     sp = new int[0];
   }
   return sp;                                                                                                         โจทย์ข้อที 2 [ระดับง่ าย]          โจทย์ ข้อที 3 [ระดับง่ าย]              โจทย์ ข้อที 4 [ระดับง่ าย]
 }
                                                                                                                         การเรี ยกใช้        คําตอบ         การเรี ยกใช้         คําตอบ             การเรี ยกใช้         คําตอบ
                                                                                                                       mul(4, 3)                  12       gcd(28, 16)             4                 expo(4)                 16
                                                                                                                       mul(5, 7)                  35       gcd(9, 14)              1                 expo(7)                128
                                                                                                                       mul(20, 10)             200         gcd(75, 30)             15                expo(11)              2048




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 10-11                                                                 Computer Programming using Java                    9   10     Computer Programming using Java                                                           ANSWER 10-11


จงแสดงรายละเอียดของ expo(0), expo(1), expo(2) และ expo(3)                                                                                โจทย์ข้อที 7 [ระดับง่ าย]
    expo(0)                           expo(1)                                             expo(2)                                                    n                           กรณีฐาน         f n) = 1
                                                                                                                                                                                                  (                                 n = 1
                                                                                                                                                           1
       1                                   2                                                 4
                                                                                                                                           f n) =
                                                                                                                                            (       ∑ i3       n ≥ 1
                                                                                                                                                    i =1
                        expo(0)                      expo(0)                                                                                                                     กรณีเวียนเกิด   f n) = f n − 1) + 1 / n 3
                                                                                                                                                                                                  (      (                          n > 1
                                                                        expo(1)                         expo(1)
                             1                        1
                                                                             2                             2                              public static double f(int n) {
                                                                                                                                            if (n <= 1) {
                                                                expo(0)              expo(0) expo(0)                  expo(0)                 return 1.0;
                                                                                                                                            } else {
                                                                    1                 1             1                 1
                                                                                                                                              return f(n - 1) + 1.0 / (n * n * n);
                                                                                                                                            }
                                                      expo(3)                                                                             }

                                                          8
                            expo(2)                                                       expo(2)                                        โจทย์ข้อที 8 [ระดับง่ าย]
                              4                                                             4                                             public static int f(int n) {
                                                                                                                                            if (n <= 0) {
           expo(1)                         expo(1)                      expo(1)                         expo(1)                               return 0;
                                                                                                                                            } else {
               2                               2                             2                            2                                   return f(n - 1) +
 expo(0)               expo(0) expo(0)               expo(0) expo(0)                 expo(0) expo(0)              expo(0)                     (int) Math.pow(n, n) + (2 * n);
                                                                                                                                            }
       1                1              1              1             1                 1             1                 1                   }


                                                                                                                                         โจทย์ข้อที 9 [ระดับง่ าย]
โจทย์ข้อที 5 [ระดับง่ าย]                                                                                                                 public static double g(int n) {
   การเรี ยกใช้ และคําตอบ                                                                                                                   if (n <= 0) {
                                                                                                                                              return 0.0;
 printX(4)             printX(7)                                                                                                            } else if (n == 1) {
 1234                  1234567                                                                                                                return 1.0;
 123                   123456                                                                                                               } else {
                                                                                                                                              return g(n - 1) * (1.0 + 1.0 / n);
 12                    12345
                                                                                                                                            }
 1                     1234                                                                                                               }
                       123
                       12
                       1                                                                                                                 โจทย์ ข้อที 10 [ระดับง่ าย]
                                                                                                                                          public static int fac(int n) {
                                                                                                                                            if (n < 2) {
โจทย์ข้อที 6 [ระดับปานกลาง]                                                                                                                   return 1;
                                                                พารามิเตอร์      n                                                          } else {
  พารามิเตอร์      m                                                                                                                          return fac(n - 1) * n;
                                  0                  1                  2                       3                 4                         }
                                                                                                                                          }
           0                      1                  2                  3                     4                   5
           1                      2                  3                  4                     5                   6
           2                      3                  5                  7                     9                11
           3                      5                13                   29                   61                125


© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)                              © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 10-11                                                                Computer Programming using Java      11   12        Computer Programming using Java                                                        ANSWER 10-11


โจทย์ข้อที 11 [ระดับง่ าย]                                                                                                 โจทย์ข้อที 15 [ระดับยาก]
 public static int fibo(int n) {                                                                                           1) public static double       g(int n, int m) {
   if (n < 2) {
                                                                                                                                      if (n <= 1) {
     return n;
                                                                                                                                        return 1;
   } else {
                                                                                                                                      } else {
     return fibo(n - 1) + fibo(n - 2);
                                                                                                                                        if (n % 2 == 0) {
   }
                                                                                                                                          return g(n – 1, m) - Math.pow(n, m);
 }
                                                                                                                                        } else {
                                                                                                                                          return g(n – 1, m) + Math.pow(n, m);
                                                                                                                                        }
โจทย์ข้อที 12 [ระดับง่ าย]                                        fibo(5)
                                                                                                                                      }
                                     fibo(4)                           5                     fibo(3)                              }

                                         3                                                     2                           2)     public static double h(int n) {
                     fibo(3)                           fibo(2)                     fibo(2)             fibo(1)
                                                                                                                                    if (n <= 1) {
                       2                                 1                            1                  1                            return 1;
                                                                                                                                    } else {
           fibo(2)             fibo(1)       fibo(1)             fibo(0)    fibo(1)          fibo(0)
                                                                                                                                      if (n % 2 == 0) {
              1                  1              1                  0           1               0                                        return h(n - 1) + 1.0 / n;
                                                                                                                                      } else {
  fibo(1)             fibo(0)                                                                                                           return h(n - 1) - 1.0 / n;
                                                                                                                                      }
       1               0                                                                                                            }
                                                                                                                                  }

โจทย์ข้อที 13 [ระดับปานกลาง]
 public static double pow(double a, int b) {                                                                               โจทย์ข้อที 16 [ระดับปานกลาง]
   if (b < 0) {
     return pow(a, b + 1) * 1.0 / a;                                                                                        public static int search(int a[], int k) {
   } else if (b == 0) {                                                                                                       return search(a, k, 0);
     return 1.0;                                                                                                            }
   } else {
     return pow(a, b - 1) * a;                                                                                              public static int search(int a[], int k, int i) {
   }                                                                                                                          if (i < a.length) {
 }                                                                                                                              if (a[i] == k) {
                                                                                                                                  return i;
                                                                                                                                } else {
โจทย์ข้อที 14 [ระดับยาก]                                                                                                        }
                                                                                                                                  return search(a, k, i + 1);

 public static double f(int n) {                                                                                              } else {
   if (n <= 1) {                                                                                                                return -1;
     return 1.0;                                                                                                              }
   } else {                                                                                                                 }
     return 1.0 + (n / f(n - 1));
   }
 }




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)                © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 10-11                                                      Computer Programming using Java             13   14     Computer Programming using Java                                                           ANSWER 10-11


โจทย์ข้อที 17 [ระดับปานกลาง]                                                                                             public static void formulaAtoB(int a, int b, int i) {
public class OverHundredNumber {                                                                                           if (a <= b) {
  public static void main(String[] args) {                                                                                   if (i <= 12) {
    int d[] = { 99, 101, 13, 78, 200, 534, 47, 1234, 736 };                                                                    System.out.println(a + " x " + i + " = " + (a * i));
    System.out.println(overHundred(d));                                                                                        formulaAtoB(a, b, i + 1);
  }                                                                                                                          } else {
                                                                                                                               System.out.println("-------------");
    public static int overHundred(int d[]) {                                                                                   formulaAtoB(a + 1, b);
      return overHundred(d, 0, 0);                                                                                           }
    }                                                                                                                      }//End of if (a <= b)
                                                                                                                         }
    public static int overHundred(int d[], int i, int count) {
      if (i < d.length) {
        if (d[i] > 100) {                                                                                               โจทย์ข้อที 20 [ระดับยาก]
          return overHundred(d, i + 1, count + 1);
        } else {                                                                                                         public static int[] addArray(int a[], int b[]) {
          return overHundred(d, i + 1, count);                                                                             int sum[] = new int[a.length];
        }                                                                                                                  return addArray(a, b, sum, 0);
      } else {                                                                                                           }
        return count;
      }                                                                                                                  public static int[] addArray(int a[], int b[], int sum[], int i) {
    }                                                                                                                      if (i < sum.length) {
                                                                                                                             sum[i] = a[i] + b[i];
                                                                                                                             return addArray(a, b, sum, i + 1);
} //End of class                                                                                                           } else {
                                                                                                                             return sum;
                                                                                                                           }
โจทย์ข้อที 18 [ระดับปานกลาง]                                                                                             }
public class TheString {
  public static void main(String[] args) {
    System.out.println(revStr("Computer"));
  }                                                                                                                     โจทย์ข้อที 21 [ระดับยาก]
    public static String revStr(String s) {                                                                              public static boolean isMatrixEquals(int a[][], int b[][]) {
      return revStr(s, "", s.length() - 1);                                                                                if (a.length == b.length && a[0].length == b[0].length) {
    }                                                                                                                        return isMatrixEquals(a, b, 0, 0);
                                                                                                                           } else {
    public static String revStr(String s, String rs, int i) {                                                                return false;
      if (i >= 0) {                                                                                                        }
        rs += s.substring(i, i + 1);                                                                                     }
        return revStr(s, rs, i - 1);
      } else {                                                                                                           public static boolean isMatrixEquals(int a[][], int b[][], int i,
        return rs;                                                                                                         int j) {
      }                                                                                                                    if (i < a.length) {
    }                                                                                                                        if (j < a[i].length) {
                                                                                                                               if (a[i][j] != b[i][j]) {
                                                                                                                                 return false;
} //End of class                                                                                                               } else {
                                                                                                                                 return isMatrixEquals(a, b, i, j + 1);
                                                                                                                               }
                                                                                                                             } else {
โจทย์ข้อที 19 [ระดับยาก]                                                                                                       return isMatrixEquals(a, b, i + 1, 0);
                                                                                                                             }
 public static void formulaAtoB(int a, int b) {                                                                            } else {
   formulaAtoB(a, b, 1);                                                                                                     return true;
 }                                                                                                                         }
                                                                                                                         }


© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)             © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)

Contenu connexe

Tendances

Java-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and ObjectsJava-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and ObjectsWongyos Keardsri
 
Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)Wongyos Keardsri
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 RecursionsWongyos Keardsri
 
Java-Answer Chapter 05-06 (For Print)
Java-Answer Chapter 05-06 (For Print)Java-Answer Chapter 05-06 (For Print)
Java-Answer Chapter 05-06 (For Print)Wongyos Keardsri
 
Java-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsJava-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsWongyos Keardsri
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsWongyos Keardsri
 
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชันค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชันsawed kodnara
 
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญาสูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญาKanomwan Jeab
 
อนุพันธ์
อนุพันธ์อนุพันธ์
อนุพันธ์krurutsamee
 
การอินทีเกรต
การอินทีเกรตการอินทีเกรต
การอินทีเกรตANNRockART
 
เฉลยอินทิเกรต
เฉลยอินทิเกรตเฉลยอินทิเกรต
เฉลยอินทิเกรตkrurutsamee
 
09 multi arrays
09 multi arrays09 multi arrays
09 multi arraysa-num Sara
 
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจแบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจชัชชญา ช่างเจริญ
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysWongyos Keardsri
 
Chapter 4 ลิมิตของฟังก์ชัน
Chapter 4 ลิมิตของฟังก์ชันChapter 4 ลิมิตของฟังก์ชัน
Chapter 4 ลิมิตของฟังก์ชันPumPui Oranuch
 

Tendances (20)

Java-Answer Chapter 07
Java-Answer Chapter 07Java-Answer Chapter 07
Java-Answer Chapter 07
 
Java-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and ObjectsJava-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and Objects
 
Java-Answer Chapter 05-06
Java-Answer Chapter 05-06Java-Answer Chapter 05-06
Java-Answer Chapter 05-06
 
Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 Recursions
 
Java-Answer Chapter 05-06 (For Print)
Java-Answer Chapter 05-06 (For Print)Java-Answer Chapter 05-06 (For Print)
Java-Answer Chapter 05-06 (For Print)
 
Java-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsJava-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and Objects
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 Algorithms
 
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชันค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
 
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญาสูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
 
ปริพันธ์
ปริพันธ์ปริพันธ์
ปริพันธ์
 
อนุพันธ์
อนุพันธ์อนุพันธ์
อนุพันธ์
 
การอินทีเกรต
การอินทีเกรตการอินทีเกรต
การอินทีเกรต
 
662305 08
662305 08662305 08
662305 08
 
เฉลยอินทิเกรต
เฉลยอินทิเกรตเฉลยอินทิเกรต
เฉลยอินทิเกรต
 
09 multi arrays
09 multi arrays09 multi arrays
09 multi arrays
 
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจแบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional Arrays
 
Chapter 4 ลิมิตของฟังก์ชัน
Chapter 4 ลิมิตของฟังก์ชันChapter 4 ลิมิตของฟังก์ชัน
Chapter 4 ลิมิตของฟังก์ชัน
 
Limit
LimitLimit
Limit
 

Plus de Wongyos Keardsri

How to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramHow to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramWongyos Keardsri
 
The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsWongyos Keardsri
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingWongyos Keardsri
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemWongyos Keardsri
 
SysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageSysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IWongyos Keardsri
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsWongyos Keardsri
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityWongyos Keardsri
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 CountingWongyos Keardsri
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsWongyos Keardsri
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIWongyos Keardsri
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IWongyos Keardsri
 
Discrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesDiscrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesWongyos Keardsri
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesWongyos Keardsri
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationWongyos Keardsri
 

Plus de Wongyos Keardsri (20)

How to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramHow to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master Program
 
The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applications
 
IP address anonymization
IP address anonymizationIP address anonymization
IP address anonymization
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script Programming
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating System
 
SysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageSysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming Language
 
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part III
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part II
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part I
 
Discrete-Chapter 10 Trees
Discrete-Chapter 10 TreesDiscrete-Chapter 10 Trees
Discrete-Chapter 10 Trees
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 Relations
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 Probability
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 Counting
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and Proofs
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part II
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part I
 
Discrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesDiscrete-Chapter 03 Matrices
Discrete-Chapter 03 Matrices
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and Sequences
 
Discrete-Chapter 01 Sets
Discrete-Chapter 01 SetsDiscrete-Chapter 01 Sets
Discrete-Chapter 01 Sets
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling Computation
 

Java-Answer Chapter 10-11 (For Print)

  • 1. ANSWER 10-11 Computer Programming using Java 1 2 Computer Programming using Java ANSWER 10-11 CHAPTER อาเรย์สองมิติ โจทย์ข้อที 4 [ระดับปานกลาง] ANS-10 ANS- (Two Dimensional Arrays) public class RainStatistic { public static double[][] getTable() { double rain[][] = new double[12][]; โจทย์ข้อที 1 [ระดับง่ าย] for (int i = 0; i < 12; i++) { 1) int a[][] = {{0,0,0},{0,0,0}}; if (i == 1) { rain[i] = new double[28]; // rain[i] = new double[29]; 2) int b[][] = {{1,1},{1,1},{1,1},{1,1},{1,1}}; } else if (i == 3 || i == 5 || i == 8 || i == 10) { rain[i] = new double[30]; 3) String s[][] = {{"Java"},{"Java"},{"Java"}}; } else { rain[i] = new double[31]; } 4) String t[][] = {{"Java","Java","Java"}}; } return rain; 5) double em[][] = {{}}; } public static void main(String[] args) { โจทย์ข้อที 2 [ระดับง่ าย] double x[][] = getTable(); ข้ อที 1 อาเรย์ m[][] ข้ อที 2 อาเรย์ n[][] ข้ อที 3 อาเรย์ p[][] } //End of main } //End of class 1 2 3 4 1 1 2 3 2 3 4 5 2 3 2 3 โจทย์ ข้อที 5 [ระดับง่ าย] 3 4 5 6 3 4 5 3 4 5 6 1) int x = num[51][49]; 4 5 6 7 4 5 6 7 4 5 6 2) char c = code[9][60]; 3) int var1 = bank[0][1]; โจทย์ข้อที 3 [ระดับง่ าย] 1) boolean matrix[][] 4) double var2 = bank[1][bank[1].length - 1]; = new boolean[5][8]; 2) 5) code[6][4] = x; String chess[][] = new String[8][8]; 3) 6) sName[0][9] = s1; int tranMatrix[][] = new int[4][9]; 4) 7) sName[1][2] = s2; double data[][] = new double[300][3]; 5) n[7] = new int[4]; 8) a[a.length - 1][a[a.length - 1].length - 1] = y; 6) n[4] = new int[x * x + 1]; © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 2. ANSWER 10-11 Computer Programming using Java 3 4 Computer Programming using Java ANSWER 10-11 โจทย์ข้อที 6 [ระดับง่ าย] โจทย์ข้อที 8 [ระดับปานกลาง - ระดับยาก] public class The2DArray { import java.util.Scanner; public class DataExperiment { public static void setArray(int n[][]) { for (int i = 0; i < n.length; i++) { public static double max(double t[][]) { for (int j = 0; j < n[i].length; j++) { double maxData = t[0][0]; n[i][j] = (i + 1) * (j + 1); for (int i = 0; i < t.length; i++) { } for (int j = 0; j < t[i].length; j++) { } if (t[i][j] > maxData) maxData = t[i][j]; } } } public static void main(String[] args) { return maxData; int x[][] = new int[5][6]; } //End of max(…) setArray(x); public static double min(double t[][]) { } //End of main double minData = t[0][0]; } //End of class for (int i = 0; i < t.length; i++) { for (int j = 0; j < t[i].length; j++) { if (t[i][j] < minData) minData = t[i][j]; โจทย์ข้อที 7 [ระดับปานกลาง] } public class EqualityOf2DArray { } return minData; public static boolean isArrayEquals(int x[][], int y[][]) { if (x.length == y.length) { for (int i = 0; i < x.length; i++) { } //End of min(…) if (x[i].length != y[i].length) return false; } public static double middleRange(double t[][]) { for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { return (max(t) + min(t)) / 2.0; if (x[i][j] != y[i][j]) return false; } } //End of avgRange(…) } return true; public static double meanOfMonth(double t[][], int m) { } else { double sum = 0.0; return false; int i = (m + 3) % 12; } for (int j = 0; j < t[i].length; j++) { } sum += t[i][j]; } public static void main(String[] args) { return sum / t[i].length; int m[][] = {{1},{2,3},{3,4,5,6},{4,5,6,7}}; } //End of meanOfMonth(…) int n[][] = {{1},{2,3},{3,4,5},{4,5,6,7}}; boolean ch = isArrayEquals(m, n); public static double dataOfDay(double t[][], int d, int m) { return t[(m + 3) % 12][d - 1]; } //End of main } //End of class } //End of dataOfDay(…) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 3. ANSWER 10-11 Computer Programming using Java 5 6 Computer Programming using Java ANSWER 10-11 public static void main(String[] args) { //[ระดับยาก] เขียนเมท็อด transposeMatrix(…) Scanner kb = new Scanner(System.in); double test[][] = new double[12][]; public static double[][]transposeMatrix(double x[][]) { for (int i = 0; i < test.length; i++) { double at[][] = new double[x[0].length][x.length]; if (i == 5) { for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { test[i] = new double[28]; } else if (i == 0 || i == 2 || i == 7 || i == 9) { at[j][i] = x[i][j]; } test[i] = new double[30]; } } else { test[i] = new double[31]; return at; } for (int j = 0; j < test[i].length; j++) { System.out.print((j + 1) + "/" + ((i + 9) % 12) + " : "); test[i][j] = kb.nextDouble(); }//End of for j //[ระดับยาก] เขียนเมท็อด mulMatrix(…) }//End of for i public static double[][] mulMatrix(double x[][], double y[][]) { double mul[][] = new double[x.length][y[0].length]; //========= Method Call ========= for (int i = 0; i < mul.length; i++) { System.out.println(max(test)); for (int j = 0; j < mul[i].length; j++) { System.out.println(min(test)); for (int k = 0; k < y.length; k++) { System.out.println(middleRange(test)); mul[i][j] += x[i][k] * y[k][j]; System.out.println(meanOfMonth(test, 10)); } System.out.println(dataOfDay(test, 13, 6)); } } } //End of main(…) return mul; } //End of Class } โจทย์ข้อที 9 [ระดับง่ าย – ระดับยาก] //[ระดับง่ าย] เขียนเมท็อด printMatrix(…) import java.util.Scanner; public class MatrixOperation { public static void printMatrix(double x[][]) { for (int i = 0; i < x.length; i++) { //[ระดับปานกลาง] เขียนเมท็อด addMatrix(…) for (int j = 0; j < x[i].length; j++) { public static double[][] addMatrix(double x[][], double y[][]) { System.out.print(x[i][j] + "t"); double add[][] = new double[x.length][x[0].length]; } for (int i = 0; i < x.length; i++) { System.out.println(); for (int j = 0; j < x[i].length; j++) { } add[i][j] = x[i][j] + y[i][j]; } } } return add; //[ระดับง่ าย] เขียนเมท็อด main(…) } public static void main(String[] args) { //Define and creat matrix //[ระดับปานกลาง] เขียนเมท็อด mulScalarMatrix(…) double a[][] = {{1,3,5,9},{2,6,4,3}}; double b[][] = {{3,6},{4,8},{1,0}}; public static double[][] mulScalarMatrix(double x[][], double n) { double c[][] = {{0,2,4},{5,3,1},{7,11,2},{6,6,9}}; double mulS[][] = new double[x.length][x[0].length]; for (int i = 0; i < x.length; i++) { //Calculate matrix m for (int j = 0; j < x[i].length; j++) { double t1[][] = transposeMatrix(mulScalarMatrix(a, 2.0)); mulS[i][j] = n * x[i][j]; double t2[][] = transposeMatrix(mulScalarMatrix(b, 0.25)); } double t3[][] = mulMatrix(t1, t2); } double t4[][] = mulScalarMatrix(c, -1.0); return mulS; double m[][] = addMatrix(t3, t4); } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 4. ANSWER 10-11 Computer Programming using Java 7 8 Computer Programming using Java ANSWER 10-11 //Print matrix System.out.println("----- Matrix a -----"); CHAPTER การเวียนเกิด printMatrix(a); ANS-11 ANS- (Recursion) System.out.println("----- Matrix b -----"); printMatrix(b); System.out.println("----- Matrix c -----"); โจทย์ข้อที 1 [ระดับง่ าย] printMatrix(c); เรี ยก เรี ยก เรี ยก เรี ยก เรี ยก เรี ยก System.out.println("----- Matrix m -----"); printMatrix(m); f(5) f(4) f(3) f(2) f(1) f(0) } //End of main } //End of class ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ เวียนเข้ า 0 โจทย์ข้อที 10 [ระดับยาก] 1 + 1 + public static int[] splitRowOfArray(int a[][], int row) { 2 + 2 + 2 + int sp[]; 3 + 3 + 3 + 3 + if (row > 0 && row <= a.length) { 4 + 4 + 4 + 4 + 4 + sp = new int[a[row - 1].length]; 5 + 5 + 5 + 5 + 5 + 5 + for (int i = 0; i < sp.length; i++) { sp[i] = a[row - 1][i]; รอบที 1 รอบที 2 รอบที 3 รอบที 4 รอบที 5 รอบที 6 } } else { คืนค่ า คืนค่ า คืนค่ า คืนค่ า คืนค่ า คืนค่ า sp = new int[0]; } 15 10 6 3 1 0 return sp; } ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ โจทย์ข้อที 11 [ระดับยาก] 0 public static int[] splitColumnOfArray(int a[][], int col) { 1 + 1 + int sp[]; 2 + 2 + 2 + เวียนออก if (col > 0 && col <= a[0].length) { 3 + 3 + 3 + 3 + sp = new int[a.length]; 4 + 4 + 4 + 4 + 4 + for (int i = 0; i < sp.length; i++) { 5 + 5 + 5 + 5 + 5 + 5 + sp[i] = a[i][col - 1]; } รอบที 1 รอบที 2 รอบที 3 รอบที 4 รอบที 5 รอบที 6 } else { sp = new int[0]; } return sp; โจทย์ข้อที 2 [ระดับง่ าย] โจทย์ ข้อที 3 [ระดับง่ าย] โจทย์ ข้อที 4 [ระดับง่ าย] } การเรี ยกใช้ คําตอบ การเรี ยกใช้ คําตอบ การเรี ยกใช้ คําตอบ mul(4, 3) 12 gcd(28, 16) 4 expo(4) 16 mul(5, 7) 35 gcd(9, 14) 1 expo(7) 128 mul(20, 10) 200 gcd(75, 30) 15 expo(11) 2048 © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 5. ANSWER 10-11 Computer Programming using Java 9 10 Computer Programming using Java ANSWER 10-11 จงแสดงรายละเอียดของ expo(0), expo(1), expo(2) และ expo(3) โจทย์ข้อที 7 [ระดับง่ าย] expo(0) expo(1) expo(2) n กรณีฐาน f n) = 1 ( n = 1 1 1 2 4 f n) = ( ∑ i3 n ≥ 1 i =1 expo(0) expo(0) กรณีเวียนเกิด f n) = f n − 1) + 1 / n 3 ( ( n > 1 expo(1) expo(1) 1 1 2 2 public static double f(int n) { if (n <= 1) { expo(0) expo(0) expo(0) expo(0) return 1.0; } else { 1 1 1 1 return f(n - 1) + 1.0 / (n * n * n); } expo(3) } 8 expo(2) expo(2) โจทย์ข้อที 8 [ระดับง่ าย] 4 4 public static int f(int n) { if (n <= 0) { expo(1) expo(1) expo(1) expo(1) return 0; } else { 2 2 2 2 return f(n - 1) + expo(0) expo(0) expo(0) expo(0) expo(0) expo(0) expo(0) expo(0) (int) Math.pow(n, n) + (2 * n); } 1 1 1 1 1 1 1 1 } โจทย์ข้อที 9 [ระดับง่ าย] โจทย์ข้อที 5 [ระดับง่ าย] public static double g(int n) { การเรี ยกใช้ และคําตอบ if (n <= 0) { return 0.0; printX(4) printX(7) } else if (n == 1) { 1234 1234567 return 1.0; 123 123456 } else { return g(n - 1) * (1.0 + 1.0 / n); 12 12345 } 1 1234 } 123 12 1 โจทย์ ข้อที 10 [ระดับง่ าย] public static int fac(int n) { if (n < 2) { โจทย์ข้อที 6 [ระดับปานกลาง] return 1; พารามิเตอร์ n } else { พารามิเตอร์ m return fac(n - 1) * n; 0 1 2 3 4 } } 0 1 2 3 4 5 1 2 3 4 5 6 2 3 5 7 9 11 3 5 13 29 61 125 © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 6. ANSWER 10-11 Computer Programming using Java 11 12 Computer Programming using Java ANSWER 10-11 โจทย์ข้อที 11 [ระดับง่ าย] โจทย์ข้อที 15 [ระดับยาก] public static int fibo(int n) { 1) public static double g(int n, int m) { if (n < 2) { if (n <= 1) { return n; return 1; } else { } else { return fibo(n - 1) + fibo(n - 2); if (n % 2 == 0) { } return g(n – 1, m) - Math.pow(n, m); } } else { return g(n – 1, m) + Math.pow(n, m); } โจทย์ข้อที 12 [ระดับง่ าย] fibo(5) } fibo(4) 5 fibo(3) } 3 2 2) public static double h(int n) { fibo(3) fibo(2) fibo(2) fibo(1) if (n <= 1) { 2 1 1 1 return 1; } else { fibo(2) fibo(1) fibo(1) fibo(0) fibo(1) fibo(0) if (n % 2 == 0) { 1 1 1 0 1 0 return h(n - 1) + 1.0 / n; } else { fibo(1) fibo(0) return h(n - 1) - 1.0 / n; } 1 0 } } โจทย์ข้อที 13 [ระดับปานกลาง] public static double pow(double a, int b) { โจทย์ข้อที 16 [ระดับปานกลาง] if (b < 0) { return pow(a, b + 1) * 1.0 / a; public static int search(int a[], int k) { } else if (b == 0) { return search(a, k, 0); return 1.0; } } else { return pow(a, b - 1) * a; public static int search(int a[], int k, int i) { } if (i < a.length) { } if (a[i] == k) { return i; } else { โจทย์ข้อที 14 [ระดับยาก] } return search(a, k, i + 1); public static double f(int n) { } else { if (n <= 1) { return -1; return 1.0; } } else { } return 1.0 + (n / f(n - 1)); } } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 7. ANSWER 10-11 Computer Programming using Java 13 14 Computer Programming using Java ANSWER 10-11 โจทย์ข้อที 17 [ระดับปานกลาง] public static void formulaAtoB(int a, int b, int i) { public class OverHundredNumber { if (a <= b) { public static void main(String[] args) { if (i <= 12) { int d[] = { 99, 101, 13, 78, 200, 534, 47, 1234, 736 }; System.out.println(a + " x " + i + " = " + (a * i)); System.out.println(overHundred(d)); formulaAtoB(a, b, i + 1); } } else { System.out.println("-------------"); public static int overHundred(int d[]) { formulaAtoB(a + 1, b); return overHundred(d, 0, 0); } } }//End of if (a <= b) } public static int overHundred(int d[], int i, int count) { if (i < d.length) { if (d[i] > 100) { โจทย์ข้อที 20 [ระดับยาก] return overHundred(d, i + 1, count + 1); } else { public static int[] addArray(int a[], int b[]) { return overHundred(d, i + 1, count); int sum[] = new int[a.length]; } return addArray(a, b, sum, 0); } else { } return count; } public static int[] addArray(int a[], int b[], int sum[], int i) { } if (i < sum.length) { sum[i] = a[i] + b[i]; return addArray(a, b, sum, i + 1); } //End of class } else { return sum; } โจทย์ข้อที 18 [ระดับปานกลาง] } public class TheString { public static void main(String[] args) { System.out.println(revStr("Computer")); } โจทย์ข้อที 21 [ระดับยาก] public static String revStr(String s) { public static boolean isMatrixEquals(int a[][], int b[][]) { return revStr(s, "", s.length() - 1); if (a.length == b.length && a[0].length == b[0].length) { } return isMatrixEquals(a, b, 0, 0); } else { public static String revStr(String s, String rs, int i) { return false; if (i >= 0) { } rs += s.substring(i, i + 1); } return revStr(s, rs, i - 1); } else { public static boolean isMatrixEquals(int a[][], int b[][], int i, return rs; int j) { } if (i < a.length) { } if (j < a[i].length) { if (a[i][j] != b[i][j]) { return false; } //End of class } else { return isMatrixEquals(a, b, i, j + 1); } } else { โจทย์ข้อที 19 [ระดับยาก] return isMatrixEquals(a, b, i + 1, 0); } public static void formulaAtoB(int a, int b) { } else { formulaAtoB(a, b, 1); return true; } } } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)