A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© kelin410 中级黑马   /  2016-4-4 17:21  /  1333 人查看  /  26 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

1,需求:从键盘接收一个文件夹路径,统计该文件夹大小。
2,需求:从键盘接收一个文件夹路径,删除该文件夹。
3,需求:从键盘接收两个文件夹路径,把其中一个文件夹(包含内容)拷贝到另一个文件夹中。
4,需求:从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印。
例如下面这样的:
E:\aaa
        bbb.txt
        ccc.txt
        ddd.txt
        E:\aaa\eee
                fff.txt
                ggg.txt


5,不死神兔
         * 故事得从西元1202年说起,话说有一位意大利青年,名叫斐波那契。
         * 在他的一部著作中提出了一个有趣的问题:假设一对刚出生的小兔一个月后就能长成大兔,再过一个月就能生下一对小兔,并且此后每个月都生一对小兔,一年内没有发生死亡,
         * 问:一对刚出生的兔子,一年内繁殖成多少对兔子?
6,需求:已知一个算法,给定一个结果就返回另一个结果;具体如下:
         *                 给1        得1
         *                 给2        得1
         *                 给3        得2
         *                 给4        得4
         *                 给5        得7
         *                 给6        得13
         *                 ………………
         * 请用代码实现,给定任意数都能得到符合上面逻辑的结果。

7,需求:求出1000的阶乘所有零和尾部零的个数,正确答案是:249个0。请用代码写出来。
8,需求:约瑟夫环 (求幸运数),这个不理解的可以去百度,但是不要看百度的代码。尽量自己写出来。

26 个回复

倒序浏览
给了问题什么时候给出答案来大家参考下哈
回复 使用道具 举报
251855915 发表于 2016-4-4 17:25
给了问题什么时候给出答案来大家参考下哈

你先自己试试看,过l两天我会公布答案。
回复 使用道具 举报
给你10000个赞   
回复 使用道具 举报
第6题:
  1. package com.heima.demo;
  2. import java.util.Scanner;
  3. public class Demo {
  4.         public static void main(String[] args) {
  5.                 Scanner sc = new Scanner(System.in);
  6.                 System.out.println(getNum(sc.nextInt()));
  7.         }
  8.         public int getNum(int num) {
  9.                 if(num == 1 || num == 2) {
  10.                         return 1;
  11.                 }else if(num == 3){
  12.                         return getNum(num - 2) + getNum(num - 1);
  13.                 }else {
  14.                         return getNum(num - 3) + getNum(num - 2) +getNum(num - 1);
  15.                 }
  16.         }
  17. }
复制代码

点评

不错,赞一个!  发表于 2016-4-5 08:30
回复 使用道具 举报
第7题:
  1. package com.heima.demo;
  2. public class Demo {
  3.         public static void main(String[] args) {
  4.                 System.out.println(getCount(1000));
  5.         }
  6.         public int getCount(int num) {
  7.                 if(num >= 1 && num < 5) {
  8.                         return 0;
  9.                 }else {
  10.                         return num/5 + getCount(num / 5);
  11.                 }
  12.         }
  13. }
复制代码

点评

不错,赞一个!有注释就更好了!  发表于 2016-4-5 08:31
回复 使用道具 举报
第7题:
  1. package com.heima.demo;
  2. import java.math.BigInteger;
  3. public class Demo {
  4.         public static void main(String[] args) {
  5.                 BigInteger b = new BigInteger( 1 + "");
  6.                 for(int x = 1; x <= 1000; x++) {
  7.                         b = b.multipy(new BigInteger(x + ""));
  8.                 }
  9.                 String str = b.toString();
  10.                 int count = 0;
  11.                 for(int x = 0; x < str.length; x++) {
  12.                         char ch = str.charAt(x);
  13.                         if(ch == '0') {
  14.                                 count++;
  15.                         }
  16.                 }
  17.                 System.out.println("1000的阶乘中含有:" + count + "个零!");
  18.         }
  19. }
复制代码

点评

这个是求所有0的不错,但是有两个地方代码写错了! 第一处: b.multiply()方法 你写成了b.multipy() 第二处:str.length()方法你写成了str.length  发表于 2016-4-5 08:38
回复 使用道具 举报

写错了  str.length()
回复 使用道具 举报
第8题:
  1. package com.heima.demo;
  2. import java.util.Scanner;
  3. import java.util.LinkedList;
  4. public class Demo {
  5.         public static void main(String[] args) {
  6.                 Scanner sc = new Scanner(System.in);
  7.                 LinkedList<Integer> ll = new LinkedList<>();
  8.                 for(int x =1; x <= sc.nextLine(); x++) {
  9.                         ll.add(x);
  10.                 }
  11.                 int count = 0;
  12.                 for(int index = 0; ll.size() != 1; index++) {
  13.                         if(index == ll.size()) {
  14.                                 index = 0;
  15.                         }
  16.                         if(count % 3 == 0) {
  17.                                 ll.remove(index--);
  18.                         }
  19.                         count++;
  20.                 }
  21.                 System.out.println(ll.get(0));
  22.         }
  23. }
复制代码

点评

不错,赞一个。如果有代码注释就更好了。  发表于 2016-4-5 13:13
回复 使用道具 举报
第一题:
  1. package com.heima.demo;
  2. import java.util.Scanner;
  3. import java.io.File;
  4. public class Demo {
  5.         public static void main(String[] args) {
  6.                 Scanner sc = new Scanner(System.in);
  7.                 File dir = new File(sc.nextLine());
  8.                 System.out.println(getLength(dir));
  9.         }
  10.         public int getLength(File dir) {
  11.                 int length = 0;
  12.                 File[] files = dir.listFiles();
  13.                 for(File f : files) {
  14.                         if(f.isFile()) {
  15.                                 length += f.length();
  16.                         }else {
  17.                                 length += getLength(f);
  18.                         }
  19.                 }
  20.                 return length;
  21.         }
  22. }
复制代码

点评

不错,赞一个。如果有代码注释就更好了。  发表于 2016-4-5 13:14
回复 使用道具 举报
第二题:
  1. package com.heima.demo;
  2. import java.util.Scanner;
  3. import java.io.File;
  4. public class Demo {
  5.         public static void main(String[] args) {
  6.                 Scanner sc = new Scanner(System.in);
  7.                 File dir = new File(sc.nextLine());
  8.                 deleteDir(dir);
  9.         }
  10.         public void deleteDir(File dir) {
  11.                 File[] files = dir.listFiles();
  12.                 for(File f : files) {
  13.                         if(f.isFile()) {
  14.                                 f.delete();
  15.                         }else {
  16.                                 deleteDir(f);
  17.                         }
  18.                 }
  19.                 dir.delete();
  20.         }
  21. }
复制代码

点评

不错,赞一个。如果有代码注释就更好了。  发表于 2016-4-5 13:16
回复 使用道具 举报
第三题:
  1. package com.heima.demo;
  2. import java.util.Scanner;
  3. import java.io.File;
  4. public class Demo {
  5.         public static void main(String[] args) throws IOException {
  6.                 Scanner sc = new Scanner(System.in);
  7.                 System.out.println("请输入源文件夹路径名:");
  8.                 File src = new File(sc.nextLine());
  9.                 System.out.println("请输入目的文件夹路径名:");
  10.                 File dest = new File(sc.nextLine());
  11.                 copyDir(src, dest);
  12.         }
  13.         public static void copyDir(File src, File dest) throws IOException {
  14.                 File newDir = new File(dest, src);
  15.                 newDir.mkdir();
  16.                 File[] files = dest.listFiles();
  17.                 for(File f : files) {
  18.                         if(f.isFile()) {
  19.                                 BufferedInputStream bufis = new BufferedInputStream(new FileInputStream(f));
  20.                                 BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(new File(newDir, f.getName())));
  21.                                 byte[] buf = new byte[8192];
  22.                                 int len = 0;
  23.                                 while(( len = bufis.read(buf))) {
  24.                                         bufos.write(buf, 0 , len);
  25.                                         bufos.flush();
  26.                                 }
  27.                                 bufos.close();
  28.                                 bufis.close();
  29.                         }else {
  30.                                 copyDir(f, newDir);
  31.                         }
  32.                 }
  33.         }
复制代码
回复 使用道具 举报
第四题:
  1. package com.heima.demo;
  2. import java.util.Scanner;
  3. import java.io.File;
  4. public class Demo {
  5.         public static void main(String[] args) throws IOException {
  6.                 Scanner sc = new Scanner(System.in);
  7.                 File dir = new File(sc.nextLine());
  8.                 printDir(dir, 0);
  9.         }
  10.         public static void printDir(File dir, int row) {
  11.                 File[] files = dir.listFiles();
  12.                 for(File f : files) {
  13.                         for(int x = 0; x <= row; x++) {
  14.                                 System.out.println("\t");
  15.                         }
  16.                         System.out.println(f);
  17.                         if(f.isDirectory()) {
  18.                                 printDir(f, row + 1);
  19.                         }
  20.                 }
  21.         }
  22.        
  23. }
复制代码

点评

不错,赞一个。如果有注释就更好了!  发表于 2016-4-5 08:39
回复 使用道具 举报
第五题:
  1. package com.heima.demo;
  2. import java.util.Scanner;
  3. public class Demo {
  4.         public static void main(String[] args) throws IOException {
  5.                 Scanner sc = new Scanner(System.in);
  6.                 System.out.println(getCount(sc.nextInt()));
  7.         }
  8.         public static int getCount(int num) {
  9.                 if(num == 1 || num == 2) {
  10.                         return 1;
  11.                 }else {
  12.                         return getCount(num - 2) + getCount(num - 1);
  13.                 }
  14.         }
  15.        
  16. }
复制代码

点评

不错,赞一个。  发表于 2016-4-5 08:22
回复 使用道具 举报
第七题,我自己的三种做法。
  1. package com.heima.task;

  2. import java.math.BigInteger;

  3. public class Test7 {

  4.         /**
  5.          * 需求:求出1000的阶乘尾部零的个数,不用递归做
  6.          */
  7.         public static void main(String[] args) {
  8.                 demo1();                //第一种方式
  9.                 demo2();                //第二种方式
  10.                 demo3();                //第三种方式
  11.         }

  12.         private static void demo3() {
  13.                 //求,1000的阶乘。
  14.                 BigInteger bi1 = new BigInteger("1");
  15.                 for (int x = 1; x <= 1000; x++) {
  16.                         BigInteger bi2 = new BigInteger(x + "");
  17.                         bi1 = bi1.multiply(bi2);
  18.                 }
  19.                 //将得出的结果转换为字符串
  20.                 String str = bi1.toString();
  21.                 //定义计数器
  22.                 int count = 0;
  23.                 //通过倒序遍历获得尾部0的个数
  24.                 for (int x = str.length()-1; x > 0; x--) {
  25.                         if ('0' == str.charAt(x))
  26.                                 count++;
  27.                         else
  28.                                 break;
  29.                 }
  30.                 System.out.println(count);
  31.         }

  32.         private static void demo2() {
  33.                 //求,1000的阶乘。
  34.                 BigInteger bi1 = new BigInteger("1");
  35.                 for (int x = 1; x <= 1000; x++) {
  36.                         BigInteger bi2 = new BigInteger(x + "");
  37.                         bi1 = bi1.multiply(bi2);
  38.                 }
  39.                 //定义计数器
  40.                 int count = 0;
  41.                 //将得到的结果通过StringBuilder反转后再变成字符串,然后正序遍历字符串。如果是0计数器就增加,否则就跳出循环。
  42.                 String str = new StringBuilder(bi1.toString()).reverse().toString();
  43.                 for (int x = 0; x < str.length(); x++) {
  44.                         if ('0' != str.charAt(x))
  45.                                 break;
  46.                         else
  47.                                 count++;
  48.                 }
  49.                 System.out.println(count);
  50.         }

  51.         private static void demo1() {
  52.                 //求,1000的阶乘。
  53.                 BigInteger bi1 = new BigInteger("1");
  54.                 for (int x = 1; x <= 1000; x++) {
  55.                         BigInteger bi2 = new BigInteger(x + "");
  56.                         bi1 = bi1.multiply(bi2);
  57.                 }
  58.                 //将得出的结果转换为字符串
  59.                 String str = bi1.toString();
  60.                 //定义计数器
  61.                 int count = 0;
  62.                 //遍历整个字符串,如果是0 计数器就自增一次。如果不是就将计数器清零。
  63.                 for (int x = 0; x < str.length(); x++) {
  64.                         if ('0' == str.charAt(x))
  65.                                 count++;
  66.                         else
  67.                                 count = 0;
  68.                 }
  69.                 System.out.println(count);
  70.         }

  71. }
复制代码
回复 使用道具 举报

嗯 谢谢提醒 文档写的 没太注意  不过大致意思应该是对的
回复 使用道具 举报
菊花爆满山 发表于 2016-4-5 12:13
嗯 谢谢提醒 文档写的 没太注意  不过大致意思应该是对的

嗯,写的很不错。思路很清晰。
回复 使用道具 举报
楼主给力啊,前面都是自学的,看完视频自己写的时候有些会忘记,马上进入基础班学习了,加油

点评

加油,看好你!  发表于 2016-4-7 20:09
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马