1. 程式人生 > >素數遞歸統計三項代碼

素數遞歸統計三項代碼

scanner nbsp while tr1 ber rime package java ring

        public static void isprime() {
            int n;import java.util.Scanner;
public class number {
    public static void main(String[] args) {
            int i = 0;
            System.out.println("1.3-100 2.任意兩數 3.最大十個和最小十個");
            Scanner sc = new Scanner(System.in);
            i = sc.nextInt();
            
switch (i) { case 1: isprime(); break; case 2: isprime1(); break; case 3: isprime2(); break; } } int i = 2; int m = 0; for (n = 3; n >= 3 && n <= 100; n++) {
int j = 0; if (n % 2 == 0) j = 1; for (i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) j = 1; } if (j == 0) { m++; System.out.print(n
+ "\t"); if (m % 5 == 0) System.out.println(); } } System.out.println(); } public static void isprime1() { System.out.println("請輸入兩個整數:"); Scanner sc = new Scanner(System.in); int num1, num2; System.out.println("number1:"); num1 = sc.nextInt(); System.out.println("number2:"); num2 = sc.nextInt(); int n; int i = 2; int m = 0; for (n = num1; n >= num1 && n <= num2; n++) { int j = 0; if (n % 2 == 0) j = 1; for (i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) j = 1; } if (j == 0) { m++; System.out.print(n + "\t"); if (m % 5 == 0) System.out.println(); } } } public static void isprime2() { Scanner sc = new Scanner(System.in); System.out.println("請輸入兩個整數:"); int num1, num2; System.out.println("number1:"); num1 = sc.nextInt(); System.out.println("number2:"); num2 = sc.nextInt(); System.out.println("十個最小:"); int n; int i = 2; int m = 0; int k = 0; int a[] = new int[100]; for (n = num1; n >= num1 && n <= num2; n++) { int j = 0; if (n % 2 == 0) j = 1; for (i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) j = 1; } if (j == 0) { m++; a[m] = n; if (m <= 10) System.out.print(a[m] + "\t"); } } System.out.println(); System.out.println("十個最大:"); for (k = m - 10; k <= m; k++) { System.out.print(a[k] + "\t"); } } }
package huiwen;
import java.util.Scanner;
public class Huiwen {
public static void main(String[] args) {
    String s;
    int w;
    int q = 0;
    Scanner input = new Scanner(System.in);
    System.out.println("輸入一串字母:");
    s = input.next();
    char a[];
   a = s.toCharArray();
    w = a.length - 1;
    for (int k = 0; k < s.length(); k++) {
        System.out.print(a[k]);
    }
    huiwen(a, w, q);
}
 
public static void huiwen(char a[], int w, int q) {
    if (q >= w) {
        if (a[q] == a[w])
 
            System.out.println("是"); 
        System.exit(0);
    }
    if (a[q] == a[w])
        huiwen(a, --w, ++q);
    else {
        System.out.println("不是!");
        System.exit(0);
    }
}
}
package j;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.TreeMap;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class lolo{

        public static void main(String[] args) throws IOException {
            String addr="C:/jhl/lol.txt"; 
            BufferedReader bufr = new BufferedReader(new FileReader(addr));
            StringBuffer sbuf = new StringBuffer();
            String line = null;
            while ((line = bufr.readLine()) != null) {
                sbuf.append(line);
            }
            bufr.close();// 讀取結束
            Pattern expression = Pattern.compile("[a-zA-Z]+");
            String str1 = sbuf.toString().toLowerCase();
            Matcher matcher = expression.matcher(str1); 
            TreeMap myTreeMap = new TreeMap();
            int n = 0; 
            Object word = null; 
            Object num = null; 
            while (matcher.find()) { 
                word = matcher.group();
                n++;// 單詞數加1
                if (myTreeMap.containsKey(word)) {
                    num = myTreeMap.get(word);
                    Integer count = (Integer) num; 
                    myTreeMap.put(word, count.intValue() + 1);
                } else {
                    myTreeMap.put(word, new Integer(1));
                }
            }
            System.out.println("文章內容如下:");
            System.out.println(str1);
            System.out.println("統計分析如下:");
            System.out.println("文章中單詞總數:" + n+"個");
            System.out.println("具體的信息在當前目錄的lwl.txt文件中");
            BufferedWriter bufw=new BufferedWriter(new FileWriter("C:/jhl/lwl.txt"));
            Iterator iter=myTreeMap.keySet().iterator();
            Object key=null;
            bufw.write("文件來自:"+addr+"內容如下:");
            bufw.write(sbuf.toString());
            bufw.newLine();
            while(iter.hasNext()){
                 key=iter.next();
                 System.out.println(key+":"+myTreeMap.get(key));
                 
                 bufw.write((String)key+":"+myTreeMap.get(key));
                 bufw.newLine();
            }
            bufw.write("統計分析如下:");
            bufw.write("文章中單詞總數:" + n+"個");
            bufw.newLine();
            bufw.write("文章中不同單詞總數:" + myTreeMap.size()+"個");
            bufw.close();
        
}
}

素數遞歸統計三項代碼