您的位置:首页 > 其它

算法 第二次作业

2014-03-26 20:21 281 查看
import java.util.Scanner;
public class dsd {
public static void main(String args[]) {
long k1;
Scanner scan = new Scanner(System.in);
System.out.println("Enter one number:");

k1 = scan.nextInt();

isPrime(k1);
}

public static int isPrime(long a) {
boolean e = true;
for (long i = a - 1; i > 1; i--) {
if (a % i == 0) {
e = false;
}
}

if (e) {
System.out.println(1);

} else
System.out.println(0);
return 0;
}

}






2.2
import java.util.Random;  

import java.util.Scanner;  

  

public class dsd {  

    public static void main(String args[]) {  

        int k1;  

        Scanner scan = new Scanner(System.in);  

        System.out.println("Enter one number:");  

        k1 = scan.nextInt();  

        System.out.println(createRndInteger(k1));// 生成随机字节  

    }  

  

    public static long createRndInteger(int n) {  

        int a = 0;  

        // 判断输入的值  

        if (n <= 0)  

            return 0;  

        StringBuffer result = new StringBuffer("1");  

        for (int i = 0; i < n - 1; i++) {  

            boolean b = new Random().nextBoolean();  

  

            if (b) {  

                a = 1;  

            } else  

                a = 0;  

            result.append(a);  

        }  

        return Long.parseLong(result.toString());  

    }  

}  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法作业