Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java
Java
  1. การเปลี่ยน public เป็น private ใน main method ของ Java

    Main Method ต้องเป็น public JVM เรียก main method จากนอก class ต้องเป็น public จึงจะเข้าถึงได้ ตัวอย่าง: public (ถูกต้อง) public class Demo { public static void main(String[] args) { System.out.println(This works!); } } ผลลัพธ์ This works! ตัวอย่าง: private (Compile OK แต่ Run Er

  2. โปรแกรม Java หาตำแหน่งของตัวคูณ n-th ใน Fibonacci Sequence

    โจทย์: หาตำแหน่ง (index) ของตัวคูณของ K ที่เป็นตัวที่ N ใน Fibonacci Sequence Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... หาตำแหน่งของ multiple of K ที่เป็นตัวที่ N คุณสมบัติ: Pisano Period / Divisibility ทุก K มี multiple ใน Fibonacci sequence (ทฤษฎี) Multiple ของ K ใน Fibonacci

  3. โปรแกรม Java นับจำนวนที่มีตัวหารคี่ในช่วง (Perfect Squares in Range)

    โจทย์: นับจำนวนใน [L, R] ที่มีตัวหารเป็นจำนวนคี่ จากบทความก่อน: จำนวนตัวหารคี่ ⟺ เป็น Perfect Square → นับ Perfect Squares ในช่วง [L, R] สูตร Count = ⌊√R⌋ - ⌊√(L-1)⌋ ตัวอย่างโค้ด R) return 0; // floor(sqrt(R)) - floor(sqrt(L-1)) int sqrtR = (int) Math.sqrt(R); int sqrtL =

  4. โปรแกรม Java หาเลข K หลักที่เล็กที่สุดที่หาร X ลงตัว

    โจทย์: Smallest K-digit Number Divisible by X หาเลขที่มี K digits อย่างน้อย (คือ ≥ 10^(K-1)) ที่หาร X ลงตัว สูตร Min K-digit = 10^(K-1) ถ้า 10^(K-1) % X == 0 → คำตอบ = 10^(K-1) มิฉะนั้น → คำตอบ = 10^(K-1) + (X - 10^(K-1) % X) ตัวอย่างโค้ด public class Demo { public static long smallestKDigitD

  5. โปรแกรม Java ตรวจสอบว่าหลักเลขทุกหลักหารตัวเลขนั้นลงตัวหรือไม่

    โจทย์: Self-Dividing Numbers เลขที่ทุกหลัก (digit) หารตัวเลขนั้นลงตัว (และไม่มีหลัก 0) ตัวอย่าง: 128 → 1|128, 2|128, 8|128 → YES 10 → มี 0 → NO 26 → 2|26 ใช่, 6|26 ไม่ใช่ → NO ตัวอย่างโค้ด 0) { int digit = num % 10; if (digit == 0 || original % digit != 0) {

  6. โปรแกรม Java ตรวจสอบจำนวนตัวหารเป็นคู่หรือคี่

    ทฤษฎี: จำนวนตัวหาร (Divisor Count) ถ้า n = p₁^e₁ × p₂^e₂ × ... × pₖ^eₖ (prime factorization) จำนวนตัวหาร: d(n) = (e₁+1)(e₂+1)...(eₖ+1) d(n) เป็นเลขคี่ ⟺ n เป็น Perfect Square เพราะ (e+1) เป็นคี่ ⟺ e เป็นเลขคู่ ⟺ ทุก exponent เป็นเลขคู่ ⟺ n เป็นกำลังสองสมบูรณ์ ตัวอย่างโค้ด (Optimal: O(1) check) pu

  7. โปรแกรม Java ตรวจสอบว่าสามารถสร้างเลขหาร 3 ลงตัวจาก Digits ใน Array ได้หรือไม่

    หลักการ: Divisibility by 3 เลขหาร 3 ลงตัว ⇔ ผลรวมของหลักเลขหาร 3 ลงตัว ไม่สนใจลำดับตัวเลข เพราะการจัดเรียงใหม่ไม่เปลี่ยนผลรวมหลักเลข ตัวอย่างโค้ด 0) { sum += n % 10; n /= 10; } } return sum % 3 == 0; } public static void main

  8. แปลง Iterator เป็น Spliterator ใน Java 8+

    Iterator vs Spliterator Iterator: Sequential traversal เท่านั้น Spliterator: สามารถ split ได้ สำหรับ parallel processing (Stream API) การแปลง: Spliterators.spliteratorUnknownSize() import java.util.*; import java.util.stream.*; public class Demo { public static Spliterator toSpliterator(It

  9. โปรแกรม Java นับ Set Bits (Bit Count / Population Count)

    Brian Kernighans Algorithm นับจำนวน bit 1 ใน binary representation ของ integer หลักการ: n & (n-1) ลบ bit 1 ตัวท้ายสุดออก ทำซ้ำจน n=0 จำนวนรอบ = จำนวน set bits ตัวอย่างโค้ด public class Demo { // Brian Kernighans Algorithm static int countSetBits(int n) { int count = 0; whil

  10. โปรแกรม Java นับศูนย์ท้ายใน Factorial (Trailing Zeros)

    Trailing Zeros ใน n! ศูนย์ท้ายมาจาก factor 10 = 2 × 5 ใน factorial มี factor 2 มากกว่า 5 เสมอ → นับจำนวน factor 5 สูตร: Z(n) = ⌊n/5⌋ + ⌊n/25⌋ + ⌊n/125⌋ + ... ตัวอย่างโค้ด = 1; i *= 5) { count += n / i; } return count; } public static void main(String[] args)

  11. โปรแกรม Java ลบข้อความเฉพาะจากไฟล์

    ลบบรรทัดที่ตรงกับ Pattern จากไฟล์ ใช้ไฟล์ชั่วคราว (temp file) อ่านไฟล์ต้นฉบับ เขียนเฉพาะบรรทัดที่ไม่ตรง pattern ลง temp แล้ว rename แทนที่ ตัวอย่างโค้ด (NIO.2 + Try-with-resources) import java.io.*; import java.nio.file.*; public class Demo { public static void deleteLines(Path inputFile, Str

  12. โปรแกรม Java หาผลรวมตัวหารน้อยที่สุด (Minimum Sum of Factors)

    โจทย์: Minimum Sum of Factors 1 ให้ผลรวมของตัวเลขเหล่านั้นน้อยที่สุด คำตอบ: ผลรวมของ Prime Factors (รวมซ้ำ) เหตุผล สำหรับ composite factor a × b = c 1): a + b ≤ c (ยกเว้น 2×2=4) → แยกเป็น prime factors ให้ผลรวมน้อยที่สุด ตัวอย่างโค้ด public class Demo { static int minSumFactors(int n) {

  13. โปรแกรม Java หา Remainder ของ Array Multiplication หาร n

    โจทย์: (a₁ × a₂ × ... × aₙ) mod m คำนวณ modulo ทีละขั้นตอนป้องกัน overflow คุณสมบัติ Modular Arithmetic (a × b) mod m = ((a mod m) × (b mod m)) mod m ตัวอย่างโค้ด public class Demo { public static int arrayProductMod(int[] arr, int m) { long result = 1; // ใช้ long ป้องกัน overflow ระ

  14. โปรแกรม Java หาผลรวมตัวหารที่เป็นเลขคู่ของจำนวน

    โจทย์: Sum of Even Factors ตัวอย่าง: 36 → factors: 1,2,3,4,6,9,12,18,36 → even factors: 2,4,6,12,18,36 → sum = 78 สูตรคณิตศาสตร์ (Prime Factorization) ถ้า n = 2^k × p1^e1 × p2^e2 × ... (k ≥ 1 เพื่อให้มี even factors) Sum of even factors = (2^1 + 2^2 + ... + 2^k) × Π(1 + p + p² + ... + p^e) = 2(2^k

  15. โปรแกรม Java หาคู่ที่ใกล้เคียงที่สุดจาก Sorted Arrays 2 อัน

    โจทย์: 2 Sorted Arrays หาคู่ (a, b) ที่ |a + b - target| น้อยที่สุด Two Pointers Technique (O(n + m)) Pointer ซ้าย (i) เริ่ม arr1[0], Pointer ขวา (j) เริ่ม arr2[m-1] target → j-- (ลดค่า) ผลรวม < target → i++ (เพิ่มค่า) ตัวอย่างโค้ด (แก้ไข bug ใน original) public class Demo { static void close

Total 1935 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:74/97  20-คอมพิวเตอร์/Page Goto:1 68 69 70 71 72 73 74 75 76 77 78 79 80