找到 21 篇文章,關於 Java.Util 包

Java程式:以AM-PM格式格式化時間

Shriansh Kumar
更新於 2024年8月1日 10:59:17

1K+ 次瀏覽

時間格式化是將日期和時間值轉換為具有特定格式的人類可讀字串的過程。生成的字串描述瞭如何表示日期/時間值(例如平面檔案或人類可讀輸出)。在本文中,我們將瞭解如何以 AM-PM 格式格式化時間。在 12 小時制時鐘系統中,使用 AM-PM 格式來定義時間。這裡,AM 代表 Ante meridiem,指的是中午之前的時間,而 PM 代表 Post meridiem,表示中午之後的時間段。示例場景 輸入:current_date = Thu Mar 17 16:04:31 IST 2024 輸出:當前時間 ... 閱讀更多

Java程式:從Java集合中刪除所有元素

Shriansh Kumar
更新於 2024年8月1日 11:42:49

1K+ 次瀏覽

我們得到一個包含多個元素的集合,我們的任務是編寫一個 Java 程式來刪除集合中的元素。這裡,集合是 Java 集合介面的一個子介面,它定義了一個數學集合。在 Java 中刪除集合元素 要刪除 Java 中給定集合中的元素,請使用以下方法 - 使用 clear() 方法 使用 removeAll() 方法 使用迭代器和 remove() 使用 Java clear() 方法 使用 clear() 方法,我們可以刪除所有元素 ... 閱讀更多

Java 中的 Pollard’s Rho 素因子分解演算法

Ankith Reddy
更新於 2020年6月25日 12:59:32

192 次瀏覽

這是一種對給定整數執行因式分解的演算法。以下是實現 Rho 演算法進行素因子分解的程式。程式即時演示 public class PollardsRho {    int num = 65;    public int gcd(int a, int b) {       int gcd = 0;       for(int i = 1; i

Java 中的埃拉托色尼篩法

George John
更新於 2020年6月25日 12:55:11

4K+ 次瀏覽

埃拉托色尼篩法是一種古老的演算法,用於查詢小於給定數字的所有素數。演算法 1. 生成從 2 到 n(給定數字)的整數。2. 從 2 開始,標記每個第二個整數。(2 的倍數)3. 現在,從 3 開始,標記每個第三個整數。(3 的倍數)4. 最後,從 5 開始,標記每個第五個整數。(5 的倍數)程式 import java.util.Scanner; public class SievePrimeFactors  {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("輸入一個數字");       int num = sc.nextInt();       boolean[] bool = new boolean[num];       ... 閱讀更多

Java 中小於或等於 n 的所有數字的尤拉 totient 函式

Chandu yadav
更新於 2020年6月25日 12:51:01

896 次瀏覽

以下是一個程式,用於在給出 n 時獲取小於或等於 n 的所有數字的尤拉 totient 函式的結果。程式 import java.util.Scanner; public class EulerTotient {    public static int gcd(int a,int b){       int i, hcf = 0;          for(i = 1; i

Java 中的尤拉準則

Arjun Thakur
更新於 2020年6月25日 12:50:12

427 次瀏覽

根據尤拉準則,當且僅當存在一個數字 num 使得 num%p 等於 n%p 時,n 在模 p 下的平方根才存在。程式 import java.util.Scanner; public class EulersCriterion {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("輸入 n 值:");             int n = sc.nextInt();       System.out.println("輸入 p 值:");       int p = sc.nextInt();       n = n % p;       int flag = 0;            for ... 閱讀更多

Java 中數字階乘的除數

Chandu yadav
更新於 2020年6月25日 12:47:39

169 次瀏覽

以下是一個 Java 程式,用於查詢數字階乘的除數。程式 import java.util.Scanner; public class DivisorsOfFactorial {    public static long fact(int i) {       if(i

Java 中的勒讓德公式

George John
更新於 2020年6月25日 12:46:49

197 次瀏覽

您可以使用勒讓德公式計算整除 n! 的最大素數冪的指數。程式 import java.util.Scanner; public class LegendresFormula {    static int Largestpower(int n, int p) {       int ans = 0;       while (n > 0) {          n /= p;          ans += n;       }       return ans;    }    public static void main (String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("輸入 n 值:");     ... 閱讀更多

Java 中的二項式係數

Ankith Reddy
更新於 2020年6月25日 12:41:38

4K+ 次瀏覽

二項式係數 (c(n, r) 或 nCr) 使用公式 n!/r!*(n-r)! 計算。以下是查詢給定整數的二項式係數的 Java 程式。程式 import java.util.Scanner; public class BinomialCoefficient {    public static long fact(int i) {       if(i

Java 中的乘法階

Arjun Thakur
更新於 2020年6月25日 12:39:40

96 次瀏覽

以下是一個 Java 程式,它列印給定數字的乘法階。import java.util.Scanner;程式 public class MultiplicativeOrder {    public static int gcd(int num1, int num2) {       if (num2 != 0) {          return gcd(num2, num1 % num2);       } else {          return num1;       }    }    static int multiplicativeOrder(int num1, int num2) {       if (gcd(num1, num2) != 1) {          return -1;       }       int res = 1;       int p ... 閱讀更多

廣告
© . All rights reserved.