Skip to content

Instantly share code, notes, and snippets.

@HaoweiCh
Created May 24, 2018 12:49
Show Gist options
  • Save HaoweiCh/56d60e96c3f135f67f3313a07e34f9e3 to your computer and use it in GitHub Desktop.
Save HaoweiCh/56d60e96c3f135f67f3313a07e34f9e3 to your computer and use it in GitHub Desktop.
package com.school;
import java.util.Scanner;
interface Task {
public String getName();
public void run();
}
class Task1 implements Task {
@Override
public String getName() {
return "Task 1 水仙花数";
}
@Override
public void run() {
for (int i = 100; i < 999; i++) {
int a = i / 100 % 100, b = i / 10 % 10, c = i % 10;
int sum = (int) (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3));
// System.out.println("a = " + a);
// System.out.println("b = " + b);
// System.out.println("c = " + c);
// System.out.println("sum = " + sum);
if (sum == i) System.out.println(i);
}
System.out.println(" ");
}
}
class Task2 implements Task {
private int getFib(int n) {
if (n < 0) {
return -1;
} else if (n == 0 || n == 1) {
return 1;
} else {
return getFib(n - 1) + getFib(n - 2);
}
}
@Override
public String getName() {
return "Task 2 兔子总数";
}
private void method1(int monthToShow) {
long firstMonth = 0, secondMonth = 1, birthMonth = 0;
for (int i = 0; i < monthToShow; i++) {
if (i < 2) System.out.printf("第 %d 月, 一共有 %d 只兔子\n", i + 1, firstMonth);
else {
birthMonth += secondMonth;
secondMonth = firstMonth;
firstMonth = birthMonth;
System.out.printf("第 %d 月, 一共有 %d 只兔子\n", i + 1, firstMonth + secondMonth + birthMonth);
}
}
}
private void method2(int monthToShow) {
for (int i = 0; i < monthToShow; i++) {
System.out.printf("第 %d 月, 一共有 %d 只兔子\n", i + 1, this.getFib(i));
}
}
@Override
public void run() {
{
Scanner sc = new Scanner(System.in);
System.out.print("遍历多少个月数: ");
int monthToShow = sc.nextInt();
// this.method1(monthToShow);
this.method2(monthToShow);
}
}
}
class Task3 implements Task {
@Override
public String getName() {
return "Task 3 素数判断";
}
private boolean isPrime(int in) {
for (int i = 2; i < in; i++) {
//若能除尽,则不为质数
if ((in % i) == 0) {
return false;
}
}
return true;
}
@Override
public void run() {
for (int i = 101; i < 200; i++) {
if (this.isPrime(i)) System.out.println(i);
}
}
}
class Task4 implements Task {
@Override
public String getName() {
return "Task 4 正整数分解质因数";
}
private String resolvePrime(int target) {
StringBuilder stringBuilder = new StringBuilder(target + " = ");
int minimumPrime = 2;
// 辗转相除法
while (minimumPrime <= target) {
if (target % minimumPrime == 0) {
stringBuilder.append(minimumPrime).append(" * ");
target /= minimumPrime;
minimumPrime = 2;
} else {
minimumPrime++;
}
}
stringBuilder.setLength(stringBuilder.length() - 3);
return stringBuilder.toString();
}
@Override
public void run() {
Scanner sc = new Scanner(System.in);
System.out.print("分解质因数: ");
int nextInt = sc.nextInt();
// this.method1(monthToShow);
System.out.println(
this.resolvePrime(nextInt)
);
}
}
class Task5 implements Task {
@Override
public String getName() {
return "Task 5 条件运算符";
}
@Override
public void run() {
Scanner sc = new Scanner(System.in);
System.out.print("输入学习成绩: ");
int nextInt = sc.nextInt();
System.out.println(nextInt < 60 ? 'C' : nextInt < 89 ? 'B' : 'A');
}
}
class Task6 implements Task {
private int a, b;
@Override
public String getName() {
return "Task 6";
}
private int maxCommonDivisor() {
int m = a, n = b;
if (m < n) {
int temp = m;
m = n;
n = temp;
}
while (m % n != 0) {
int temp = m % n;
m = n;
n = temp;
}
return n;
}
private int minCommonMultiple() {
return a * b / maxCommonDivisor();
}
@Override
public void run() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入两个数: ");
a = sc.nextInt();
b = sc.nextInt();
System.out.printf("最大公约数 %d 最小公倍数 %d\n", maxCommonDivisor(), minCommonMultiple());
}
}
class Task7 implements Task {
@Override
public String getName() {
return "Task 7 100 内完全数";
}
@Override
public void run() {
for (int i = 1; i <= 1000; i++) {
int factor = 0;
for (int j = 1; j < i / 2 + 1; j++) {
if (i % j == 0)
factor += j;
}
if (factor == i)
System.out.println(" " + i);
}
}
}
class Task8 implements Task {
@Override
public String getName() {
return "Task 8 球落地问题";
}
@Override
public void run() {
final Integer HEIGHT = 100;
final Integer TIME = 10;
double now = HEIGHT;
double past = 0.0;
for (int i = 0; i < TIME; i++) {
past += now * 1.5;
now /= 2;
}
System.out.printf("第 %d 次经过 %f 米\n第 %d 次经过反弹 %f 米", TIME, past, TIME, now);
}
}
public class Main {
public static void main(String[] args) {
Task[] tasks = {
new Task1(),
new Task2(),
new Task3(),
new Task4(),
new Task5(),
new Task6(),
new Task7(),
new Task8(),
};
for (Task task : tasks) {
System.out.println("========= " + task.getName() + " ==========\n");
task.run();
System.out.println(" ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment