1、判断素数
/**
* Created by "Seaside" on 2016/12/15.
*/
public class isPrime {
/*
* 判断素数
* */
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
int num2 = isPrime(i);
if (num2 != 0) {
System.out.println(num2);
}
}
}
static int isPrime(int num) {
boolean flag = false;
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0) {
return 0;
}
}
return num;
}
}
2、因式分解
/**
* Created by "Seaside" on 2016/12/15.
*/
public class isZhiYinShuo {
/*
* 分解质因数:找到和是最小的那一组
* 如:12+2*6
* 12=3*4
* 12=2*2*3
* 这组和是最小的
* */
public static void main(String[] args) {
ZhiYinShuoFenJie(12);
}
static void ZhiYinShuoFenJie(int num) {
if (isPrime(num)) {
System.out.println(num);
} else {
for (int i = 2; i < Math.sqrt(num); i++) {
if (num % i == 0) {
ZhiYinShuoFenJie(i);
ZhiYinShuoFenJie(num / i);
break;
}
}
}
}
static boolean isPrime(int num) {
boolean flag = true;
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0) {
flag = false;
break;
}
}
return flag;
}
}
3.水仙花数
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
/**
* Created by "Seaside" on 2016/12/15.
*/
public class ArmstrongNumber {
/*
* 水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153) *
* */
static int one = 1;
static int two = 2 * 2 * 2;
static int three = 3 * 3 * 3;
static int four = 4 * 4 * 4;
static int five = 5 * 5 * 5;
static int six = 6 * 6 * 6;
static int seven = 7 * 7 * 7;
static int eight = 8 * 8 * 8;
static int nine = 9 * 9 * 9;
static int zero = 0;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
System.out.println(isArmstrongNumber(number));
}
static boolean isArmstrongNumber(int num) {
int temp=num;
boolean flag = false;
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
hm.put(0, zero);
hm.put(1, one);
hm.put(2, two);
hm.put(3, three);
hm.put(4, four);
hm.put(5, five);
hm.put(6, six);
hm.put(7, seven);
hm.put(8, eight);
hm.put(9, nine);
int sum = 0;
do {
if (num < 10) {
sum += hm.get(num);
break;
}
int k = num % 10;
num /= 10;
sum += hm.get(k);
} while (num >= 0);
if (sum == temp) {
flag = true;
}
return flag;
}
}
4、成绩判断+猴子吃桃问题
/**
* Created by "Seaside" on 2016/12/15.
*/
public class ScoreMonkey {
/*
* 学生成绩+猴子吃桃子的问题
* */
public static void main(String[] args) {
// Score(100);
Monkey();
}
static void Score(int num) {
/*
* 成绩是0-100
* */
String result = "";
switch (num / 10) {
case 10:
result = "非常棒!";
break;
case 9:
result = "优秀!";
break;
case 8:
result = "良好!";
break;
case 7:
result = "良";
break;
case 6:
result = "及格";
break;
default:
result = "不及格,有待努力!";
}
System.out.println(result);
}
public static void Monkey() {
/*
* 猴子吃枣问题。猴子摘了一堆枣。第一天吃了一半,还嫌不过瘾,又吃一个;第二天又吃了剩下的一半零一个;
以后每天如此。到第十天,猴子一看只剩下一个了。问第一天有多少个枣子?
* */
int[] touzi = new int[10]; //声明一个整型数组,用来存储每天剩余的桃子数量
touzi[9] = 1; //第十天剩下一个桃子
for (int i = 8; i >= 0; i--) {
touzi[i] = (touzi[i + 1] + 1) * 2; //每前一天的桃子数都等一现在剩余的桃子数加一在乘以二
}
System.out.println("第一天摘的桃子数为:" + touzi[0]); //输出第一天共摘的桃子数
}}
//递归实现猴子吃桃问题 static int sum = 0; static int Monkey_DiGui(int day) { if (day == 10) { return 1; } sum += 2*Monkey_DiGui(day + 1)+2; return sum; }