本帖最后由 朱晓杰 于 2013-7-12 17:15 编辑
- /**
- * 题目2、求出1-100之间的素数,假设有n个,将这些素数存入a[n+1]的数组中, 其中n个是求出的各个素数,还有一个是随机添加其中的一个素数
- * 这样数组中就有相同的两个素数,写一个搜索算法,找出数组中相同的那个素数。(随机添加的素数,可手动指定,也可利用随机方法产生)
- * (注:存入数组后,数组中的所有元素理论上是无序的,相同的那个素数不一定就等于a[n],而是被存入了数组中的某个角标上)
- *
- * 分析: 1、找出1-100之间的素数,并存入数组;
- * 2、产生你找出素数中随机的一个,也存入数组,位置不确定;
- * 3、找出数组中相同的那个素数。
- *
- * 问题:1.什么是素数?由于1-100间的素数的个数不确定,所以数组如何定义?
- *
- * 2.随机素数如何产生?手动?随机数?
- *
- * 3.如何搜索相同的那个素数?且保证程序效率高
- * 查找相同的那个素数,可以用遍历数组的方法,可是效率有点儿低,需要遍历所有数组,那么折半查找呢?
- *
- */
- import java.util.*;
- public class SearchPrime {
- public static void main(String[] args) {
- int maxNum = 100,primeNum = 17;
- Integer[] primeArrays = getPrimeArrays(maxNum,primeNum);
- Arrays.sort(primeArrays);
- int index = Arrays.binarySearch(primeArrays, primeNum);
- System.out.println("数组中与 " + primeNum + " 相同的素数的位置是:primeArrays[" + index + "]=" + primeArrays[index]);
- }
-
- /**
- * @功能:求出1-100间的素数,手动指定一个素数,并将求出的素数与指定的素数存入数组
- * @params:maxNum,primeNum
- * @return:Integer[]
- * */
- public static Integer[] getPrimeArrays(int maxNum,int primeNum){
- Integer[] primeArrays = null;
- int count = 0;
- Collection<Integer> collect = new ArrayList<Integer>();
- for(int i = 2; i < maxNum; i++){
- //判断一个数是否为素数
- if(isPrimeNum(i)){
- collect.add(i);
- count++;
- }
- }
- collect.add(primeNum);
- primeArrays = collect.toArray(new Integer[count+1]);
- return primeArrays;
- }
-
- /**
- * @功能:判断一个正整数是否为质数(素数)
- * @param:num
- * @return:boolean
- * */
- public static boolean isPrimeNum(int num){
- boolean flag = true;
- for(int i = num - 1; i > 1; i--){
- if(num % i == 0){
- flag = false;
- break;
- }
- }
- return flag;
- }
- }
复制代码- import static java.lang.System.out;
- import java.io.*;
- /**
- * 题目3. 背景:在一个文件夹内有文件夹和文件,文件名的格式为:数字+名称.java。
- *
- * 要求:把所有的.java文件复制一份,变为文件名格式为:数字+名称+数字.java。
- * 其中数字是相对应的,名称由字母组成。如:01Demo.java——>01Demo01.java
- *
- * 分析:1.在一个文件夹内有文件夹和文件, 需要遍历文件夹,用到了递归
- *
- * 2.更改文件名,需要对文件名进行操作,用到了字符串操作
- */
- public class CopyFiles {
- public static void main(String[] args) {
-
- String path = "C://java//";
-
- File rootPath = new File(path);
-
- if(!rootPath.exists()){
- out.println(rootPath + "目录或文件不存在!");
- }else if(rootPath.isFile()){
- out.println(rootPath + "是一个文件!");
- }else if(rootPath.isDirectory()){
- try {
- FileUtils(rootPath);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
- /**
- * @功能:文件操作路
- * @param:rootPath
- * @return:void
- * */
- private static void FileUtils(File rootPath)throws Exception{
- File[] files = rootPath.listFiles();
- for(File file : files){
- if(file.isFile()){
- //重命名文件
- renameFiles(file);
- }else if(file.isDirectory()){
- //递归继续遍历
- FileUtils(file);
- }
- }
- }
-
- /**
- * @功能:重命名文件
- * @param:file
- * @return:void
- * */
- private static void renameFiles(File file) throws Exception{
- //获取上级根目录
- String parentPath = file.getParent() + File.separator;
- //文件名
- String fileName = file.getName();
- //重命名后的文件名
- String newFileName = "";
- //重命名后的文件
- File destFile = null;
- if(fileName.endsWith(".java")){
- fileName = fileName.substring(0,fileName.indexOf(".java"));
- newFileName = fileName + fileName.substring(0,fileName.indexOf("Demo")) + ".java";
- destFile = new File(parentPath + newFileName);
- //复制文件
- copyFile(file, destFile);
- }
- }
-
- /**
- * @功能:复制文件
- * @param:srcFile,destFile
- * @return:void
- * */
- private static void copyFile(File srcFile, File destFile)
- throws FileNotFoundException, IOException {
- BufferedReader reader = new BufferedReader(new FileReader(srcFile));
- BufferedWriter writer = new BufferedWriter(new FileWriter(destFile));
- String content = "";
- while((content = reader.readLine()) != null){
- writer.write(content);
- writer.newLine();
- writer.flush();
- }
- if(writer != null){
- writer.close();
- }
- if(reader != null){
- reader.close();
- }
- }
- }
复制代码 |