- トピック
そのような数を見つけてください:数はその分解項目の加算に等しいです。例番号28は、1、2、4、7、14、1 + 2 + 4 + 7 + 14 = 28に分解できます。同様に、数値6は1、2、3、1 + 2 + 3 = 6に分解されます。コードを使用して、この条件を満たす1〜500内のすべての番号を検索します。
- 分析
分解項目とは:要するに、分解項目で除算され、余りなしで0になり、分解項目に属します。たとえば、28は一度に1から27に除算されますが、28への除算は半分であるため、分解項目は絶対に存在しないため、ループするときに必要なのはtemp / 2 +1だけです。このとき、要件を満たす番号を足し合わせれば、要件を満たす番号はそれ自体と同じになります。 - コード
package com.dairuijie.demo.study;
import java.util.ArrayList;
import java.util.List;
/**
*
* @模块名:Day01
* @包名:com.dairuijie.demo.study
* @描述:DecompositionTerm.java
* @版本:1.0
* @创建人:drj
* @创建时间:2020年3月28日下午10:45:07
*/
public class DecompositionTerm {
/**
*
*/
public static void main(String[] args) {
int total = 0;
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= 500; i++) {
int temp = i;
for (int j = 1; j <= temp / 2 + 1; j++) {
if (temp % j == 0) {
total = total + j;
}
}
if(total == temp) {
list.add(total);
}
System.err.println(String.format("数字:%s,分解项和%s", temp, total));
total = 0;
}
System.err.println(list);
}
}
- トピック2
すべてのサブディレクトリ内のファイルを含む、ディレクトリ内のすべてのファイルを一覧表示し、ファイルの総数を出力するプログラムを作成します
- 分析
これは、file.listFiles()を再帰的に検索してから、その数をカウントするためのものです。
- コード
package com.dairuijie.demo.study;
import java.io.File;
/**
*
* @模块名:Day01
* @包名:com.dairuijie.demo.study
* @描述:FIndFileCount.java
* @版本:1.0
* @创建人:drj
* @创建时间:2020年3月28日下午5:38:33
*/
public class FIndFileCount {
public static Integer count = 0;
public static void main(String[] args) {
File file = new File("D:\\music");
if(file.exists()) {
findFile(file);
}
System.out.println("文件夹数量:" + count);
}
public static void findFile(File file) {
File[] fs = file.listFiles();
if(fs != null) {
for(File f: fs) {
findFile(f);
}
}else {
count++;
System.err.println(file.getName());
}
}
}